如何在 Windows 上使用 CMake 进行非交互式构建?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1459482/
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-09-09 06:45:10  来源:igfitidea点击:

How to Use CMake for Non-Interactive Build on Windows?

windowsbuildcmake

提问by amit

I want to set up automated build using CMake on Windows. I am using Visual Studio 2005.

我想在 Windows 上使用 CMake 设置自动构建。我正在使用 Visual Studio 2005。

Update: Here is what I am using:

更新:这是我正在使用的:

I set devenv.exe to my PATH. Then to build I run the command below. I am using Hudson to build.

我将 devenv.exe 设置为我的 PATH。然后构建我运行下面的命令。我正在使用 Hudson 进行构建。

devenv Crackpot.sln /build Debug /project ALL_BUILD

devenv Crackpot.sln /build 调试 /project ALL_BUILD

As per http://blogs.msdn.com/aaronhallberg/archive/2007/06/29/building-from-the-command-line-with-devenv.aspxprefer to use "devenv" and not "denenv.exe" as the latter may spawn a GUI thus hanging the build.

根据http://blogs.msdn.com/aaronhallberg/archive/2007/06/29/building-from-the-command-line-with-devenv.aspx更喜欢使用“devenv”而不是“denenv.exe”因为后者可能会产生一个 GUI 从而挂起构建。

采纳答案by OregonGhost

I'm not sure if I understand the question. You use it exactly like for any other build system. Just specify "Visual Studio 8 2005" (little bit weird, but you can get a list of all supported systems by calling cmake without parameters) and you'll get a solution that can be built on the command line either with devenv.exe /buildor with MSBuild.

我不确定我是否理解这个问题。您可以像使用任何其他构建系统一样使用它。只需指定“Visual Studio 8 2005”(有点奇怪,但您可以通过不带参数调用 cmake 来获取所有支持系统的列表),您将获得一个可以使用devenv.exe /build或使用 MSBuild在命令行上构建的解决方案。

The only thing that is a little bit complicated is if you want to generate the solution when Visual Studio is not installed, like on a build server. You can, of course, just install it, but I prefer not to install things you don't need. In that case, you have to fake it to accept MSBuild as the build command line (by specifying a batch file as build tool at the command line, that just reorders the arguments so MSBuild accepts them), so that it won't start bitching about how it misses Visual Studio (which is so crazy, since the CMake people are from the command-line world...)

唯一有点复杂的是,如果您想在未安装 Visual Studio 的情况下(例如在构建服务器上)生成解决方案。当然,您可以只安装它,但我不想安装您不需要的东西。在这种情况下,你必须假装它接受 MSBuild 作为构建命令行(通过在命令行指定一个批处理文件作为构建工具,它只是重新排序参数以便 MSBuild 接受它们),这样它就不会开始抱怨关于它如何错过 Visual Studio(这太疯狂了,因为 CMake 人来自命令行世界......)

Oh, and if what you really want is just to build an existing Visual Studio solution on the command line, you don't need CMake. Just call MSBuild or devenv /build.

哦,如果您真正想要的只是在命令行上构建现有的 Visual Studio 解决方案,那么您不需要 CMake。只需调用 MSBuild 或devenv /build.

回答by Aaditya Kalsi

The simplest way I found to doing this was:
% cmake --build "buildDir"
you can also add --target and --config 'Debug|Release|...'

我发现这样做的最简单方法是:
% cmake --build "buildDir"
您还可以添加--target --config 'Debug|Release|...'

回答by pkit

You can run CMake from the command line. You could run.

您可以从命令行运行 CMake。你可以跑。

cmake.exe -G"Visual Studio 8 2005" -H<source_dir> -B<build_dir>

Below is a snippet from the original command line usage output. Notice that the -H and -B option are not documented there. But they can be used to explicitly define the source and build directories on the command line.

以下是原始命令行用法输出的片段。请注意,此处未记录 -H 和 -B 选项。但是它们可用于在命令行上显式定义源和构建目录。

C:\Program Files (x86)\CMake 2.6\bin>cmake
  cmake version 2.6-patch 4
  Usage

  cmake [options] <path-to-source>
  cmake [options] <path-to-existing-build>

  Options
  -C <initial-cache>          = Pre-load a script to populate the cache.
  -D <var>:<type>=<value>     = Create a cmake cache entry.
  -U <globbing_expr>          = Remove matching entries from CMake cache.
  -G <generator-name>         = Specify a makefile generator.
  -Wno-dev                    = Suppress developer warnings.
  -Wdev                       = Enable developer warnings.
  -E                          = CMake command mode.
  -i                          = Run in wizard mode.
  -L[A][H]                    = List non-advanced cached variables.
  -N                          = View mode only.
  -P <file>                   = Process script mode.

Here are the available generators.

这是可用的生成器。

Generators

The following generators are available on this platform:
  Borland Makefiles           = Generates Borland makefiles.
  MSYS Makefiles              = Generates MSYS makefiles.
  MinGW Makefiles             = Generates a make file for use with
                                mingw32-make.
  NMake Makefiles             = Generates NMake makefiles.
  Unix Makefiles              = Generates standard UNIX makefiles.
  Visual Studio 6             = Generates Visual Studio 6 project files.
  Visual Studio 7             = Generates Visual Studio .NET 2002 project
                                files.
  Visual Studio 7 .NET 2003   = Generates Visual Studio .NET 2003 project
                                files.
  Visual Studio 8 2005        = Generates Visual Studio .NET 2005 project
                                files.
  Visual Studio 8 2005 Win64  = Generates Visual Studio .NET 2005 Win64
                                project files.
  Visual Studio 9 2008        = Generates Visual Studio 9 2008 project files.
  Visual Studio 9 2008 Win64  = Generates Visual Studio 9 2008 Win64 project
                                files.
  Watcom WMake                = Generates Watcom WMake makefiles.
  CodeBlocks - MinGW Makefiles= Generates CodeBlocks project files.
  CodeBlocks - Unix Makefiles = Generates CodeBlocks project files.
  Eclipse CDT4 - MinGW Makefiles
                              = Generates Eclipse CDT 4.0 project files.
  Eclipse CDT4 - NMake Makefiles
                              = Generates Eclipse CDT 4.0 project files.
  Eclipse CDT4 - Unix Makefiles
                              = Generates Eclipse CDT 4.0 project files.  

回答by Mohith Maratt

This is the bat file I created. It automatically creates the solution in the build folder you specify, each time deleting and creating a new build folder.

这是我创建的bat文件。它会自动在您指定的构建文件夹中创建解决方案,每次删除并创建一个新的构建文件夹。

RMDIR C:\Users\abc /s /q

if EXIST C:\Users\abc GOTO FALIURE

MKDIR C:\Users\abc\build
CD C:\Users\abc\build
cmake -G "Visual Studio 12" "C:\Users\abc\src"
EXIT

:FALIURE
CLS
echo "Failed to delete BUILD directory, Close all related files and programs and try again."
pause
EXIT