C++ CMake:何时使用 add_definitions 而不是 set_target_properties(目标 PROPERTIES COMPILE_DEFINITIONS 定义)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15223779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:11:13  来源:igfitidea点击:

CMake: when to use add_definitions instead of set_target_properties(target PROPERTIES COMPILE_DEFINITIONS definitions)

c++cmake

提问by Korchkidu

In CMake documentation, we can read:

在 CMake 文档中,我们可以阅读:

add_definitions

添加定义

Adds flags to the compiler command line for sources in the current directory and below.

为当前目录及以下目录中的源代码向编译器命令行添加标志。

COMPILE_DEFINITIONS property on directories

目录上的 COMPILE_DEFINITIONS 属性

COMPILE_DEFINITIONS: Preprocessor definitions for compiling a directory's sources.

COMPILE_DEFINITIONS:用于编译目录源的预处理器定义。

COMPILE_DEFINITIONS property on targets

目标上的 COMPILE_DEFINITIONS 属性

COMPILE_DEFINITIONS: Preprocessor definitions for compiling a target's sources.

COMPILE_DEFINITIONS:用于编译目标源的预处理器定义。

COMPILE_DEFINITIONS property on source files

源文件的 COMPILE_DEFINITIONS 属性

COMPILE_DEFINITIONS: Preprocessor definitions for compiling a source file.

COMPILE_DEFINITIONS:用于编译源文件的预处理器定义。

COMPILE_DEFINITIONSand add_definitionsfunctionality seems to overlap. COMPILE_DEFINITIONSproperty seems more flexible.

COMPILE_DEFINITIONSadd_definitions功能似乎重叠。COMPILE_DEFINITIONS属性似乎更灵活。

So it seems that COMPILE_DEFINITIONS property does everything add_definitions does, and even more.

因此,似乎 COMPILE_DEFINITIONS 属性可以完成 add_definitions 所做的一切,甚至更多。

So, in which cases mustwe call add_definitions because COMPILE_DEFINITIONS property cannot be used?

那么,在哪些情况下我们必须调用 add_definitions 因为不能使用 COMPILE_DEFINITIONS 属性?

回答by DLRdave

add_definitionshas existed in CMake since the very first build of CMake came online more than a decade ago.

add_definitions自从十多年前 CMake 的第一个版本上线以来,CMake 就已经存在了。

COMPILE_DEFINITIONSis simply the newer, more flexible and fine-grained way to do it.

COMPILE_DEFINITIONS只是更新,更灵活和细粒度的方式来做到这一点。

They will always both be around: since 99%+ of the existing CMakeLists.txtfiles in the world use add_definitions, it simply would not be wise to remove it. The CMake devs work very hard to maintain backwards compatibility... sometimes to the detriment of clarity and simplicity. And sometimes doing essentially the same thing in multiple differing ways.

它们将永远存在:由于世界上 99% 以上的现有CMakeLists.txt文件都在使用add_definitions,因此删除它是不明智的。CMake 开发人员非常努力地保持向后兼容性……有时会损害清晰度和简单性。有时以多种不同的方式做本质上相同的事情。

So: add_definitionsis primarily useful to configure pre-existing CMakeLists files -- for those projects that have been around since before COMPILE_DEFINITIONSwas introduced. And, since those projects use it, any newer projects that are based on what people learn from reading those CMakeLists files are also quite likely to use add_definitions.

So:add_definitions主要用于配置预先存在的 CMakeLists 文件——对于那些自从COMPILE_DEFINITIONS引入之前就已经存在的项目。而且,由于这些项目使用它,任何基于人们从阅读这些 CMakeLists 文件中学到的知识的新项目也很可能使用add_definitions.

But if using COMPILE_DEFINITIONSalone is sufficient for your needs, there's certainly nothing wrong with that.

但如果COMPILE_DEFINITIONS单独使用就足以满足您的需求,那肯定没有错。

回答by Sergei Nikulov

I'm not say that something mustbe used.

我不是说必须使用某些东西。

It is just matter of your habit.

这只是你的习惯问题。

Some recommendations:

一些建议:

  • Use add_definitionswhen you want add to compiler command line for sources in the current directory and below. It is just shorter to type.

  • Use COMPILE_DEFINITIONSfor fine tunning of target or specific sources.

  • 当您想为当前目录及以下目录中的源添加到编译器命令行时,请使用add_definitions。打字时间更短。

  • 使用COMPILE_DEFINITIONS对目标或特定源进行微调。

回答by Jayhello

If you want to add compile definition for target, you can use this function target_compile_definitionswhich is more convenient, like add multiple compile definitionsonce, like:

如果要添加compile definition for target,可以使用这个target_compile_definitions更方便的功能,比如添加多个compile definitions一次,比如:

add_executable (trie_io_test demo12.cpp)
target_compile_definitions(trie_io_test PRIVATE UNIT_TESTING=1 IO_TEST=1)

You can see this question how to set multiple compile definitions for target executablefor more information also from this https://cmake.org/cmake/help/v3.0/command/target_compile_definitions.html.

您也可以从这个https://cmake.org/cmake/help/v3.0/command/target_compile_definitions.html看到这个问题如何为目标可执行文件设置多个编译定义以获取更多信息。