Creating two separate executables from a makefile (g++)

后端 未结 2 1587
广开言路
广开言路 2021-02-05 12:50

Currently, I have my makefile set up to compile and make a fairly large project. I have written a second cpp file with main function for running tests. I want these to run sep

2条回答
  •  梦如初夏
    2021-02-05 13:21

    Here's one way to do it:

    CXXFLAGS += -std=c++11 -Wall -O3
    
    all: myprog mytest
    
    myprog.cpp: main.cpp
        cp -vf $< $@
    myprog: myprog.o Foo.o Bar.o Test.o A.o B.o C.o
    
    mytest.cpp: main.cpp
        cp -vf $< $@
    mytest.o: CPPFLAGS += -DDEBUG
    mytest.o: CXXFLAGS += -O0 -g
    mytest: mytest.o Foo.o Bar.o Test.o A.o B.o C.o
    

    This works because built-in rules exist for compiling objects from c++ source (%.o: %.cpp) and linking main programs (%: %.o).

    Also note the use of target-specific values for the variables CPPFLAGS and CXXFLAGS.

提交回复
热议问题