【发布时间】:2013-06-05 08:17:56
【问题描述】:
我可能有一个愚蠢的问题,但没有问题是愚蠢的,我会问它...假设我有文件matrix.hpp 和matrix.cpp。在这些文件中,我使用assert(...) 来确保遵守某些条件。我编译这个文件并得到一个matrix.o 文件。现在我将在许多不同的程序中使用这个matrix.o 文件,其中一些只是测试,需要检查assert(...) 条件,其他是不需要这些检查的工作程序。
我的问题是:我可以在没有-DNDEBUG 标志的情况下编译matrix.o,因此通常会检查assert(...) 条件。但是当我为不需要检查的程序链接 .o 文件时,我添加了这个标志而不重新编译 matrix.o 文件。
更准确地说,这会做我想要的吗:
# the test program with the "assert(..)" checks
test:test.o matrix.o
gcc -o $@ $^
test.o:test.cpp matrix.hpp
gcc -c $^
# the real program without the "assert(..)" checks
prog:prog.o matrix.o
gcc -o $@ $^ -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。如果每次我在我添加的矩阵文件中使用“assert(...)”怎么办:
#ifdef CHECK
assert(...)
#endif
现在当我编译“测试”程序时,我使用 CHECK 标志,但不使用“prog”程序?我想这也行不通……
【问题讨论】:
标签: c++ compilation makefile g++ flags