CMakeLists.txt:30行出现错误:没有找到CMAKE_C_COMPILER。

181

你可以使用Visual Studio和它的编译器或者Makefiles和gcc来配置项目。你尝试过哪种方法? - Tsyvarev
你输入的cmake命令是什么? - bames53
3
我想为@Caio Fontes添加相应的命令行:cmake .. -G "Visual Studio 14 2015"cmake .. -G "MSYS Makefiles"(如果gcc表示MinGW)。并且我看到了很多这样的问题(如这里这里这里),我认为我们在SO上缺少一个结合/教程答案。 - Florian
我刚在 Visual Studio 12 2013 Win64 上尝试了一下,完全没有任何问题(虽然我每天都使用 CMake,所以我知道我的 Vs2013 能够与 CMake-3.3.1 协同工作)。你在切换编译器时是否删除了构建文件夹? - drescherjm
显示剩余2条评论
23个回答

148

对于Ubuntu,请安装以下内容:

sudo apt-get update && sudo apt-get install build-essential

3
用于CentOS 7.9的是什么? - learner
我在Ubuntu上做了这件事,但它触发了错误“未知的CXX编译器标识”。 - Pablo
这在我的Ubuntu上有效。 - Roi

130

那些错误信息

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".

或者

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!

这意味着CMake无法找到您的C/CXX编译器来编译一个简单的测试程序(在检测构建环境时,CMake尝试的第一件事情之一)。

查找问题的步骤取决于您想要生成的构建环境。以下教程是Stack Overflow上的答案和我在Microsoft Windows 7/8/10和Ubuntu 14.04上使用CMake的经验的集合。

前提条件

  • You have installed the compiler/IDE and it was able to once compile any other program (directly without CMake)

  • 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.

你可以/应该尝试的一般方法


  1. 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

  2. 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 at CMakeFiles\[Version]\CompilerIdC|CompilerIdCXX directly from the command line (as found in the error log).

CMake无法找到Visual Studio

  1. Try to select the correct generator version:

     > cmake --help
     > cmake -G "Visual Studio 14 2015" ..
    
  2. 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 under All Programs/Visual Studio 2015/Visual Studio Tools (thanks at @Antwane for the hint).

背景:CMake支持所有Visual Studio版本和类型(Express、Community、Professional、Premium、Test、Team、Enterprise、Ultimate等)。为了确定编译器的位置,它使用搜索注册表(例如在HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\[Version];InstallDir中)、系统环境变量以及 - 如果没有其他方法找到 - 直接尝试调用编译器的组合。

CMake无法找到GCC(MinGW/MSys)

  1. You start the MSys bash shell with msys.bat and just try to directly call gcc

     $ 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.

  2. 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 MinGW

  3. Still 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

背景:和 Visual Studio 工具一样,CMake 支持各种不同版本的 GCC。它会在环境变量(如 CC、CXX 等)中搜索或尝试调用编译器。此外,当进行交叉编译时,CMake 也会检测到任何前缀,并将其添加到 GNU 编译器工具链的所有 binutils(包括 arranlibstripldnmobjdumpobjcopy)。


1
提醒一下,一些cmake文档链接指向了一个非常旧的CMake版本,请记得使用版本选择器来匹配您计算机上的版本(例如,如果您没有看到需要的更新平台列表)。 - jrh
不错的列表。但请注意,如果没有安装g++,gcc可能是可以的,会导致错误。 - Nick Westgate
将C/C++编译器路径提供给cmake-gui.exe即可解决问题。 - Gray Programmerz

35

在我安装了Visual Studio 15 2017之后,出现了这个问题。

Visual Studio 14 2015的C++编译器并不是问题所在。似乎是与Windows 10 SDK有关的问题。

将Windows 10 SDK添加到Visual Studio 14 2015中解决了我的问题。

请参阅附加的截图。

在此输入图片描述


24

在我的电脑上,这个方法适用于 Ubuntu 17.10 (Artful Aardvark) 版本:

apt-get update
apt-get install build-essential

14

当我使用CMake工作时,我也遇到过这个错误:

No CMAKE_C_COMPILER could be found.
No CMAKE_CXX_COMPILER could be found.

在 MSDN 文库文章 Visual Studio 2015 中的 Visual C++ 中的 'warning' 盒子给了我需要的帮助。

Visual Studio 2015 默认没有安装 C++。因此,创建新的 C++ 项目将提示您下载必要的 C++ 组件。


谢谢,安装C++包后问题已解决。 - vel

6

我在使用CMake时遇到了相同的错误。在初始的CMake对话框中,我们需要选择Visual Studio编译器,但是我选择了错误的版本。

后来我将其更改为“Visual Studio 11 2012”,然后问题得到解决。(我在电脑上安装了Visual Studio Ultimate 2012版本)。一般来说,在初始的CMake配置对话框中,请尝试输入旧版本的Visual Studio。


6
我在构建libgit2-0.23.4时遇到了这个问题。对我来说,问题是由于未安装VS2015的C++编译器和相关软件包,因此缺少“C:\ Program Files(x86)\ Microsoft Visual Studio 14.0 \ VC \ vcvarsall.bat”文件,Cmake无法找到编译器。
我尝试在Visual Studio 2015 GUI(“C:\ Program Files(x86)\ Microsoft Visual Studio 14.0 \ Common7 \ IDE \ devenv.exe”)中手动创建一个C ++项目,创建项目时,我收到提示下载C ++及相关软件包。
下载所需的软件包后,我可以看到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

1
这对我有用。手动创建一个C++项目来安装必要的软件包解决了问题。 - Melaz

4
对我来说,当我将项目移动到更浅的父目录中时,即到:C:\Users\spenc\Desktop\MyProjectDirectory而不是C:\Users\spenc\Desktop\...\MyProjectDirectory,此问题在Windows上就解决了。我认为问题的根源是 MSBuild 有一个文件路径长度限制 为260个字符。这导致CMake执行的基本编译器测试失败,该测试构建了一个名为CompilerIdCXX.vcxproj的项目,并显示以下错误:C1083: Cannot open source file: 'CMakeCXXCompilerId.cpp',因为文件路径的长度如下所示,例如:C:\Users\spenc\Desktop\...\MyProjectDirectory\build\CMakeFiles\...\CMakeCXXCompilerId.cpp超过了MAX_PATH限制。然后,CMake得出结论:没有CXX编译器。

3

尝试了所有解决方案都无果后,我只需通过 cmake -DCMAKE_C_COMPILER=/usr/bin/clang -DCMAKE_CXX_COMPILER=/usr/bin/clang++ ... 提供这些缺失的参数。


这个解决了我在Ubuntu 20上的问题... - Onyr

2

确保您选择了正确版本的Visual Studio。这比看起来更棘手,因为Visual Studio 2015实际上是Visual Studio 14,类似地,Visual Studio 2012是Visual Studio 11。我曾经错误地选择了Visual Studio 15,它实际上是Visual Studio 2017,而我安装的是2015版。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接