In CMake documentation, we can read:
add_definitions
Adds flags to the compiler command line for sources in the current directory and below.
COMPILE_DEFINITIONS property on directories
COMPILE_DEFINITIONS: Preprocessor definitions for compiling a directory's sources.
COMPILE_DEFINITIONS property on targets
COMPILE_DEFINITIONS: Preprocessor definitions for compiling a target's sources.
COMPILE_DEFINITIONS property on source files
COMPILE_DEFINITIONS: Preprocessor definitions for compiling a source file.
COMPILE_DEFINITIONS
and add_definitions
functionality seems to overlap. COMPILE_DEFINITIONS
property seems more flexible.
So it seems that COMPILE_DEFINITIONS property does everything add_definitions does, and even more.
So, in which cases must we call add_definitions because COMPILE_DEFINITIONS property cannot be used?
Add -D define flags to the compilation of source files. Adds definitions to the compiler command line for targets in the current directory, whether added before or after this command is invoked, and for the ones in sub-directories added after.
Add compile definitions to a target. target_compile_definitions(<target> <INTERFACE|PUBLIC|PRIVATE> [items1...] [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...]) Specify compile definitions to use when compiling a given <target> .
Definition of compile transitive verb. 1 : to compose out of materials from other documents compile a statistical chart. 2 : to collect and edit into a volume compile a book of poems. 3 : to build up gradually compiled a record of four wins and two losses. 4 : to run (something, such as a program) through a compiler.
add_definitions
has existed in CMake since the very first build of CMake came online more than a decade ago.
COMPILE_DEFINITIONS
is simply the newer, more flexible and fine-grained way to do it.
They will always both be around: since 99%+ of the existing CMakeLists.txt
files 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.
So: add_definitions
is primarily useful to configure pre-existing CMakeLists files -- for those projects that have been around since before COMPILE_DEFINITIONS
was 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
.
But if using COMPILE_DEFINITIONS
alone is sufficient for your needs, there's certainly nothing wrong with that.
If you want to add compile definition for target
, you can use this function target_compile_definitions
which is more convenient, like add multiple compile definitions
once, like:
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 executable for more information also from this https://cmake.org/cmake/help/v3.0/command/target_compile_definitions.html.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With