【问题标题】:How to build x86 and/or x64 on Windows from command line with CMAKE?如何使用 CMAKE 从命令行在 Windows 上构建 x86 和/或 x64?
【发布时间】:2015-04-05 16:14:59
【问题描述】:

让 cmake 在 Windows 上使用 Visual Studio 构建 x86 的一种方法如下:

  1. 为 x86 启动 Visual Studio 命令提示符
  2. 运行cmake:cmake -G "NMake Makefiles" \path_to_source\
  3. nmake

让 cmake 在 Windows 上使用 Visual Studio 构建 x64 的一种方法如下:

  1. 为 x64 启动 Visual Studio 命令提示符
  2. 运行cmake:cmake -G "NMake Makefiles" \path_to_source\
  3. nmake

使用 Cmake,我如何编译一个或两个架构? (就像 Visual Studio 在 IDE 中的做法一样)

【问题讨论】:

  • 这里相同,但似乎有一个解决方案:zeroset.mnim.org/2015/07/15/…(并且不关闭提示,并使用nmake
  • 如果您来到这里是因为您在 Windows 上使用-G"Ninja" 作为生成器;构建 32bit 使用 "x86 Native Tools Command Prompt" 构建 64bit 使用 "x64 Native Tools Command Prompt",它将使用正确的库、编译器和链接器。跨度>

标签: c visual-studio cmake cross-compiling x86-64


【解决方案1】:

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

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

对于 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

对于早期版本的 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

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

【讨论】:

  • 有没有办法使用一个命令提示符 + 两个构建目录,并且无需退出并启动 x86 提示符,然后是 x64 提示符,就可以创建两种架构?
【解决方案2】:

尝试使用CMAKE_GENERATOR_PLATFORM

例如

// x86
cmake -DCMAKE_GENERATOR_PLATFORM=x86 . 

// x64
cmake -DCMAKE_GENERATOR_PLATFORM=x64 . 

【讨论】:

【解决方案3】:

除了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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-23
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    相关资源
    最近更新 更多