根据set_directory_properties 文档,目录上设置的属性应该传播到子目录:
\n\n\n\n\n设置当前目录和子目录的属性。
\n
根据支持的属性文档,COMPILE_DEFINITIONS 是目录支持的属性。
\n\n鉴于此,为什么目录的 COMPILE_DEFINITIONS 不传播到以下示例中的子目录?
\n\n- CMakeLists.txt\n- sub\n -CMakeLists.txt\n -main.cpp\nRun Code Online (Sandbox Code Playgroud)\n\n根 CMakeLists.txt:
\n\ncmake_minimum_required(VERSION 2.8.11)\nproject(cmake_sandbox)\nadd_subdirectory(sub)\nset_directory_properties(PROPERTIES COMPILE_DEFINITIONS SHOW_MESSAGE=1)\nRun Code Online (Sandbox Code Playgroud)\n\n子 CMakeLists.txt:
\n\nadd_executable(hello main.cpp)\nRun Code Online (Sandbox Code Playgroud)\n\n主要.cpp:
\n\n#include <iostream>\nusing namespace std;\n\nint main()\n{\n #define A_LOCAL_MESSAGE\n #ifdef A_LOCAL_MESSAGE\n #pragma message("A local message!")\n #else\n #pragma message("No local message!")\n #endif\n\n #ifdef SHOW_MESSAGE\n #pragma message("A message!")\n #else\n #pragma message("No message!")\n #endif\n cout << "Hello, World!\\n";\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n$ cmake --version\ncmake version 3.10.2\n\nCMake suite maintained and supported by Kitware (kitware.com/cmake).\n$ rm -rf build && mkdir build && cd build && cmake .. && make -j && sub/hello\n-- The C compiler identification is GNU 7.4.0\n-- The CXX compiler identification is GNU 7.4.0\n-- Check for working C compiler: /usr/bin/cc\n-- Check for working C compiler: /usr/bin/cc -- works\n-- Detecting C compiler ABI info\n-- Detecting C compiler ABI info - done\n-- Detecting C compile features\n-- Detecting C compile features - done\n-- Check for working CXX compiler: /usr/bin/c++\n-- Check for working CXX compiler: /usr/bin/c++ -- works\n-- Detecting CXX compiler ABI info\n-- Detecting CXX compiler ABI info - done\n-- Detecting CXX compile features\n-- Detecting CXX compile features - done\n-- Configuring done\n-- Generating done\n-- Build files have been written to: /home/caleb/src/cmake-sandbox/build\nScanning dependencies of target hello\n[ 50%] Building CXX object sub/CMakeFiles/hello.dir/main.cpp.o\n/home/caleb/src/cmake-sandbox/sub/main.cpp: In function \xe2\x80\x98int main()\xe2\x80\x99:\n/home/caleb/src/cmake-sandbox/sub/main.cpp:8:36: note: #pragma message: A local message!\n #pragma message("A local message!")\n ^\n/home/caleb/src/cmake-sandbox/sub/main.cpp:16:31: note: #pragma message: No message!\n #pragma message("No message!")\n ^\n[100%] Linking CXX executable hello\n[100%] Built target hello\nHello, World!\nRun Code Online (Sandbox Code Playgroud)\n\n如果在根级别设置的 COMPILE_DEFINITION 已按预期传播,则第二个编译指示输出将更改为“一条消息!” 案件。为什么这没有发生?
\nadd_subdirectory使 CMake 进入子目录并处理其中的 CMakeLists.txt 文件,然后再处理后面的指令add_subdirectory。
如果您希望采用这些设置,则需要在递归到子目录之前设置它们。
set_directory_properties(PROPERTIES COMPILE_DEFINITIONS SHOW_MESSAGE=1)
add_subdirectory(sub)
Run Code Online (Sandbox Code Playgroud)