我有这个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)
Run Code Online (Sandbox Code Playgroud)
使用:调用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!
Run Code Online (Sandbox Code Playgroud)
然而,gcc
编译器在C:/MinGW/bin/
,它的工作原理.
任何的想法?
平台:
DLR*_*ave 68
切勿尝试在CMakeLists.txt
文件中设置编译器.
有关如何使用其他编译器的信息,请参阅CMake FAQ:
https://gitlab.kitware.com/cmake/community/wikis/FAQ#how-do-i-use-a-different-compiler
(请注意,您正在尝试方法#3,常见问题解答说"(避免)"......)
我们建议避免使用"在CMakeLists中"技术,因为当第一次配置使用不同的编译器时会出现问题,然后CMakeLists文件会更改以尝试设置不同的编译器...并且因为CMakeLists文件的意图根据运行CMake的开发人员的偏好,应该与多个编译器一起工作.
最好的方法是设置环境变量CC
和CXX
调用CMake的在一个构造树的第一次之前.
在CMake检测到要使用的编译器之后,它会将它们保存在CMakeCache.txt
文件中,这样即使这些变量从环境中消失,它仍然可以生成正确的构建系统......
如果您需要更改编译器,则需要从新的构建树开始.
小智 6
我和 Pietro 有类似的问题,
我在 Window 10 上并使用“Git Bash”。我试图执行 >>cmake -G "MinGW Makefiles",但我得到了与 Pietro 相同的错误。
然后,我尝试了 >>cmake -G "MSYS Makefiles",但意识到我需要正确设置我的环境。
确保将路径设置为 C:\MinGW\msys\1.0\bin 并检查那里是否有 gcc.exe。如果 gcc.exe 不存在,那么您必须运行 C:/MinGW/bin/mingw-get.exe 并从 MSYS 安装 gcc。
之后它对我来说很好用