Dogus Ural
Dogus Ural

Reputation: 613

Adding Condition to CMake add_definitions() command

I have two targets for my CMake file. After I run cmake .. , I can either say make rsutest for unit tests and build a test executable or say make rsu to get a normal executable. If I am building my test target I want to add a define to the test code with add_definitions(-DRSU_TEST) command so that some of the lines in the actual code can be ignored . How can I write my CMake file so #define RSU_TEST line is only active when I build the test target ?

add_custom_target(rsutest COMMAND ${CMAKE_CTEST_COMMAND} DEPENDS rsu_test)
add_custom_target(rsu COMMAND ${CMAKE_CTEST_COMMAND} DEPENDS rsu_agent)

if(rsutest)  // this if else statement doesn't work and I need a a condition  
add_definitions(-DRSU_TEST)// which will only be true when I build rsutest
endif(rsutest)

Upvotes: 0

Views: 1339

Answers (1)

Dogus Ural
Dogus Ural

Reputation: 613

option(WITH_TEST "BUILD THE TEST CODE" OFF)

if(WITH_TEST)

add_definitions(-DRSU_TEST)

endif()

Then if I want to run the test code I use cmake .. -DWITH_TEST=ON instead of just cmake ... And turn it off by cmake .. -DWITH_TEST=OFF when I want my normal build.

Upvotes: 1

Related Questions