C++ 使用 cmake 强制构建 32 位或 64 位的选项

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

Option to force either 32-bit or 64-bit build with cmake

c++cmake32bit-64bit

提问by Flogo

I would like to offer a way that always builds my target as a 32-bit or always as 64-bit executable executable with cmake independent of the host system (Adding the "-m32" or "-m64" flag for gcc, not sure yet what to do for other compilers).

我想提供一种始终将我的目标构建为 32 位或始终作为 64 位可执行文件的方法,cmake 独立于主机系统(为 gcc 添加“-m32”或“-m64”标志,不确定还可以为其他编译器做什么)。

I can think of three ways to do this, which one should I use?

我可以想到三种方法来做到这一点,我应该使用哪一种?

  1. an option (-DUSE32bit=true)
  2. a tool chain (-DCMAKE_TOOLCHAIN_FILE=64bit.toolchain)
  3. build types (-DCMAKE_BUILD_TYPE=release32)
  1. 一个选项(-DUSE32bit=true)
  2. 一个工具链(-DCMAKE_TOOLCHAIN_FILE=64bit.toolchain)
  3. 构建类型(-DCMAKE_BUILD_TYPE=release32)

In my case the forced 32-bit build will be the default and should be easy to use. A forced 64-bit build is the also useful for some cases and should not be too difficult. Using the bit width of the host system rarely makes sense for my case and I don't want to support it.

在我的情况下,强制的 32 位构建将是默认的并且应该易于使用。强制 64 位构建在某些情况下也很有用,应该不会太难。使用主机系统的位宽对我的情况很少有意义,我不想支持它。

I found a related question here (The proper way of forcing a 32-bit compile using CMake) but the answers mostly discuss how it can be done at all, not how best to make it configurable.

我在这里找到了一个相关的问题(使用 CMake 强制进行 32 位编译的正确方法),但答案主要讨论了如何完成,而不是如何最好地使其可配置。

采纳答案by Flogo

TL;DR

TL; 博士

Use toolchain

使用工具链

In depth

深入

  1. an option (-DUSE32bit=true)
  1. 一个选项(-DUSE32bit=true)

This is not scalable I guess. So what if you want to build N projects? You have to add N options.

我猜这不是可扩展的。那么如果你想建N个项目呢?您必须添加 N 个选项。

  1. build types (-DCMAKE_BUILD_TYPE=release32)
  1. 构建类型(-DCMAKE_BUILD_TYPE=release32)

This may work well. But in my opinion you're mixing unrelated stuff. Also I'm sure you have to adapt find_packagebehaviour by setting some *_ROOTCMake variables. It's not possible to do it with CMAKE_BUILD_TYPE(at least, again, in a scalable fashion).

这可能工作得很好。但在我看来,你正在混合不相关的东西。此外,我确定您必须find_package通过设置一些*_ROOTCMake 变量来适应行为。这是不可能的CMAKE_BUILD_TYPE(至少,再次以可扩展的方式)。

  1. a tool chain (-DCMAKE_TOOLCHAIN_FILE=64bit.toolchain)
  1. 一个工具链(-DCMAKE_TOOLCHAIN_FILE=64bit.toolchain)

The best variant. If you want to build two projects - just use same toolchain:

最好的变种。如果您想构建两个项目 - 只需使用相同的工具链:

cmake -Hproj-1 -B_builds/proj-1 -DCMAKE_TOOLCHAIN_FILE=/.../64bit.toolchain
cmake -Hproj-2 -B_builds/proj-2 -DCMAKE_TOOLCHAIN_FILE=/.../64bit.toolchain

If you want to build your 3rd party ExternalProject_Add with 64 bit architecture - just pass toolchain to CMAKE_ARGS:

如果您想使用 64 位架构构建您的第 3 方 ExternalProject_Add - 只需将工具链传递给 CMAKE_ARGS:

ExternalProject_Add(
    ...
    CMAKE_ARGS ... -DCMAKE_TOOLCHAIN_FILE=/.../64bit.toolchain
    ...
)

Want to adapt find_package- just add any CMake variables to toolchain file.

想要适应find_package- 只需将任何 CMake 变量添加到工具链文件中。

回答by tresf

For Visual Studio and per https://cmake.org/cmake/help/latest/variable/CMAKE_GENERATOR_PLATFORM.html

对于 Visual Studio 和每个https://cmake.org/cmake/help/latest/variable/CMAKE_GENERATOR_PLATFORM.html

For Visual Studio Generators with VS 2005 and above this specifies the target architecture.

对于 VS 2005 及更高版本的 Visual Studio 生成器,这指定了目标体系结构。

cmake . -DCMAKE_GENERATOR_PLATFORM=x64

回答by TarmoPikaro

Personally I wanted to switch to 32/64-bit projects within same solution for Visual studio / cmake configuration. I've figured out that this can be done using

我个人想在 Visual Studio/cmake 配置的相同解决方案中切换到 32/64 位项目。我发现这可以使用

set_target_properties(${project} PROPERTIES LINK_FLAGS ${PROJ_LINK_FLAGS})

where PROJ_LINK_FLAGScan be either /MACHINE:X86or /MACHINE:X64depending on compilation.

wherePROJ_LINK_FLAGS可以是/MACHINE:X86/MACHINE:X64取决于编译。