CMake - 如何为目标可执行文件设置多个编译定义?

标签 cmake

我正在尝试为我尝试在 CMake 中编译的可执行文件之一设置多个编译定义(以激活用于调试的宏)。这是我尝试过的:

add_executable (trie_io_test trie_io_test.c trie.c word_list.c)
    set_target_properties(
            trie_io_test
            PROPERTIES
            COMPILE_DEFINITIONS UNIT_TESTING=1)
    set_target_properties(
            trie_io_test
            PROPERTIES
            COMPILE_DEFINITIONS IO_TEST=1)

不幸的是,这只会导致 IO_TEST 被定义。

我还尝试了以下方法:
add_executable (trie_io_test trie_io_test.c trie.c word_list.c)
    set_target_properties(
            trie_io_test
            PROPERTIES
            COMPILE_DEFINITIONS UNIT_TESTING=1 IO_TEST=1)

但是,另一方面,这会导致 CMake 错误。

如何为我尝试构建的可执行文件设置这两个定义?

最佳答案

您要 target_compile_definitions 而不是 set_target_properties :

target_compile_definitions(trie_io_test PRIVATE UNIT_TESTING=1 IO_TEST=1)

关于CMake - 如何为目标可执行文件设置多个编译定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30546677/

相关文章:

c++ - boost: vector256.hpp: 没有这样的文件或目录

c++ - 我可以从多个目标创建一个库吗?

linker - 如何使用cmake查找库?

c++ - cmake mingw-w64 : strange error when trying to build

c++ - 每个带有 cmake 的 objective-c uda 编译器选项

c++ - LINK1104 的 Opencv 构建错误

c++ - 使 Qt 翻译保持最新

c++ - Python.h : No such file or directory

c++ - 无法使用 Visual Studio 和 CMake 将 __cplusplus 设置为 C++17 标准

linux - cmake 找不到 sudo