CMake:如何阻止 64 位构建?
我有一个 cmake 项目,由两个子项目组成,即项目 1 和项目 2。 项目 1 是一个可执行文件,应该是 32 位的。 项目 2 是一个库(模块),我需要它的 32 位和 64 位版本。 我正在使用 Visual Studio 2022。现在,如果我选择 64 位(通过 x64 Release),我担心 cmake 将为项目 1 构建 64 位可执行文件。 所以我想要一些 CMake 命令来确保项目 1 只能在 32 位上构建。 类似于项目 1 中的内容
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
##ABORT##
。谢谢
I have a cmake project comprising of two subprojects, say Project 1 and Project 2.
Project 1 is an executable and is supposed to be 32 bit.
Project 2 is a library (MODULE) and i need both 32 bit and 64 bit versions of it.
I am using visual studio 2022. Now, if i select 64 bit (via x64 Release), i am afraid cmake will build 64 bit executable for Project 1.
So i want some CMake commands to ensure that Project 1 can be built on 32 bit only.
Something like
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
##ABORT##
in Project 1. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是在 cmake 配置期间发出致命错误。
通常您会希望在 CMake 文件中避免类似的逻辑。 CMake 的最大好处之一是,通常只需很少的精力就可以为不同的配置编译项目的二进制文件。如果项目的某些部分仅适用于某些配置,那么为项目提供一个主
CMakeLists.txt
将会是用户友好的,它可以做出适当的选择,而不会在配置过程中导致错误。实现此目的的一个好方法是有条件地将子目录添加到父
CMakeLists.txt
中。即使使用这种方法,您也应该将带有致命错误的代码片段保留在子项目中,以防止出现问题,如果某些用户以无意的方式使用您的项目并使用项目 1 作为源目录而不是主项目目录。
Just emit a fatal error during the cmake configuration.
Usually you'll want to avoid logic like this in your CMake files though. One of the greatest benefits of CMake is the fact that it usually requires very little effort to compile the binaries of a project for different configurations. If parts of your project only apply to certain configurations, it would be user-friendly to provide a main
CMakeLists.txt
for the project that makes the appropriate choices without resulting in an error during configuration.A good way of accompilishing this would be to conditionally add the subdirectory in the parent
CMakeLists.txt
.Even with this approach you should keep the snippet with the fatal error in the subproject to prevent issues, if some user uses your project in an unintended way and uses Project 1 as source directory instead of your main project dir.