Makefile export .o file to a different path than .cpp

前端 未结 2 1386
天命终不由人
天命终不由人 2020-12-22 09:33

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 o

相关标签:
2条回答
  • 2020-12-22 10:12

    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 is CXX and CXXFLAGS, e.g. with (I am extracting this from my make -p output)

    LINK.cc = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
    COMPILE.cc = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
    CXX = g++
    %.o: %.cc
          $(COMPILE.cc) $(OUTPUT_OPTION) $<
    

    I strongly suggest to have CXXFLAGS= -Wall -g at least during the development phase. Learn also to use gdb and valgrind.

    You could have the following in your Makefile

     CXXFLAGS= -g -Wall
     SOURCES=f1.cc f2.cc
     SOURCE_PATH=yoursourcedir/
     OBJECT_PATH=yourobjectdir/
     SRCFILES=$(patsubst %.cc,$(SOURCE_PATH)/%.cc,$(SOURCES))
     OBJFILES=$(patsubst %.cc,$(OBJECT_PATH)/%.o,$(SOURCES))
     PROGFILE=$(OBJECT_PATH)
     .PHONY: all clean
     all: $(PROGFILE)
     $(PROGFILE): $(OBJFILES)
             $(LINK.cc) $^ $(LOADLIBES) $(LDLIBS) -o $@
     $(OBJECT_PATH)/%.o: $(SOURCE_PATH)/%.cc
             $(COMPILE.cc)  $(OUTPUT_OPTION) $<
     clean:
             $(RM) $(OBJECT_PATH)/*.o $(PROGFILE)
    
    0 讨论(0)
  • 2020-12-22 10:15

    try

    $(OBJECT_PATH)/file1.o: $(SOURCE_PATH)/file2.cpp $(SOURCE_PATH)/file1.cpp
        $(CC) $(CFLAGS) $^ -c $@
    

    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

    0 讨论(0)
提交回复
热议问题