'CMake : target_compile_options() vs target_compile_definitions()
What is the different between target_compile_options() vs target_compile_definitions()?
As per CMake docs:
target_compile_options - Adds options to the COMPILE_OPTIONS or INTERFACE_COMPILE_OPTIONS target properties.
target_compile_definitions - The INTERFACE, PUBLIC and PRIVATE keywords are required to specify the scope of the following arguments. PRIVATE and PUBLIC items will populate the COMPILE_DEFINITIONS property of <target>. PUBLIC and INTERFACE items will populate the INTERFACE_COMPILE_DEFINITIONS property of <target>.
But I am not getting which one to use and when.
Solution 1:[1]
Use target_compile_definitions for definitions of preprocessor macros, use target_compile_options for other flags.
For target_compile_definitions cmake is able to choose the appropriate compiler flags based on the compiler used. Furthermore you save yourself the -D:
Example
target_compile_definitions(MyTarget PRIVATE
ADD_DEBUG_PRINTS=1
ABCDE
)
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_options(MyTarget PRIVATE -Wall)
endif()
Note that the use of -Wall usually shouldn't be added in this place; it's just used as an example of a well known compiler flag...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | fabian |
