我可能有一個愚蠢的問題,但沒有問題是愚蠢的我會問它...讓我們想象我有文件matrix.hpp
和matrix.cpp
。在這些文件中,我使用assert(...)
來確保某些條件得到尊重。我編譯這個文件並得到一個matrix.o
文件。現在,我將在許多不同的程序中使用這個matrix.o
文件,其中一些只是測試,需要檢查assert(...)
條件,而其他人正在運行的程序不需要這些檢查。C++鏈接和編譯標誌
我的問題是:我可以編譯matrix.o
沒有-DNDEBUG
標誌,因此通常情況下,assert(...)
條件將被檢查。但是當我鏈接不需要檢查的程序的.o文件時,我添加了這個標誌而沒有重新編譯matrix.o
文件。
更確切地說,這會做我想做:
# the test program with the "assert(..)" checks
test:test.o matrix.o
gcc -o [email protected] $^
test.o:test.cpp matrix.hpp
gcc -c $^
# the real program without the "assert(..)" checks
prog:prog.o matrix.o
gcc -o [email protected] $^ -DNDEBUG
prog.o:prog.cpp matrix.hpp
gcc -c -DNDEBUG $^
# the matrix.o that can be either checked or not if the -DNDEBUG flag
# is given when the .o files are linked
matrix.o:matrix.cpp matrix.hpp
gcc -c $^
好的,謝謝您的回答!所以我不能簡單地使用標誌-DNDEBUG。如果有什麼我的每個矩陣中的文件使用「斷言(......)」一次,我補充一下:
#ifdef CHECK
assert(...)
#endif
,現在當我編制了「試驗」方案我使用檢查標記,但不與「編程」程序?我想這也行不通...
我認爲兩個相關的問題是相關的:http://stackoverflow.com/questions/8035394/gnu-make-how-to-make-conditional-cflags 其中引用http://stackoverflow.com/questions/ 5127977/makefile-define-compilation-variables-based-on-target-for/5153406#5153406 –