使用 CMake 强制进行 32 位编译的正确方法 [英] The proper way of forcing a 32-bit compile using CMake

查看:42
本文介绍了使用 CMake 强制进行 32 位编译的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,有很多类似的问题,但我确实发现谷歌搜索 CMake 查询总是会产生类似但不相同的场景、冲突的 CMake 命令等等!

Sorry that there are many similar questions, but I do find that Googling for CMake queries always yields similar-but-not-the-same scenarios, conflicting CMake commands and so on!

我需要强制我的项目构建 32 位二进制文​​件,因为我必须链接一个只能作为 32 位使用的库.我根据错误消息对此进行了诊断,例如:

I need to force my project to build 32-bit binaries because I have to link with a library which is only available as 32-bit. I diagnosed this based on error messages such as:

/usr/bin/ld: i386 architecture of input file `*external-32bit-lib*' is incompatible with i386:x86-64 output

根据我收集的信息,我应该使用:

From what I gather, I should therefore use:

set (CMAKE_CXX_FLAGS "-m32")

这确实改变了一些事情 - 我现在收到几个错误,例如:

This does change things - I now get several errors like:

/usr/bin/ld: i386 architecture of input file `*project-output-lib*' is incompatible with i386:x86-64 output

对于外部库,仍然会出现相同的错误.我认为这是因为 -m32 使 gcc 生成 32 位二进制文​​件,但 ld 仍在尝试 64 位输出?进一步谷歌搜索这个问题没有取得任何成功,所以如果有人能证实我是对的并给出正确的方法,我将不胜感激!

AND still get the same errors for the external library too. I think this is because the -m32 made gcc generate 32-bit binaries, but ld is still trying for 64-bit output? Further Googling for this problem didn't give any success, so if anyone could verify that I am right and give the correct way of doing this, I would be very grateful!

非常感谢!

推荐答案

如果您想使用 cmake 编译和链接 32 位,请使用它来创建库和二进制文件:

If you want to compile and link for 32 bit using cmake use this for creating libraries and binaries:

创建库:

add_library(mylib SHARED my_source.c)
set_target_properties(mylib PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")

创建可执行文件:

add_executable(mybin sources.c)
set_target_properties(mybin PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")

这篇关于使用 CMake 强制进行 32 位编译的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆