【问题标题】:creating two outputs with Makefile使用 Makefile 创建两个输出
【发布时间】:2021-12-18 23:55:00
【问题描述】:

我想创建一个 makefile,它在 C++ 中运行程序一次,使用“CXXFLAGS = -std=c++11 -g -O3 -DTEST -fopenmp”,一次使用:“CXXFLAGS = -std=c++11 -g -O3 -fopenmp" 最后输出两个不同的文件,如 P1-Test 和 P1。如何编辑此文件?

CXX = g++
CXXFLAGS = -std=c++11 -g -O3 -fopenmp

ifdef code_coverage
    GCOV_FLAG :=  -DTEST
else
    GCOV_FLAG :=
endif

all: P1
        @echo The program has been compiled

# implicit rule: create x from x.cpp
.cpp:
        $(CXX) $(CXXFLAGS) $? -o $@
        $(CXX) $(CXXFLAGS) $(GCOV_FLAG) $? -o $@
.PHONY: clean
clean:
        $(RM) -r P1 *.dSYM

【问题讨论】:

    标签: c++ c++11 makefile openmp gnu-make


    【解决方案1】:

    我的建议:

    CXX = g++
    CXXFLAGS = -std=c++11 -g -O3 -fopenmp
    
    all: P1 P1-Test
        @echo The program has been compiled
    
    # implicit rule: create x from x.cpp
    .cpp:
        $(CXX) $(CXXFLAGS) $? -o $@
    
    .PHONY: clean
    clean:
        $(RM) -r P1 *.dSYM
    
    P1: main.o second.o
        $(CXX) $(CXXFLAGS) $(LDFLAGS) -o "$@" $^
    
    P1-Test: CXXFLAGS+=-DTEST
    P1-Test: main.o second.o
        $(CXX) $(CXXFLAGS) $(LDFLAGS) -o "$@" $^
    

    带有示例来源:

    • 文件main.cpp

       extern void foo(); // should be in second.h or something
      
       int main() { foo(); }
      
    • 文件second.cpp

       #include <cstdio>
      
       void foo() {
       #ifdef TEST
           puts("TEST defined");
       #else
           puts("TEST not defined");
       #endif
       }
      

    结果

    $ make -B
    g++ -std=c++11 -g -O3 -fopenmp -o "P1" main.cpp second.cpp 
    g++ -std=c++11 -g -O3 -fopenmp -DTEST -o "P1-Test" main.cpp second.cpp 
    The program has been compiled
    

    当然还有输出:

    ./P1; ./P1-Test 
    TEST not defined
    TEST defined
    

    另类

    如果您的.o 文件真的很珍贵,您可能需要构建单独的副本。这里我分成release/main.otest/main.o

    CXX = g++
    CXXFLAGS = -std=c++11 -g -O3 -fopenmp
    
    all: P1 P1-Test
        @echo The program has been compiled
    
    test/%.o: CXXFLAGS+=-DTEST
    test/%.o: %.cpp
        mkdir -pv $(@D)
        $(CXX) $(CXXFLAGS) $? -c -o $@
    
    release/%.o: %.cpp
        mkdir -pv $(@D)
        $(CXX) $(CXXFLAGS) $? -c -o $@
    
    .PHONY: clean
    clean:
        $(RM) -rfv P1 P1-Test *.dSYM release/ test/
    
    P1: release/main.o release/second.o
    P1-Test: test/main.o test/second.o
    
    P1 P1-Test:
        $(CXX) $(CXXFLAGS) -o "$@" $^ $(LDFLAGS)
    

    这给出了:

    mkdir -pv release
    g++ -std=c++11 -g -O3 -fopenmp main.cpp -c -o release/main.o
    mkdir -pv release
    g++ -std=c++11 -g -O3 -fopenmp second.cpp -c -o release/second.o
    g++ -std=c++11 -g -O3 -fopenmp -o "P1" release/main.o release/second.o 
    mkdir -pv test
    g++ -std=c++11 -g -O3 -fopenmp -DTEST main.cpp -c -o test/main.o
    mkdir -pv test
    g++ -std=c++11 -g -O3 -fopenmp -DTEST second.cpp -c -o test/second.o
    g++ -std=c++11 -g -O3 -fopenmp -o "P1-Test" test/main.o test/second.o 
    echo The program has been compiled
    

    【讨论】:

    • 添加了一个替代版本,在构建之间保留 .o 文件。
    猜你喜欢
    • 2018-04-05
    • 1970-01-01
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多