Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reuse target_compile_options from variable for multiple targets (CMake)

Tags:

cmake

I have several build targets and want to set the same set of compile options like this:

set(app_compile_options "-Wall -Wextra -Wshadow -Wnon-virtual-dtor \
    -Wold-style-cast \
    -Woverloaded-virtual -Wzero-as-null-pointer-constant \
    -pedantic -fPIE -fstack-protector-all -fno-rtti")

add_executable(foo foo.cpp)
target_compile_options(foo PUBLIC ${app_compile_options})

add_executable(bar bar.cpp)
target_compile_options(bar PUBLIC ${app_compile_options})

When compiling I get the following error:

error: unrecognized command line option ‘-Wall -Wextra -Wshadow -Wnon-virtual-dtor -Wold-style-cast     -Woverloaded-virtual -Wzero-as-null-pointer-constant     -pedantic -fPIE -fstack-protector-all -fno-rtti’

Do I need another format or special syntax to define the compile options in a variable?

like image 965
aggsol Avatar asked Nov 24 '25 04:11

aggsol


2 Answers

In addition of @squaresstkittles's answer, I'd add that you can use interface targets for this purpose:

add_library(common INTERFACE)

target_compile_options(common INTERFACE
    -Wall -Wextra -Wshadow -Wnon-virtual-dtor
    -Wold-style-cast
    -Woverloaded-virtual -Wzero-as-null-pointer-constant
    -pedantic -fPIE -fstack-protector-all -fno-rtti
)

target_link_libraries(foo PRIVATE common)
target_link_libraries(bar PRIVATE common)

Even better, you can define those flags in CMakePresets.json without affecting your cmake files. This would also allow different flags for different environment (dev, ci, etc.)

like image 83
Guillaume Racicot Avatar answered Nov 25 '25 20:11

Guillaume Racicot


You are passing the options as a single string by using the quotations. Try removing the quotations (and the \ line continuation markers) to pass the compile options as a list instead:

set(app_compile_options -Wall -Wextra -Wshadow -Wnon-virtual-dtor
    -Wold-style-cast
    -Woverloaded-virtual -Wzero-as-null-pointer-constant
    -pedantic -fPIE -fstack-protector-all -fno-rtti
)
like image 45
Kevin Avatar answered Nov 25 '25 21:11

Kevin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!