在根CMakeLists.txt中我有
add_definition(-DMY_MACRO)
然后在子CMakeLists.txt中,我想做类似
的事情add_library(mylib, STATIC
file1
#ifdef MY_MACRO
file2
#endif
)
我试过像
这样的东西IF( DEFINED MY_MACRO )
SET(FILE2 "file2")
ELSE( DEFINED MY_MACRO )
SET(FILE2 "")
END_IF( DEFINED MY_MACRO )
add_library(mylib static
file1
${FILE2}
)
但没有快乐。
答案 0 :(得分:5)
add_definitions添加预处理器定义,它不定义cmake变量。
我还没有测试过,但是如果你真的想要寻找预处理器定义,可能会更像这样:
set(mylib_SOURCES )
list(APPEND mylib_SOURCES file1)
get_directory_property(CURRENT_DEFINITIONS COMPILE_DEFINITIONS)
list(FIND CURRENT_DEFINITIONS "MY_MACRO" IN_CURRENT_DEFINITIONS)
if(NOT IN_CURRENT_DEFINITIONS EQUAL -1)
list(APPEND mylib_SOURCES file2)
endif(NOT IN_CURRENT_DEFINITIONS EQUAL -1)
add_library(mylib static ${mylib_SOURCES})
(定义继承自父目录)
在您的父母CMakeLists.txt中:
add_definitions(-DMY_MACRO) # should appear before going into subdirectories
add_subdirectory(foo)
基于变量的不同方法是:
父:
option(WITH_FOO "with(out) foo option") # or set(WITH_FOO true/false)
if(WITH_FOO)
add_definitions(-DMY_MACRO)
endif(WITH_FOO)
add_subdirectory(foo)
子目录:
set(mylib_SOURCES )
list(APPEND mylib_SOURCES file1)
if(WITH_FOO)
list(APPEND mylib_SOURCES file2)
endif(WITH_FOO)
add_library(mylib static ${mylib_SOURCES})