Improve behavior when user changes CMAKE_CXX_COMPILER
cmake 3.13.4
CMAKE_CXX_FLAGS are ignored when reconfiguring with a different CMAKE_CXX_COMPILER
In the following output you can see I start using clang++ with -O1. I moved to -O2, keeping clang++, and everything is OK.
Then I move to g++, going back to -O1, and DCMAKE_CXX_FLAGS becomes empty!!!
Rerunning the same command now DCMAKE_CXX_FLAGS is actually -O1 as it should.
$ echo 'cmake_minimum_required(VERSION 3.12)' > CMakeLists.txt
$ cmake -DCMAKE_CXX_COMPILER:STRING=clang++ -DCMAKE_CXX_FLAGS:STRING="-O1" .
-- The C compiler identification is GNU 8.2.1
-- The CXX compiler identification is Clang 7.0.1
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/clang++
-- Check for working CXX compiler: /usr/bin/clang++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: XXXXX
$ grep CMAKE_CXX_FLAGS:STRING CMakeCache.txt
CMAKE_CXX_FLAGS:STRING=-O1
$ cmake -DCMAKE_CXX_COMPILER:STRING=clang++ -DCMAKE_CXX_FLAGS:STRING="-O2" .
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:
CMAKE_CXX_FLAGS
-- Build files have been written to: XXXXX
$ grep CMAKE_CXX_FLAGS:STRING CMakeCache.txt
CMAKE_CXX_FLAGS:STRING=-O2
$ cmake -DCMAKE_CXX_COMPILER:STRING=g++ -DCMAKE_CXX_FLAGS:STRING="-O1" .
-- Configuring done
You have changed variables that require your cache to be deleted.
Configure will be re-run and you may have to reset some variables.
The following variables have changed:
CMAKE_CXX_COMPILER= g++
-- The C compiler identification is GNU 8.2.1
-- The CXX compiler identification is GNU 8.2.1
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/g++
-- Check for working CXX compiler: /usr/bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: XXXXX
$ grep CMAKE_CXX_FLAGS:STRING CMakeCache.txt
CMAKE_CXX_FLAGS:STRING=
$ cmake -DCMAKE_CXX_COMPILER:STRING=g++ -DCMAKE_CXX_FLAGS:STRING="-O1" .
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:
CMAKE_CXX_FLAGS
-- Build files have been written to: XXXXX
$ grep CMAKE_CXX_FLAGS:STRING CMakeCache.txt
CMAKE_CXX_FLAGS:STRING=-O1
$
Edited by Brad King