C++ CMakeLists.txt:30 (project) 中的 CMake 错误:找不到 CMAKE_C_COMPILER
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32801638/
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
CMake error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found
提问by Caio Fontes
I'm trying make a Visual Studio solution with CMake to compile the latest version of aseprite and CMake keeps giving me the:
我正在尝试使用 CMake 制作一个 Visual Studio 解决方案来编译最新版本的 aseprite 并且 CMake 不断给我:
No CMAKE_C_COMPILER could be found.
No CMAKE_CXX_COMPILER could be found.
I've already downloaded GCC, and I'm using Visual Studio 2015.
我已经下载了 GCC,我正在使用Visual Studio 2015。
I'm following this tutorial:
我正在关注本教程:
采纳答案by Florian
Those error messages
那些错误信息
CMake Error at ... (project):
No CMAKE_C_COMPILER could be found.
-- Configuring incomplete, errors occurred!
See also ".../CMakeFiles/CMakeOutput.log".
See also ".../CMakeFiles/CMakeError.log".
or
或者
CMake Error: your CXX compiler: "CMAKE_CXX_COMPILER-NOTFOUND" was not found.
Please set CMAKE_CXX_COMPILER to a valid compiler path or name.
...
-- Configuring incomplete, errors occurred!
just mean that CMake was unable to find your C/CXX compiler to compile a simple test program (one of the first things CMake tries while detecting your build environment).
只是意味着 CMake 无法找到您的 C/CXX 编译器来编译一个简单的测试程序(CMake 在检测您的构建环境时首先尝试的事情之一)。
The steps to find your problem are dependent on the build environment you want to generate. The following tutorials are a collection of answers here on Stack Overflow and some of my own experiences with CMake on Microsoft Windows 7/8/10 and Ubuntu 14.04.
查找问题的步骤取决于您要生成的构建环境。以下教程是 Stack Overflow 上的一系列答案以及我自己在 Microsoft Windows 7/8/10 和 Ubuntu 14.04 上使用 CMake 的一些经验。
Preconditions
先决条件
- You have installed the compiler/IDE and it was able to once compile any other program (directly without CMake)
- You e.g. may have the IDE, but may not have installed the compiler or supporting framework itself like described in Problems generating solution for VS 2017 with CMakeor How do I tell CMake to use Clang on Windows?
- You have the latest CMake version
- You have access rights on the drive you want CMake to generate your build environment
You have a clean build directory (because CMake does cache things from the last try) e.g. as sub-directory of your source tree
Windows cmd.exe
> rmdir /s /q VS2015 > mkdir VS2015 > cd VS2015
Bash shell
$ rm -rf MSYS $ mkdir MSYS $ cd MSYS
and make sure your command shell points to your newly created binary output directory.
- 您已经安装了编译器/IDE,它能够一次编译任何其他程序(直接没有 CMake)
- 例如,您可能有 IDE,但可能没有安装编译器或支持框架本身,如使用 CMake 为 VS 2017 生成解决方案的问题或如何告诉 CMake 在 Windows 上使用 Clang?
- 你有最新的CMake 版本
- 您对希望 CMake 生成构建环境的驱动器具有访问权限
您有一个干净的构建目录(因为 CMake 会缓存上次尝试中的内容),例如作为源树的子目录
Windows cmd.exe
> rmdir /s /q VS2015 > mkdir VS2015 > cd VS2015
Bash 外壳
$ rm -rf MSYS $ mkdir MSYS $ cd MSYS
并确保您的命令外壳指向您新创建的二进制输出目录。
General things you can/should try
你可以/应该尝试的一般事情
Is CMake able find and run with any/your default compiler? Run without giving a generator
> cmake .. -- Building for: Visual Studio 14 2015 ...
Perfect if it correctly determined the generator to use - like here
Visual Studio 14 2015
What was it that actually failed?
In the previous build output directory look at
CMakeFiles\CMakeError.log
for any error message that make sense to you or try to open/compile the test project generated atCMakeFiles\[Version]\CompilerIdC
|CompilerIdCXX
directly from the command line (as found in the error log).
CMake 是否能够找到并使用任何/您的默认编译器运行?不给发电机运行
> cmake .. -- Building for: Visual Studio 14 2015 ...
如果正确确定要使用的生成器,那就完美了 - 就像这里
Visual Studio 14 2015
实际上失败的原因是什么?
在之前的构建输出目录中,查看
CMakeFiles\CMakeError.log
对您有意义的任何错误消息,或尝试打开/编译在CMakeFiles\[Version]\CompilerIdC
|生成的测试项目。CompilerIdCXX
直接从命令行(在错误日志中找到)。
CMake can't find Visual Studio
CMake 找不到 Visual Studio
Try to select the correct generator version:
> cmake --help > cmake -G "Visual Studio 14 2015" ..
If that doesn't help, try to set the Visual Studio environment variables first (the path could vary):
> "c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" > cmake ..
or use the
Developer Command Prompt for VS2015
short-cut in your Windows Start Menu underAll Programs
/Visual Studio 2015
/Visual Studio Tools
(thanks at @Antwanefor the hint).
尝试选择正确的生成器版本:
> cmake --help > cmake -G "Visual Studio 14 2015" ..
如果这没有帮助,请尝试先设置 Visual Studio 环境变量(路径可能会有所不同):
> "c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" > cmake ..
或使用/ /
Developer Command Prompt for VS2015
下 Windows 开始菜单中的快捷方式(感谢@Antwane的提示)。All Programs
Visual Studio 2015
Visual Studio Tools
Background: CMake does support all Visual Studio releases and flavors (Express, Community, Professional, Premium, Test, Team, Enterprise, Ultimate, etc.). To determine the location of the compiler it uses a combination of searching the registry (e.g. at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\[Version];InstallDir
), system environment variables and - if none of the others did come up with something - plainly try to call the compiler.
背景:CMake 支持所有 Visual Studio 版本和风格(Express、Community、Professional、Premium、Test、Team、Enterprise、Ultimate 等)。为了确定编译器的位置,它结合了搜索注册表(例如 at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\[Version];InstallDir
)、系统环境变量和 - 如果其他人都没有想出什么 - 简单地尝试调用编译器。
CMake can't find GCC (MinGW/MSys)
CMake 找不到 GCC (MinGW/MSys)
You start the MSys
bash
shell withmsys.bat
and just try to directly callgcc
$ gcc gcc.exe: fatal error: no input files compilation terminated.
Here it did find
gcc
and is complaining that I didn't gave it any parameters to work with.So the following should work:
$ cmake -G "MSYS Makefiles" .. -- The CXX compiler identification is GNU 4.8.1 ... $ make
If GCC was not found call
export PATH=...
to add your compilers path (see How to set PATH environment variable in CMake script?) and try again.If it's still not working, try to set the
CXX
compiler path directly by exporting it (path may vary)$ export CC=/c/MinGW/bin/gcc.exe $ export CXX=/c/MinGW/bin/g++.exe $ cmake -G "MinGW Makefiles" .. -- The CXX compiler identification is GNU 4.8.1 ... $ mingw32-make
For more details see How to specify new GCC path for CMake
Note: When using the "MinGW Makefiles" generator you have to use the
mingw32-make
program distributed with MinGWStill not working? That's weird. Please make sure that the compiler is there and it has executable rights (see also preconditions chapter above).
Otherwise the last resort of CMake is to not try any compiler search itself and set CMake's internal variables directly by
$ cmake -DCMAKE_C_COMPILER=/c/MinGW/bin/gcc.exe -DCMAKE_CXX_COMPILER=/c/MinGW/bin/g++.exe ..
For more details see Cmake doesn't honour -D CMAKE_CXX_COMPILER=g++and Cmake error setting compiler
Alternatively those variables can also be set via
cmake-gui.exe
on Windows. See Cmake cannot find compiler
您启动 MSys
bash
shellmsys.bat
并尝试直接调用gcc
$ gcc gcc.exe: fatal error: no input files compilation terminated.
在这里它确实找到
gcc
并抱怨我没有给它任何参数。所以以下应该工作:
$ cmake -G "MSYS Makefiles" .. -- The CXX compiler identification is GNU 4.8.1 ... $ make
如果未找到 GCC,则调用
export PATH=...
添加编译器路径(请参阅如何在 CMake 脚本中设置 PATH 环境变量?)并重试。如果还是不行,尝试
CXX
直接通过导出设置编译器路径(路径可能会有所不同)$ export CC=/c/MinGW/bin/gcc.exe $ export CXX=/c/MinGW/bin/g++.exe $ cmake -G "MinGW Makefiles" .. -- The CXX compiler identification is GNU 4.8.1 ... $ mingw32-make
有关更多详细信息,请参阅如何为 CMake 指定新的 GCC 路径
注意:使用“MinGW Makefiles”生成器时,您必须使用
mingw32-make
与 MinGW 一起分发的程序还是行不通?这很奇怪。请确保编译器在那里并且它具有可执行权限(另请参见上面的前提条件章节)。
否则 CMake 的最后一招是不要尝试任何编译器搜索本身并直接设置 CMake 的内部变量
$ cmake -DCMAKE_C_COMPILER=/c/MinGW/bin/gcc.exe -DCMAKE_CXX_COMPILER=/c/MinGW/bin/g++.exe ..
有关更多详细信息,请参阅Cmake 不支持 -D CMAKE_CXX_COMPILER=g++和Cmake 错误设置编译器
或者,这些变量也可以
cmake-gui.exe
在 Windows上设置。见Cmake 找不到编译器
Background: Much the same as with Visual Studio. CMake supports all sorts of GCC flavors. It searches the environment variables (CC, CXX, etc.) or simply tries to call the compiler. In addition it will detect any prefixes (when cross-compiling) and tries to add it to all binutils of the GNU compiler toolchain (ar
, ranlib
, strip
, ld
, nm
, objdump
, and objcopy
).
背景:与 Visual Studio 大致相同。CMake 支持各种 GCC 风格。它搜索环境变量(CC、CXX 等)或只是尝试调用编译器。此外,它会检测到任何前缀(当交叉编译),并试图将其添加到GNU编译器工具链的所有的binutils( ,ar
,ranlib
,strip
,ld
,nm
,objdump
和objcopy
)。
回答by Vishnu
For Ubuntu, please install the below things:
对于 Ubuntu,请安装以下内容:
sudo apt-get update && sudo apt-get install build-essential
回答by Wim Vanhenden
This happened to me after I installed Visual Studio 15 2017.
这发生在我安装 Visual Studio 15 2017 之后。
The C++ compiler for Visual Studio 14 2015 was not the problem. It seemed to be a problem with the Windows 10 SDK.
Visual Studio 14 2015 的 C++ 编译器不是问题。似乎是 Windows 10 SDK 的问题。
Adding the Windows 10 SDKs to Visual Studio 14 2015 solved the problem for me.
将 Windows 10 SDK 添加到 Visual Studio 14 2015 为我解决了这个问题。
See attached screenshot.
见附件截图。
回答by Natalie Cottrill
I also experienced this error when working with CMake:
我在使用 CMake 时也遇到了这个错误:
No CMAKE_C_COMPILER could be found.
No CMAKE_CXX_COMPILER could be found.
The 'warning' box in the MSDN library article Visual C++ in Visual Studio 2015gave me the help that I needed.
MSDN 库文章Visual Studio 2015 中的 Visual C++ 中的“警告”框为我提供了所需的帮助。
Visual Studio 2015 doesn't come with C++ installed by default. So, creating a new C++ project will prompt you to download the necessary C++ components.
默认情况下,Visual Studio 2015 未安装 C++。因此,创建新的 C++ 项目将提示您下载必要的 C++ 组件。
回答by purval_patel
This works for me in Ubuntu 17.10(Artful Aardvark):
这在Ubuntu 17.10(Artful Aardvark) 中对我有用:
apt-get update
apt-get install build-essential
回答by Aksh1801
I ran into this issue while building libgit2-0.23.4. For me the problem was that C++ compiler & related packages were not installed with VS2015, therefore "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"file was missing and Cmake wasn't able to find the compiler.
我在构建 libgit2-0.23.4 时遇到了这个问题。对我来说,问题是 VS2015 没有安装 C++ 编译器和相关包,因此“C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat”文件丢失,Cmake 无法找到编译器。
I tried manually creating a C++ project in the Visual Studio 2015 GUI (C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe) and while creating the project, I got a prompt to download the C++ & related packages.
我尝试在 Visual Studio 2015 GUI ( C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe) 中手动创建一个 C++ 项目,在创建项目时,我得到了下载 C++ 的提示& 相关的包。
After downloading required packages, I could see vcvarsall.bat & Cmake was able to find the compiler & executed successfully with following log:
下载所需的包后,我可以看到 vcvarsall.bat & Cmake 能够找到编译器并成功执行,并显示以下日志:
C:\Users\aksmahaj\Documents\MyLab\fritzing\libgit2\build64>cmake ..
-- Building for: Visual Studio 14 2015
-- The C compiler identification is MSVC 19.0.24210.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual
Studio 14.0/VC/bin/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual
Studio 14.0/VC/bin/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE)
-- Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR)
-- zlib was not found; using bundled 3rd-party sources.
-- LIBSSH2 not found. Set CMAKE_PREFIX_PATH if it is installed outside of
the default search path.
-- Looking for futimens
-- Looking for futimens - not found
-- Looking for qsort_r
-- Looking for qsort_r - not found
-- Looking for qsort_s
-- Looking for qsort_s - found
-- Looking for clock_gettime in rt
-- Looking for clock_gettime in rt - not found
-- Found PythonInterp: C:/csvn/Python25/python.exe (found version "2.7.1")
-- Configuring done
-- Generating done
-- Build files have been written to:
C:/Users/aksmahaj/Documents/MyLab/fritzing/libgit2/build64
回答by spenceryue
For me, this problem went away on Windows when I moved my project to a shallower parent directory, i.e. to:
对我来说,当我将项目移动到较浅的父目录时,Windows 上的这个问题就消失了,即:
C:\Users\spenc\Desktop\MyProjectDirectory
C:\Users\spenc\Desktop\MyProjectDirectory
instead of
代替
C:\Users\spenc\Desktop\...\MyProjectDirectory
.
C:\Users\spenc\Desktop\...\MyProjectDirectory
.
I think the source of the problem was that MSBuild has a file path length restrictionto 260 characters. This causes the basic compiler test CMake performs to build a project called CompilerIdCXX.vcxproj
to fail with the error:
我认为问题的根源在于MSBuild 的文件路径长度限制为 260 个字符。这会导致 CMake 执行的基本编译器测试构建一个调用CompilerIdCXX.vcxproj
失败并出现错误的项目:
C1083: Cannot open source file: 'CMakeCXXCompilerId.cpp'
C1083: Cannot open source file: 'CMakeCXXCompilerId.cpp'
because the length of the file's path e.g.
因为文件路径的长度,例如
C:\Users\spenc\Desktop\...\MyProjectDirectory\build\CMakeFiles\...\CMakeCXXCompilerId.cpp
C:\Users\spenc\Desktop\...\MyProjectDirectory\build\CMakeFiles\...\CMakeCXXCompilerId.cpp
exceeds the MAX_PATH restriction.
超出 MAX_PATH 限制。
CMake then concludes there is no CXX compiler.
然后 CMake 得出结论,没有 CXX 编译器。
回答by TSac
I had the same errors with CMake. In my case, I have used the wrong Visual Studio version in the initial CMake dialog where we have to select the Visual Studio compiler.
我对 CMake 有同样的错误。就我而言,我在初始 CMake 对话框中使用了错误的 Visual Studio 版本,我们必须在其中选择 Visual Studio 编译器。
Then I changed it to "Visual Studio 11 2012" and things worked. (I have Visual Studio Ultimate 2012 version on my PC). In general, try to input an older version of Visual Studio version in the initial CMake configuration dialog.
然后我将其更改为“Visual Studio 11 2012”并且一切正常。(我的 PC 上有 Visual Studio Ultimate 2012 版本)。通常,尝试在初始 CMake 配置对话框中输入旧版本的 Visual Studio 版本。
回答by Aryaman Gupta
Make sure you have selected the correct version of Visual Studio. This is trickier than it seems because Visual Studio 2015 is actually Visual Studio 14, and similarly Visual Studio 2012 is Visual Studio 11. I had incorrectly selected Visual Studio 15 which is actually Visual Studio 2017, when I had 2015 installed.
确保您选择了正确版本的 Visual Studio。这比看起来更棘手,因为 Visual Studio 2015 实际上是 Visual Studio 14,同样,Visual Studio 2012 是 Visual Studio 11。当我安装 2015 时,我错误地选择了 Visual Studio 15,它实际上是 Visual Studio 2017。
回答by Meir Gerenstadt
None of the solutions here solves my problem - only when I install Windows Update for universal C runtime.
这里的任何解决方案都不能解决我的问题 - 只有当我为通用 C 运行时安装 Windows 更新时。
Now CMake is working and no more link hangs from Visual Studio.
现在 CMake 正在运行,并且 Visual Studio 不再挂起链接。