无法使用 CMake 指定编译器 [英] Unable to specify the compiler with CMake

查看:80
本文介绍了无法使用 CMake 指定编译器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个 CMakeLists.txt 文件有问题:

cmake_minimum_required(VERSION 2.6)

SET(CMAKE_C_COMPILER C:/MinGW/bin/gcc)
SET(CMAKE_CXX_COMPILER C:/MinGW/bin/g++)

project(cmake_test)

add_executable(a.exe test.cpp)

使用: cmake -G "MinGW Makefiles" 调用 cmake,它失败并显示以下输出:

Calling cmake with: cmake -G "MinGW Makefiles" , it fails with the following output:

c:Userspietro.meleprojects	estsuildSystem_testcmake_test>cmake -G "MinGW Makefiles" .
-- The C compiler identification is GNU 4.6.1
-- The CXX compiler identification is GNU 4.6.1
-- Check for working C compiler: C:/MinGW/bin/gcc
CMake Error: your C compiler: "C:/MinGW/bin/gcc" was not found.   Please set CMAKE_C_COMPILER to a valid compiler path or name.
CMake Error: Internal CMake error, TryCompile configure of cmake failed
-- Check for working C compiler: C:/MinGW/bin/gcc -- broken
CMake Error at C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:52 (MESSAGE):
  The C compiler "C:/MinGW/bin/gcc" is not able to compile a simple test
  program.

CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:10 (project)

CMake Error: your C compiler: "C:/MinGW/bin/gcc" was not found.   Please set CMAKE_C_COMPILER to a valid compiler path or name.
CMake Error: your CXX compiler: "C:/MinGW/bin/g++" was not found.   Please set CMAKE_CXX_COMPILER to a valid compiler path or name.
-- Configuring incomplete, errors occurred!

然而,gcc 编译器在 C:/MinGW/bin/ 中并且可以工作.

However the gcc compiler is in C:/MinGW/bin/ and it works.

有什么想法吗?

平台:

  • Windows 7
  • MinGW/GCC 4.6

推荐答案

切勿尝试在 CMakeLists.txt 文件中设置编译器.

Never try to set the compiler in the CMakeLists.txt file.

请参阅有关如何使用不同编译器的 CMake 常见问题解答:

See the CMake FAQ about how to use a different compiler:

https://gitlab.kitware.com/cmake/community/wikis/FAQ#how-do-i-use-a-different-compiler

(请注意,您正在尝试方法#3,并且常见问题解答说(避免)"...)

(Note that you are attempting method #3 and the FAQ says "(avoid)"...)

我们建议避免使用在 CMakeLists 中"技术,因为当第一次配置使用不同的编译器时它会出现问题,然后 CMakeLists 文件更改以尝试设置不同的编译器......并且因为根据运行 CMake 的开发人员的偏好,一个 CMakeLists 文件应该可以与多个编译器一起使用.

We recommend avoiding the "in the CMakeLists" technique because there are problems with it when a different compiler was used for a first configure, and then the CMakeLists file changes to try setting a different compiler... And because the intent of a CMakeLists file should be to work with multiple compilers, according to the preference of the developer running CMake.

最好的方法是在构建树中第一次调用 CMake 之前设置环境变量 CCCXX.

The best method is to set the environment variables CC and CXX before calling CMake for the very first time in a build tree.

在 CMake 检测到要使用的编译器后,它会将它们保存在 CMakeCache.txt 文件中,这样即使这些变量从环境中消失,它仍然可以生成正确的构建系统......

After CMake detects what compilers to use, it saves them in the CMakeCache.txt file so that it can still generate proper build systems even if those variables disappear from the environment...

如果您需要更改编译器,则需要从新的构建树开始.

If you ever need to change compilers, you need to start with a fresh build tree.

这篇关于无法使用 CMake 指定编译器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆