仅当特定的 cmake 目标使用相应的源文件时,我才尝试启用 C 宏。假设我有以下设置:
tests/test.cpp
src/code.cpp
include/code.hpp
CMakeList.txt
代码.hpp
class MyClass
{
public:
void normal_stuff();
#ifdef TEST
int debug;
void _dangerous_function()
{
debug++;
}
#endif
}
代码.cpp
#include "code.hpp"
MyClass::normal_stuff()
{
// boring code
}
测试.cpp
#include "code.hpp"
void some_test()
{
MyClass foo;
foo._dangerous_function();
}
CMakeList.txt
project(foo)
include_directories(include)
file(GLOB_RECURSE foo_source src/*.cpp)
file(GLOB_RECURSE test_source test/*.cpp)
add_executeable(foo ${foo_source})
add_executeable(test ${test_source} ${foo_source})
我只想在为测试目标构建code.cpp而不是为 foo 目标构建时设置 TEST 。当然,在我包含code.hpp之前,我可以在test.cpp中写“#define TEST” ,但是code.cpp对 MyClass 的看法与test.cpp不同。
有谁知道我该怎么做?我知道,我不应该这样做,但我想知道,如果我能让它运行起来。