c++ - 如何正确设置 CMakeLists.txt 文件?

标签 c++ windows gcc cmake

我有一个简单的C++测试项目,我的CMakeLists.txt文件写成如下;

 cmake_minimum_required(VERSION 2.8)

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

 project(simpleTest)
 add_executable(main main.cpp)

当我尝试运行 CMake GUI 并将生成器设置为 MinGW 时,我收到以下错误消息:

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:61 (message):
  The C compiler "C:/MinGW/bin/gcc" is not able to compile a simple test
  program.

  It fails with the following output:



  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:7 (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!

我在 Windows 7 64 位上,我在 cmd 上确认了这一点; G++ --version 给出 G++ (GCC) 4.6.1。

我做错了什么?

最佳答案

抱歉,编译器似乎没有安装在您指定的位置。另见 here为什么你应该避免在 CMakeLists.txt 中设置编译器

所以我会删除它们,清除 cmake 缓存,在调用 CMake 之前设置环境变量 CC 和 CXX,然后重试。

关于c++ - 如何正确设置 CMakeLists.txt 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15051347/

相关文章:

c++ - 将 int** 转换为 "pointer to a two-dimensional array of integers with fixed number of elements per column"

c++ - 错误 : no matching function for call to 'b::b()'

c++ - 命名管道 : ReadFile after ConnectNamedPipe return ERROR_BROKEN_PIPE

c - 通过 mmap 分配的内存没有 munmap 会在进程退出或终端后导致泄漏

c++ - 针对不同的目标架构进行编译和优化

linux - 如何使用相对路径从共享库链接到共享库?

c++ - 赋值运算符实现的解释

c++ - 尝试编译时出现QT undefined reference 错误

windows - 在 git bash 中使用 python json.tool 时出现 `stdin is not a tty` 错误

windows - 为 Web 应用程序创建 Windows 安装程序 (larragon/laravel)