c++ - 带有Microsoft Visual C++生成工具的CMake

原文 标签 c++ visual-c++ cmake

CMake with Microsoft Visual C++ Build Tools

EDIT: Having posted this to their issue tracker and stepped through it with some CMake devs, this actually isn't a CMake problem. Something is broken with my MSBuild installation that causes it to return the "The operation completed successfully." error. Still no resolution on the issue overall, but this narrows down the potential causes.


I'm trying to build a CMake project on Windows using the MS Visual C++ Build Tools 2015 (note: not full Visual Studio). CMake, however, is apparently unable to find cl.exe

cmake ..
-- Building for: Visual Studio 14 2015
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:4 (project):
  No CMAKE_C_COMPILER could be found.



CMake Error at CMakeLists.txt:4 (project):
  No CMAKE_CXX_COMPILER could be found.

I suspect this is because CMake is expecting to find the compiler in with the Visual Studio installation but perhaps the standalone build tools install it in a different location? Is it possible to configure CMake to look elsewhere for the compiler?

EDIT: To head this possibility off at the pass, cl.exe is definitely installed. When I open the Visual C++ shell (adds the tools to the path) cl.exe outputs:

cl.exe
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24210 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

usage: cl [ option... ] filename... [ /link linkoption... ]

And that is the same environment I'm running cmake .. from so cl.exe is definitely on the PATH for discovery by CMake.

EDIT 2: Looking at the CMakeError.log file I see a couple variants of the following

Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" failed.
Compiler:  
Build flags: 
Id flags:  

The output was:
1
Microsoft (R) Build Engine version 14.0.25420.1
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 4/14/2017 11:58:13 AM.
Project "E:\<project_dir>\build\CMakeFiles\3.8.0-rc4\CompilerIdCXX\CompilerIdCXX.vcxproj" on node 1 (default targets).
PrepareForBuild:
  Creating directory "Debug\".
  Creating directory "Debug\CompilerIdCXX.tlog\".
InitializeBuildStatus:
  Creating "Debug\CompilerIdCXX.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
ClCompile:
  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\CL.exe /c /nologo /W0 /WX- /Od /Oy- /D _MBCS /Gm- /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"Debug\\" /Fd"Debug\vc140.pdb" /Gd /TP /analyze- /errorReport:queue CMakeCXXCompilerId.cpp
TRACKER : error TRK0002: Failed to execute command: ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\CL.exe" @C:\Users\<user>\AppData\Local\Temp\tmpa0925a9f05d5426d82afdcee8d722031.rsp". The operation completed successfully. [E:\<project_dir>\build\CMakeFiles\3.8.0-rc4\CompilerIdCXX\CompilerIdCXX.vcxproj]


Done Building Project "E:\<project_dir>\build\CMakeFiles\3.8.0-rc4\CompilerIdCXX\CompilerIdCXX.vcxproj" (default targets) -- FAILED.

Build FAILED.

"E:\<project_dir>\build\CMakeFiles\3.8.0-rc4\CompilerIdCXX\CompilerIdCXX.vcxproj" (default target) (1) ->
(ClCompile target) -> 
  TRACKER : error TRK0002: Failed to execute command: ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\CL.exe" @C:\Users\<user>\AppData\Local\Temp\tmpa0925a9f05d5426d82afdcee8d722031.rsp". The operation completed successfully. [E:\<project_dir>\build\CMakeFiles\3.8.0-rc4\CompilerIdCXX\CompilerIdCXX.vcxproj]

    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:01.21

This looks like some kind of output compatibility issue, especially the "ERROR: The operation completed successfully." lines. I'm using CMake 3.8.0-rc4 and the Visual C++ 2015 Build Tools. Any ideas?

EDIT 3: I thought that upgrading from 3.8.0-rc4 to 3.8.0 might fix it, but no avail. I also considered that I was using 64bit CMake trying to build a 32 bit program so I changed that as well. No luck yet.

EDIT 4: Also, for the record, this does build with CMake on a PC with full VS 2015 installed.

Answer

I'm not sure I bring a solution to your The operation completed successfully problem, but I was able to compile a [big] real-life project using cmake 3.8.1 and C++2015 BuildTools.

The trick is simply to use the "VS2015 x64 Native Tools Command Prompt" (or x86). You either call cmake using this prompt, or you call this prompt from your normal prompt and it will set your path properly.

REM Set up the include/lib paths of Visual Studio
call "C:\Program Files (x86)\Microsoft Visual C++ Build Tools\vcbuildtools.bat" amd64

cd to_build_dir

REM Call cmake with the right parameters
"C:\Program Files\CMake\bin\cmake.exe" .......

REM Build the XYZ project
MSBuild.exe SOLUTION_FILE.sln /t:XYZ .......

As I wrote, I'm not sure it will fix your error because I wasn't able to reproduce it. However, I know that my project compile only when I use the right prompt. When I'm not using it, I get some error MSB4019: The imported project "C:\Microsoft.Cpp.Default.props" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.

翻译

编辑:
将其发布到他们的问题跟踪器并与一些CMake开发人员一起逐步解决之后,这实际上不是CMake问题。我的MSBuild安装发生了问题,导致它返回“操作成功完成”。错误。总体而言,仍然没有解决方案,但这可以缩小潜在原因的范围。



我正在尝试使用MS Visual C++ Build Tools 2015在Windows上构建CMake项目(注意:不完整的Visual Studio)。 CMake,但是,显然无法找到cl.exe

cmake ..
-- Building for: Visual Studio 14 2015
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:4 (project):
  No CMAKE_C_COMPILER could be found.



CMake Error at CMakeLists.txt:4 (project):
  No CMAKE_CXX_COMPILER could be found.


我怀疑这是因为CMake希望在Visual Studio安装中找到编译器,但也许独立的构建工具会将其安装在其他位置?是否可以将CMake配置为在其他地方寻找编译器?

编辑:为避免这种可能性,肯定安装了cl.exe。当我打开Visual C++ shell(将工具添加到路径)时,cl.exe输出:

cl.exe
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24210 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

usage: cl [ option... ] filename... [ /link linkoption... ]


这与我从中运行cmake ..的环境相同,因此cl.exe肯定位于CMake发现的路径上。

编辑2:查看CMakeError.log文件,我看到以下几个变体

Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" failed.
Compiler:  
Build flags: 
Id flags:  

The output was:
1
Microsoft (R) Build Engine version 14.0.25420.1
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 4/14/2017 11:58:13 AM.
Project "E:\<project_dir>\build\CMakeFiles\3.8.0-rc4\CompilerIdCXX\CompilerIdCXX.vcxproj" on node 1 (default targets).
PrepareForBuild:
  Creating directory "Debug\".
  Creating directory "Debug\CompilerIdCXX.tlog\".
InitializeBuildStatus:
  Creating "Debug\CompilerIdCXX.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
ClCompile:
  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\CL.exe /c /nologo /W0 /WX- /Od /Oy- /D _MBCS /Gm- /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"Debug\\" /Fd"Debug\vc140.pdb" /Gd /TP /analyze- /errorReport:queue CMakeCXXCompilerId.cpp
TRACKER : error TRK0002: Failed to execute command: ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\CL.exe" @C:\Users\<user>\AppData\Local\Temp\tmpa0925a9f05d5426d82afdcee8d722031.rsp". The operation completed successfully. [E:\<project_dir>\build\CMakeFiles\3.8.0-rc4\CompilerIdCXX\CompilerIdCXX.vcxproj]


Done Building Project "E:\<project_dir>\build\CMakeFiles\3.8.0-rc4\CompilerIdCXX\CompilerIdCXX.vcxproj" (default targets) -- FAILED.

Build FAILED.

"E:\<project_dir>\build\CMakeFiles\3.8.0-rc4\CompilerIdCXX\CompilerIdCXX.vcxproj" (default target) (1) ->
(ClCompile target) -> 
  TRACKER : error TRK0002: Failed to execute command: ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\CL.exe" @C:\Users\<user>\AppData\Local\Temp\tmpa0925a9f05d5426d82afdcee8d722031.rsp". The operation completed successfully. [E:\<project_dir>\build\CMakeFiles\3.8.0-rc4\CompilerIdCXX\CompilerIdCXX.vcxproj]

    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:01.21


这看起来像某种输出兼容性问题,尤其是“错误:操作成功完成”。线。我正在使用CMake 3.8.0-rc4和Visual C++ 2015构建工具。有任何想法吗?

编辑3:
我认为从3.8.0-rc4升级到3.8.0可能会解决此问题,但无济于事。我还认为我正在使用64位CMake尝试构建32位程序,因此我也对此进行了更改。还没有运气。

编辑4:
另外,为了记录起见,这确实是在装有完整VS 2015的PC上使用CMake构建的。
最佳答案
我不确定是否可以解决您的The operation completed successfully问题,但是我可以使用cmake 3.8.1和C++ 2015 BuildTools编译一个[大型]真实项目。

窍门仅是使用“ VS2015 x64本机工具命令提示符”(或x86)。您可以使用此提示调用cmake,或者从常规提示中call提示,它将正确设置路径。

REM Set up the include/lib paths of Visual Studio
call "C:\Program Files (x86)\Microsoft Visual C++ Build Tools\vcbuildtools.bat" amd64

cd to_build_dir

REM Call cmake with the right parameters
"C:\Program Files\CMake\bin\cmake.exe" .......

REM Build the XYZ project
MSBuild.exe SOLUTION_FILE.sln /t:XYZ .......


如我所写,我不确定它是否可以解决您的错误,因为我无法重现它。但是,我知道我的项目仅在使用正确的提示时才编译。当我不使用它时,我得到一些error MSB4019: The imported project "C:\Microsoft.Cpp.Default.props" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.
相关推荐

c++ - std::cin是否无法将命令行中的用户输入传递给具有char类型的变量?

c++ - 在公共场所使用“使用”来继承私有构造函数不会使其公开

c++ - 包含文件作为字符串[重复]

c++ - 位域成员的类型

c++ - 编译器的堆空间不足

c++ - C++强制转换为基类

c++ - 放置类构造函数

c++ - 将CMake项目添加到Visual Studio解决方案

c++ - 具有QtQuick的QAbstractItemModel:索引中的列始终为0

java - 点燃C++和缓存关联性