gcc - 无法使用 CMake 指定编译器

标签 gcc cmake mingw

我有这个问题 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:cmake -G "MinGW Makefiles" ,它失败并显示以下输出:
c:\Users\pietro.mele\projects\tests\buildSystem_test\cmake_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/它有效。

任何的想法?

平台:
  • Windows 7
  • MinGW/GCC 4.6
  • 最佳答案

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

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

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

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

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

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

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

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

    关于gcc - 无法使用 CMake 指定编译器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13054451/

    相关文章:

    c - 尝试运行 C 程序时没有任何效果?

    c++ - 我如何通过 CMake(qt5_wrap_ui) 指定 Qt Designer Form 类的文件名

    c++ - 使用 MinGW 进行 C++ 的 DLL 管理?

    c++ - Windows 中的 Eclipse Ganymede 和 MinGW

    c++ - 无法识别的命令行选项 '-Zc:__cplusplus' 和无法识别的命令行选项 '-permissive-'

    gcc - 对于具有单独管道的 ARM,为 "-mfpu=neon-vfpv3"指定 "-mfpu=neon"比 0x104567910 有优势吗?

    c - C 中的 Posix 正则表达式

    c - 确定 C 宏的扩展

    c++ - 无法在带有 conan 的 cmake 中找到请求的 Boost 库

    使用 cmake 和 pybind11 构建示例应用程序时找不到 Python.h