如何将变量从 CMakeLists.txt 读取到 cpp 文件

how to read variables from CMakeLists.txt to cpp file

本文关键字:读取 cpp 文件 txt CMakeLists 变量      更新时间:2023-10-16
if (WITH_TEST)
    add_subdirectory(test/unitTesting)
endif(WITH_TEST)

如何读取.cpp文件中的with_test变量?

解决方案可能是添加预处理器定义。

在您的 CMakeList 中:

add_definitions(-DWITH_TEST_VALUE ="${WITH_TEST}")

在你的源代码中,你可以做这样的事情:

#ifdef WITH_TEST_VALUE
constexpr auto WITH_TEST = WITH_TEST_VALUE;
#else
 // something 
#endif