'overriding default CMAKE C++ compiler flags

I'm trying to set (remove) the default c++ compilers flags by using the following commands in my CMakeLists.txt:

set(CMAKE_CXX_FLAGS, "")

set(CMAKE_CXX_FLAGS_DEBUG, "")

set(CMAKE_CXX_FLAGS_RELEASE, "")

but when I go to build my project with cmake --build <builddir>, I see that there are still flags being passed to my compiler (Visual Studio 2017). I want to remove all default compiler flags and select which flags for the compiler to use. How do I do this?

I tried using the set command to set CMAKE_CXX_FLAGS* to empty strings and tried using cmake -H. -Bbuild -DCMAKE_CXX_FLAGS="", but I still see the following in my command line output:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x86\CL.exe /c /no
logo /W1 /WX- /diagnostics:classic /O2 /Oy- /D "CMAKE_INTDIR=\"Debug\"" /D _MBCS /Gm- /MD /GS /fp:precise /Zc:wchar_t
 /Zc:forScope /Zc:inline /Fo"hello.dir\Debug\\" /Fd"hello.dir\Debug\vc141.pdb" /Gd /TP /analyze- /FC /errorReport:queue main.cpp

Here is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.4)

project(TestProject)

set(CMAKE_CXX_FLAGS "")
set(CMAKE_CXX_FLAGS_DEBUG "")

add_executable(hello main.cpp)

and here is the command I used:

cmake -H. -Bbuild && cmake --build build

I expected to see on the command line:

cl.exe main.cpp

and in my CMakeCache.txt file, no values assigned to CMAKE_CXX_FLAGS* variables, but CMAKE_CXX_FLAGS* continues to have values assigned to them in my CMakeCache.txt file.



Solution 1:[1]

cache the strings in a toolchain file, then manually set the flags you need. this should be set before the call to project(...). here is an example based off the info your provided.

WARNING: not this familiar with windows tooling, so you will need to adjust for your specific requirements

#CMakeLists.txt

cmake_minimum_required(VERSION 3.4)

# before project declaration 
set(CMAKE_TOOLCHAIN_FILE "${CMAKE_CURRENT_SOURCE_DIR}/toolchain.cmake"
    CACHE PATH "toolchain file")

project(TestProject)

add_executable(hello main.cpp)

create a file named toolchain.cmake in your project's root directory, where your CMakeLists.txt is

#toolchain.cmake

set(CMAKE_CXX_COMPILER "/usr/bin/g++")

set(CMAKE_CXX_FLAGS "-Wall"
  CACHE STRING "my cmake cxx flags")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g3 -gdb3 -DDEBUG"
  CACHE STRING "my cmake cxx flags")
set(CMAKE_CXX_FLAGS_RELEASE, "-02 -DNDEBUG"
  CACHE STRING "my release cmake cxx flags)

then build from the projects root directory as normal but setting the appropriate CMAKE_BUILD_TYPE environment variable.

for release builds

$ cmake -S . -B build/release -DCMAKE_BUILD_TYPE=Release`
$ cmake --build build/release

for debug builds

$ cmake -S . -B build/debug -DCMAKE_BUILD_TYPE=Debug
$ cmake --build build/debug

NOTE: this will make CMAKE_CXX_FLAGS_<CONFIG>_INIT variable irrelevant

you can verify the results before and after the changes by looking through the build folder's CMakeCache.txt file, which will hold values such as CMAKE_CXX_FLAGS_RELEASE:STRING=...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tbny