I’m trying to enable a C-Macro only when the corresponding source file is used by a specific cmake target. Assume I have the following setup:
tests/test.cpp
src/code.cpp
include/code.hpp
CMakeList.txt
code.hpp
class MyClass
{
public:
void normal_stuff();
#ifdef TEST
int debug;
void _dangerous_function()
{
debug++;
}
#endif
}
code.cpp
#include "code.hpp"
MyClass::normal_stuff()
{
// boring code
}
test.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})
I want to set TEST only when code.cpp is build for the test target, but not for the foo target. Of course I could just write “#define TEST” in test.cpp before I include code.hpp, but then code.cpp will have a different view of MyClass than test.cpp.
Has anyone an idea how I could do that? I know, I shouldn’t be doing this, but I would like to know, if I could get it running at all.
you may set a target specific property to add such definition:
read more here and here