为什么 CMake 拒绝使用非默认编译器?

2023-12-28

我想检查我的 C++ 项目是否可以在旧版本的 GCC 上编译。因此,我安装了旧版本并希望 CMake 使用它来编译我的项目。

CMake 常见问题解答关于更改编译器 http://www.cmake.org/Wiki/CMake_FAQ#How_do_I_use_a_different_compiler.3F告诉我这是正确的方法:


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

这就是我输入的内容,CMake 似乎运行良好:


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  

但是,现在查看 CMakeCache.txt 我发现:


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

显然CMake没有使用我指定的编译器。当我更改此行以使用 g++-4.4 并再次运行 CMake 时,它​​将创建一个无限循环:


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...  

为什么 CMake 不使用我指定的编译器以及如何解决此问题?


从您提供的链接:

此方法不保证适用于所有生成器

使用第二种方法:

cmake -G "你的生成器" -D CMAKE_C_COMPILER=gcc-4.4 -D CMAKE_CXX_COMPILER=g++-4.4 路径/到/你的/源

如果没有帮助:

  1. 使用绝对路径
  2. 不要忘记全部清理在构建目录中
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 CMake 拒绝使用非默认编译器? 的相关文章

随机推荐