2

What is wrong with this:

if(CMAKE_COMPILER_IS_GNUCXX OR MINGW OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
    add_compile_options("$<$<CONFIG:RELEASE>:-W -Wall -O3 -pedantic>")
    add_compile_options("$<$<CONFIG:DEBUG>:-W -Wall -O0 -g -pedantic>")
endif()

I get:

g++: error: unrecognized command line option ‘-W -Wall -O3 -pedantic’

If I put it like this it works:

if(CMAKE_COMPILER_IS_GNUCXX OR MINGW OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
    add_compile_options("$<$<CONFIG:RELEASE>:-W>")
    add_compile_options("$<$<CONFIG:RELEASE>:-Wall>")        
    add_compile_options("$<$<CONFIG:RELEASE>:-O3>")
    add_compile_options("$<$<CONFIG:RELEASE>:-pedantic>")
    add_compile_options("$<$<CONFIG:DEBUG>:-W>")
    add_compile_options("$<$<CONFIG:DEBUG>:-Wall>")
    add_compile_options("$<$<CONFIG:DEBUG>:-g>")
    add_compile_options("$<$<CONFIG:DEBUG>:-O0>")
    add_compile_options("$<$<CONFIG:DEBUG>:-pedantic>")
endif()

...but I guess that's not intended to be like that...

juzzlin
  • 45,029
  • 5
  • 38
  • 50

2 Answers2

3

I've given your example a try and could reproduce your problem:

c++: error: unrecognized command line option ‘-W -Wall -O3 -pedantic’

The add_compile_options() command takes the parameters as a list of options, so you have to use ; as delimiter.

The following did work in my gcc/make environment:

add_compile_options("$<$<CONFIG:RELEASE>:-W;-Wall;-O3;-pedantic>")
add_compile_options("$<$<CONFIG:DEBUG>:-W;-Wall;-O0;-g;-pedantic>")

Or alternatively written as:

string(
    APPEND _opts
        "$<$<CONFIG:RELEASE>:-W;-Wall;-O3;-pedantic>"
        "$<$<CONFIG:DEBUG>:-W;-Wall;-O0;-g;-pedantic>"
)
add_compile_options("${_opts}")

References

Florian
  • 39,996
  • 9
  • 133
  • 149
3

Unlike to CMAKE_CXX_FLAGS variable, which contains single string with options, separated by space, command add_compile_options accepts separated arguments for every option. That is, without generator expressions correct usage would be:

add_compile_options("-W" "-Wall" "-O3" "-pedantic")

So if you want to use generator exressions, you need to append it to every option, as you do in your second attempt.

If you have many per-configuration options and you find manual repeating of generator expression for every of them tedios, you may create a simple function, which does that for you. Like this one:

# add_compile_options_config(<CONFIG> <option> ...)
function(add_compile_options_config CONFIG)
    foreach(opt ${ARGN})
        add_compile_options("$<$<CONFIG:${CONFIG}>:${opt}>")
    endforeach()
endfunction()

with usage:

add_compile_options_config(RELEASE "-W" "-Wall" "-O3" "-pedantic")
add_compile_options_config(DEBUG "-W" "-Wall" "-O0" "-g" "-pedantic")

If you want to add compiler options like "-W", "-O3" to all targets in your project but in configurations-specific manner, consider to use CMAKE_CXX_FLAGS_<CONFIG> variables:

string(APPEND CMAKE_CXX_FLAGS_RELEASE " -W -Wall -03 -pedantic")
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153