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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15051347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:56:55  来源:igfitidea点击:

How do I correctly set a CMakeLists.txt file?

c++windowsgcccmake

提问by Amani

I have a simple C++ test project and wrote my CMakeLists.txt file as follows;

我有一个简单的 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)

When i try to run CMake GUI an set generator to MinGW i get the following error message:

当我尝试运行 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!

I'm on Windows 7 64 bit and i confirmed on cmd that; G++ --version gives G++ (GCC) 4.6.1.

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

What am I doing wrong?

我究竟做错了什么?

回答by Christian Rapp

Sorry but this looks like the compiler is not installed where you specified. Also see herewhy you should avoid setting the compiler in CMakeLists.txt

抱歉,这看起来编译器未安装在您指定的位置。另请参阅此处为什么应避免在 CMakeLists.txt 中设置编译器

So I'd remove those, clear the cmake cache, set the environment variables CC and CXX before calling CMake and try again.

所以我会删除那些,清除 cmake 缓存,在调用 CMake 之前设置环境变量 CC 和 CXX,然后再试一次。

回答by ToniBig

I think your problem lies in the path string. Try this:

我认为您的问题在于路径字符串。尝试这个:

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

Alternatively, you can also set your compilers in the CMake GUI. After you click on generate, choose the option "Specify native compilers" and set or browse to the correct location.

或者,您也可以在 CMake GUI 中设置编译器。单击生成后,选择选项“指定本机编译器”并设置或浏览到正确的位置。