Issue
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
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...
Answered By - fabian Answer Checked By - David Goodson (WPSolving Volunteer)