c++ - 带有多个标志的 target_compile_definitions

标签 c++ gcc cmake flatbuffers

我正在尝试更改 flatbuffers library CMakeLists.txt 使一些标志成为 PUBLIC。然后,我重写了这些行:

原始代码

elseif(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -Wall -pedantic -Werror -Wextra") 
endif()

新代码

elseif(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
    target_compile_definitions(flatbuffers PUBLIC "-std=c++0x -Wall -pedantic -Werror -Wextra")
endif()

目标名称是 flatc ,当我开始构建项目时,我收到:

Scanning dependencies of target flatc
[  7%] Building CXX object CMakeFiles/flatc.dir/src/idl_parser.cpp.obj
<command-line>:0:1: error: macro names must be identifiers

使用这些标志将“set(CMAKE_CXX_FLAGS)”转换为“target_compile_definitions()”是否错误?

最佳答案

不同之处在于,旧变量 CMAKE_CXX_FLAGS 是单个空格分隔的字符串,具有由此带来的所有问题(转义、操作等)。

较新的属性(例如由 target_compile_definitions() 设置的属性)改为在 CMake 列表上工作,每个列表元素一个选项。因此,只需删除引号即可。而且,由于您实际上不是在传递定义(宏),而是传递选项,因此请改用 target_compile_options():

elseif(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
    target_compile_options(flatbuffers PUBLIC -std=c++0x -Wall -pedantic -Werror -Wextra)
endif()

关于c++ - 带有多个标志的 target_compile_definitions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25845294/

相关文章:

c++ - x86 - 为什么编译器会在下一条指令中插入看似无意义的 JMP?

c++ - C++ 中的自动缩放数据类型?

c++ - GCC:在 "Preinitialization"时间执行代码

linux - 使用基因加权遗传算法自动选择 GCC 优化选项

c++ - 如何使用 CMake 在 android 构建上编译和共享两个 c++ 库

cmake - 如何处理 C++ 库中的资源

c# - 将结构数组从 C++ 转换为 C# 时出现问题

c++ - 选择声明模板友元的语法背后的基本原理是什么?

c++ - 尝试编译 C++ 程序后出错

c++ - CMakeLists.txt :56 (include): include could not find load file: gmxVersionInfo 处的 CMake 错误