G ++: fatal error: cannot specify -o with -c, -S or -E with multiple files

I am trying to compile a library file using other library files. I use the following line in my makefile to create gameobject.o:

lib/gameobject.o: src/gameobject.cpp src/vector.hpp lib/objectevent.o lib/sprite.o
g++ $^ -c -o $@ $(SFML_FLAGS)

All dependencies are copied correctly, but I get the following error when I try to compile gameobject.o:

g++: fatal error: cannot specify -o with -c, -S or -E with multiple files

I'm still a little new to using make / separating compilation, so I'm not quite sure what to do. Should I just compile it without setting output? Should I compile gameobject.o without using any of my other .o files? If this is true, would compilation time not be large for large objects if you cannot compile libraries with other libraries? Or am I just reading this error completely wrong?

+5
source share
1 answer

You are not creating a library file here. A file .ois an object file. Typically, there is one object file for the source file. When you use the compiler option -c, it takes one source file and compiles it into one object file. You can not add additional object files to an existing object file, so adding files .oand .cppin the same line with the compiler -cwill not work.

If you want to create a library, it will be something like libfoo.a(here "a" means "archive"). If you want to create an executable file, you can also do this.

, , , .

+8

All Articles