将条件添加到CMake add_definitions()命令

时间:2017-10-02 10:58:39

标签: cmake

我的CMake文件有两个目标。运行 cmake .. 之后,我可以说 make rsutest 进行单元测试并构建测试可执行文件或者说 make rsu 以获得正常可执行文件。如果我正在构建测试目标,我想使用 add_definitions(-DRSU_TEST)命令为测试代码添加一个定义,以便可以忽略实际代码中的某些行。如何编写我的CMake文件,以便 #define RSU_TEST 行仅在构建测试目标时才有效?

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)

1 个答案:

答案 0 :(得分:1)

option(WITH_TEST "BUILD THE TEST CODE" OFF)

if(WITH_TEST)

add_definitions(-DRSU_TEST)

endif()

然后,如果我想运行测试代码,我使用 cmake .. -DWITH_TEST = ON 而不仅仅是cmake ...并通过 cmake将其关闭.. -DWITH_TEST = OFF 当我想要正常构建时。