Why does CMake refuse to use a non

I want to check whether my C++ project compiles on an older version of GCC. For this reason I have installed the older version and want CMake to use it to compile my project.

The CMake FAQ on changing the compiler tells me that this is the right way to do it:

CC=gcc-4.4 CXX=g++-4.4 cmake -G "Unix Makefiles" ..

So this is what I typed and CMake seems to run fine:

chris@chris-machine:~/projects/myProject/build$ CC=gcc-4.4 CXX=g++-4.4 cmake -G "Unix Makefiles" ..
-- Found PythonInterp: /usr/bin/python (found version "2.7.4") 
-- Looking for include file pthread.h
-- Looking for include file pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Configuring done
-- Generating done
-- Build files have been written to: /home/chris/projects/myProject/build

However, looking at CMakeCache.txt now I find this:

//CXX compiler.
CMAKE_CXX_COMPILER:FILEPATH=/usr/bin/c++

Obviously CMake did not use the compiler I specified. When I change this line to use g++-4.4 and run CMake again it will create an endless loop:

chris@chris-machine:~/projects/myProject/build$ CC=gcc-4.4 CXX=g++-4.4 cmake -G "Unix Makefiles" ..
-- 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= /usr/bin/g++-4.4

-- Found PythonInterp: /usr/bin/python (found version "2.7.4") 
-- Looking for include file pthread.h
-- Looking for include file pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- 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= /usr/bin/g++-4.4

-- Found PythonInterp: /usr/bin/python (found version "2.7.4") 
// and so on...

Why does CMake not use the compiler I specify and how can I fix this?


From link you provide:

This method is not guaranteed to work for all generators

Use the second method:

cmake -G "Your Generator" -D CMAKE_C_COMPILER=gcc-4.4 -D CMAKE_CXX_COMPILER=g++-4.4 path/to/your/source

If it not help:

  • use absolute path
  • do not forget to clean all in build directory

  • I've come across this exact issue as well. Apparently this is a "won't fix" issue in CMake: https://cmake.org/Bug/view.php?id=14841

    One work-around is to remove the CMakeCXXCompiler.cmake file inside the build directory's CMakeFiles directory every time you want to re-generate your build directory.

    The path should be: build_directory/CMakeFiles/CMAKE_VERSION/CMakeCXXCompiler.cmake . On Linux, you can get your CMAKE_VERSION programmatically with cmake --version | cut -d ' ' -f 3 cmake --version | cut -d ' ' -f 3 .

    A neat "fix" would be to simply add the removal of that file into your build script whenever you anticipate you will come across this.

    链接地址: http://www.djcxy.com/p/24462.html

    上一篇: 如何停止CMake追加C编译器标志

    下一篇: 为什么CMake拒绝使用非