我对此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,它将失败,并显示以下输出:
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,而FAQ则显示“(避免)” ...)

    我们建议避免使用“in CMakeLists”技术,因为在使用其他编译器进行首次配置时会出现问题,然后更改CMakeLists文件以尝试设置其他编译器...并且因为CMakeLists文件的意图应该根据运行CMake的开发人员的喜好与多个编译器一起使用。

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

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

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

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

    10-11 23:09
    查看更多