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.
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...
I have the same problem and then I look up the offical manual in target_compile_options. And in the bottom I see the sentence in see Also:
This command can be used to add any options. However, for adding preprocessor definitions and include directories it is recommended to use the more specific commands target_compile_definitions() and target_include_directories().
So, I think it is just a statment:
target_compile_definitions(), but you use target_compile_options is also not error.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