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

标签 c cmake 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}

关于CMakeLists.txt 如何包含条件预处理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46886308/

相关文章:

c - 使用交换函数交换二维数组指针中的地址

windows - 如何在 Win7 -64 位中安装 openCV

plugins - 如何注册 clang 预处理器插件?

c++ - 正确的包含顺序是什么

c - 如何写入结构体数组的第二个元素?在C中

c - 为什么 gcc 会创建冗余的汇编代码?

c - 在 C 语言中删除应用程序中的信息

Cmake-gui 没有出现在 Windows 上

C++ cmake & boost & arm 交叉编译

cmake - 带有 cmake add_definitions 的文件路径