C语言 如何使用 CMAKE 从命令行在 Windows 上构建 x86 和/或 x64?

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

How to build x86 and/or x64 on Windows from command line with CMAKE?

cvisual-studiocmakecross-compilingx86-64

提问by 010110110101

One way to get cmake to build x86 on Windows with Visual Studio is like so:

让 cmake 使用 Visual Studio 在 Windows 上构建 x86 的一种方法是这样的:

  1. Start Visual Studio Command prompt for x86
  2. Run cmake: cmake -G "NMake Makefiles" \path_to_source\
  3. nmake
  1. 为 x86 启动 Visual Studio 命令提示符
  2. 运行 cmake: cmake -G "NMake Makefiles" \path_to_source\
  3. 制作

One way to get cmake to build x64 on Windows with Visual Studio is like so:

让 cmake 使用 Visual Studio 在 Windows 上构建 x64 的一种方法是这样的:

  1. Start Visual Studio Command prompt for x64
  2. Run cmake: cmake -G "NMake Makefiles" \path_to_source\
  3. nmake
  1. 为 x64 启动 Visual Studio 命令提示符
  2. 运行 cmake: cmake -G "NMake Makefiles" \path_to_source\
  3. 制作

Using Cmake, how do I compile either or both architectures? (like how Visual Studio does it from in the IDE)

使用 Cmake,我如何编译一个或两个架构?(就像 Visual Studio 如何在 IDE 中执行此操作一样)

回答by sakra

This cannot be done with CMake. You have to generate two separate build folders. One for the x86 NMake build and one for the x64 NMake build. You cannot generate a single Visual Studio project covering both architectures with CMake either.

这不能用 CMake 来完成。您必须生成两个单独的构建文件夹。一种用于 x86 NMake 构建,一种用于 x64 NMake 构建。您也无法使用 CMake 生成涵盖两种架构的单个 Visual Studio 项目。

To build Visual Studio projects from the command line for both 32-bit and 64-bit without starting a Visual Studio command prompt, use the regular Visual Studio generators.

若要在不启动 Visual Studio 命令提示符的情况下从命令行为 32 位和 64 位构建 Visual Studio 项目,请使用常规 Visual Studio 生成器。

For CMake 3.13 or newer, run the following commands:

对于 CMake 3.13 或更新版本,运行以下命令:

cmake -G "Visual Studio 16 2019" -A Win32 -S \path_to_source\ -B "build32"
cmake -G "Visual Studio 16 2019" -A x64 -S \path_to_source\ -B "build64"
cmake --build build32 --config Release
cmake --build build64 --config Release

For earlier version of CMake, run the following commands:

对于早期版本的 CMake,运行以下命令:

mkdir build32 & pushd build32
cmake -G "Visual Studio 15 2017" \path_to_source\
popd
mkdir build64 & pushd build64
cmake -G "Visual Studio 15 2017 Win64" \path_to_source\
popd
cmake --build build32 --config Release
cmake --build build64 --config Release

CMake generated projects that use one of the Visual Studio generators can be built from the command line with using the option --buildfollowed by the build directory. The --configoptions specifies the build configuration.

使用 Visual Studio 生成器之一的 CMake 生成的项目可以从命令行构建,使用--build后跟构建目录的选项。该--config选项指定生成配置。

回答by Zam

try use CMAKE_GENERATOR_PLATFORM

尝试使用 CMAKE_GENERATOR_PLATFORM

e.g.

例如

// x86
cmake -DCMAKE_GENERATOR_PLATFORM=x86 . 

// x64
cmake -DCMAKE_GENERATOR_PLATFORM=x64 . 

回答by user7610

Besides CMAKE_GENERATOR_PLATFORMvariable, there is also the -Aswitch

除了CMAKE_GENERATOR_PLATFORM变量,还有-A开关

cmake -G "Visual Studio 16 2019" -A Win32
cmake -G "Visual Studio 16 2019" -A x64

https://cmake.org/cmake/help/v3.16/generator/Visual%20Studio%2016%202019.html#platform-selection

https://cmake.org/cmake/help/v3.16/generator/Visual%20Studio%2016%202019.html#platform-selection

  -A <platform-name>           = Specify platform name if supported by
                                 generator.