So my task is simple, I have created the makefile (New with makefiles) and I want to keep my .o files in a different folder to have a cleaner directory and allow the usage of .o files by others.
I searched and found many solution pointing to using -o $< $@
However, it is giving me that g++: cannot specify -o with -c or -S with multiple files
This is what I want to do:
$(OBJECT_PATH)/file1.o: $(SOURCE_PATH)/file2.cpp $(SOURCE_PATH)/file1.cpp
$(CC) $(CFLAGS) $(SOURCE_PATH)/file2.cpp $(SOURCE_PATH)/file1.cpp -o $@
file1.cpp has #include "file1.h", so from what I read I should include file1.cpp in the dependencies. However, now I can't export to a different directory.
Is there a solution? Or do I have the concept wrong?
try
and check that CFLAGS doesn't include -o -c or -s flags
also read about implicit rules. it might help you to orginzie your makefile
Use
make -d
or even better remake-x
to understand what commands are invoked.Run also
make -p
to understand what builtin rules are used.We cannot help you more, because we have no idea if you redefined
CFLAGS
.And C++ compilation should better be done with
g++
that isCXX
andCXXFLAGS
, e.g. with (I am extracting this from mymake -p
output)I strongly suggest to have
CXXFLAGS= -Wall -g
at least during the development phase. Learn also to usegdb
andvalgrind
.You could have the following in your
Makefile