CMakeLists.txt 如何包含条件预处理器

CMakeLists.txt how to include conditional preprocessor

在 cmake 中,你如何做一个条件预处理器 #ifdef #endif

例如下面的源文件中有一个 #ifdef #endif,我想在编译过程中包含这个文件?

你会怎么做?

#ifdef LWM2M_WITH_LOGS
#include <inttypes.h>
#define LOG(STR) lwm2m_printf("[%s:%d] " STR "\r\n", __func__ , __LINE__)
#endif

完整的源文件位于: Source file on github

CMakeLists.txt 文件位于: CMakeLists.txt

您可以在CMakeLists.txt中添加选项:

option (WITH_LOGS "Use reach logging." OFF)
if (WITH_LOGS)
    # Do some related work (if you need),
    # ...
    # and add definition for compiler 
    target_compile_definitions (lwm2mclient PRIVATE -DLWM2M_WITH_LOGS)
endif ()

并配置您的项目:

cmake -DWITH_LOGS=ON {path-to-CMakeLists.txt}