我想創建一個 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
uj5u.com熱心網友回復:
我的建議:
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.cppextern 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
選擇
If your .o files are really .PRECIOUS, you might want to build separate copies. Here I split into release/main.o and test/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)
Which gives:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/349113.html
標籤:C C 11 生成文件 openmp gnu-make
上一篇:這是串列初始化還是值初始化?
