Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to portably set compiler options like -Wall and -pedantic in CMake

Tags:

c++

cmake

Even if I don't want to tie my CMakeLists file to a specific compiler, I still would like to enable certain options like -Wall that I know many compilers support.

Currently I am doing it like this to set the -Wall and -pedantic flags if they are accepted by the current compiler:

include(CheckCXXCompilerFlag)

check_cxx_compiler_flag(-Wall temp)
if(temp)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
endif()
check_cxx_compiler_flag(-pedantic temp)
if(temp)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic")
endif()

Is there a better way? Or at least a nicer way to do essentially the same thing? The above is incredibly verbose and ugly for what it achieves. A much nicer command would be something like:

enable_cxx_compiler_flags_if_supported(-Wall -pedantic)
like image 302
Quantumboredom Avatar asked Oct 19 '15 19:10

Quantumboredom


People also ask

How to choose the compiler in Modern CMake?

What is the proper way, in modern CMake, to choose the compiler (and to change it, on demand)? Either using a toolchain file, using cmake -DCMAKE_$ {LANG}_COMPILER= on the first configure, or CC=…

How do I add a compiler flag to CMake?

Append Compiler Flags The initial content from the cached CMAKE_CXX_FLAGS variable is a combination of CMAKE_CXX_FLAGS_INIT set by CMake itself during OS/toolchain detection and whatever is set in the CXXFLAGS environment variable. So you can initially call: cmake -E env CXXFLAGS="-Wall" cmake..

Is it possible to change the build options of a compiler?

On current compilers, it is not possible to do this through build options. This is because of how the build model works: The compiler will get invoked once for every source file and all the header files included by that source file will invariably use the same build options as the source file itself. So CMake will not be able to help you here.

What are the linker options in CMake?

Append Linker Flags The linker options are more or less equivalent to the compiler options. Just that CMake's variable names depend on the target type (EXE, SHARED or MODULE).


1 Answers

As suggested in a comment I've tried to write a function myself. I obviously don't know much CMake, but here is my try at a function that checks both that the flag is supported using check_cxx_compiler_flag and also checks that the flag isn't already set (to avoid flooding the list with duplicates).

include(CheckCXXCompilerFlag)

function(enable_cxx_compiler_flag_if_supported flag)
    string(FIND "${CMAKE_CXX_FLAGS}" "${flag}" flag_already_set)
    if(flag_already_set EQUAL -1)
        check_cxx_compiler_flag("${flag}" flag_supported)
        if(flag_supported)
            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" PARENT_SCOPE)
        endif()
        unset(flag_supported CACHE)
    endif()
endfunction()

# example usage
enable_cxx_compiler_flag_if_supported("-Wall")
enable_cxx_compiler_flag_if_supported("-Wextra")
enable_cxx_compiler_flag_if_supported("-pedantic")
like image 154
Quantumboredom Avatar answered Nov 09 '22 07:11

Quantumboredom