В документация set_directory_properties , свойства, установленные в каталоге, должны распространяться на подкаталоги:
Установить свойство для текущего каталога и подкаталогов.
В документации по поддерживаемым свойствам , COMPILE_DEFINITIONS - это свойство, поддерживаемое каталогами.
Учитывая это, почему COMPILE_DEFINITIONS для каталога не распространяются на подкаталоги в следующем примере?
Пример структуры файла проекта
- CMakeLists.txt
- sub
-CMakeLists.txt
-main.cpp
Содержимое файла
Root CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.11)
project(cmake_sandbox)
add_subdirectory(sub)
set_directory_properties(PROPERTIES COMPILE_DEFINITIONS SHOW_MESSAGE=1)
Sub CMakeLists.txt:
add_executable(hello main.cpp)
main.cpp:
#include <iostream>
using namespace std;
int main()
{
#define A_LOCAL_MESSAGE
#ifdef A_LOCAL_MESSAGE
#pragma message("A local message!")
#else
#pragma message("No local message!")
#endif
#ifdef SHOW_MESSAGE
#pragma message("A message!")
#else
#pragma message("No message!")
#endif
cout << "Hello, World!\n";
return 0;
}
Тестирование
$ cmake --version
cmake version 3.10.2
CMake suite maintained and supported by Kitware (kitware.com/cmake).
$ rm -rf build && mkdir build && cd build && cmake .. && make -j && sub/hello
-- The C compiler identification is GNU 7.4.0
-- The CXX compiler identification is GNU 7.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/caleb/src/cmake-sandbox/build
Scanning dependencies of target hello
[ 50%] Building CXX object sub/CMakeFiles/hello.dir/main.cpp.o
/home/caleb/src/cmake-sandbox/sub/main.cpp: In function ‘int main()’:
/home/caleb/src/cmake-sandbox/sub/main.cpp:8:36: note: #pragma message: A local message!
#pragma message("A local message!")
^
/home/caleb/src/cmake-sandbox/sub/main.cpp:16:31: note: #pragma message: No message!
#pragma message("No message!")
^
[100%] Linking CXX executable hello
[100%] Built target hello
Hello, World!
Если бы COMPILE_DEFINITION, установленный на корневом уровне, распространялся как ожидалось, второй вывод прагмы изменился бы на "Сообщение!" дело. Почему этого не происходит?