C++ 使用 CMake 强制进行 32 位编译的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5805874/
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
The proper way of forcing a 32-bit compile using CMake
提问by devrobf
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!
抱歉,有很多类似的问题,但我确实发现谷歌搜索 CMake 查询总是会产生类似但不相同的场景、冲突的 CMake 命令等等!
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:
我需要强制我的项目构建 32 位二进制文件,因为我必须链接一个只能作为 32 位使用的库。我根据错误消息诊断了这一点,例如:
/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
AND still get the same errors for the external library too. I thinkthis 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!
并且对于外部库仍然会出现相同的错误。我认为这是因为-m32
made gcc 生成了 32 位二进制文件,但 ld 仍在尝试 64 位输出?进一步谷歌搜索这个问题没有取得任何成功,所以如果有人能验证我是对的并给出正确的方法,我将不胜感激!
Many thanks!
非常感谢!
回答by ant2009
If you want to compile and link for 32 bit using cmake use this for creating libraries and binaries:
如果要使用 cmake 编译和链接 32 位,请使用它来创建库和二进制文件:
Creating libraries:
创建库:
add_library(mylib SHARED my_source.c)
set_target_properties(mylib PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")
creating executables:
创建可执行文件:
add_executable(mybin sources.c)
set_target_properties(mybin PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")
回答by malat
Even if this seems like extra works, I believe a proper solution is to use toolchain file in this case. Something like:
即使这看起来像是额外的工作,我相信在这种情况下,正确的解决方案是使用工具链文件。就像是:
# the name of the target operating system
set(CMAKE_SYSTEM_NAME Linux)
# which compilers to use for C and C++
set(CMAKE_C_COMPILER gcc)
set(CMAKE_C_FLAGS -m32)
set(CMAKE_CXX_COMPILER g++)
set(CMAKE_CXX_FLAGS -m32)
# here is the target environment located
set(CMAKE_FIND_ROOT_PATH /usr/i486-linux-gnu )
# adjust the default behaviour of the FIND_XXX() commands:
# search headers and libraries in the target environment, search
# programs in the host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
Then usage is simply:
那么用法很简单:
$ cmake -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake /path/to/source
The important part here is that one is now able to specify a root dir path (CMAKE_FIND_ROOT_PATH
) which should be used to search for third party lib. Indeed your compiler may not be smart enough to know where to search for an x86 Qt library on an x86_64 system.
这里的重要部分是现在可以指定一个根目录路径 ( CMAKE_FIND_ROOT_PATH
),该路径应该用于搜索第三方库。实际上,您的编译器可能不够聪明,无法知道在 x86_64 系统上在哪里搜索 x86 Qt 库。
Having a toolchain file allows one to specify a different one on a par compiler basis, and you should be able to tweak the option when compiling in 32bits from a windows environement.
拥有一个工具链文件允许在标准编译器的基础上指定一个不同的文件,并且在 Windows 环境中以 32 位编译时,您应该能够调整该选项。
Nowadays this is extra works since compiling 32bits from an x86_64 Linux OS is pretty much trivial, but this solution will work for other more exotic setup.
现在这是额外的工作,因为从 x86_64 Linux 操作系统编译 32 位非常简单,但此解决方案适用于其他更奇特的设置。
For more information on toolchain file, one can check for example:
有关工具链文件的更多信息,可以检查例如:
回答by sakra
CMAKE_CXX_FLAGS
only affects the C++ compiler. You probably also have to set the flag for the C compiler:
CMAKE_CXX_FLAGS
只影响 C++ 编译器。您可能还必须为 C 编译器设置标志:
set (CMAKE_C_FLAGS "-m32")
回答by Fredrik E
It sounds like you did not pass m32 to LFLAGS too, or there are old obj files skulking about. Be sure to clean first.
听起来您也没有将 m32 传递给 LFLAGS,或者有旧的 obj 文件在偷偷摸摸。一定要先清洁。
This question is similar to yours: cmake, gcc, cuda and -m32
这个问题与你的类似:cmake、gcc、cuda 和 -m32
回答by Naszta
Use TRY_RUN
command by the following source.
TRY_RUN
通过以下来源使用命令。
size.cpp:
大小.cpp:
#include <cstdlib>
int main( int argc, char** argv )
{
size_t size = sizeof(void*);
if ( size == 4 )
return 0;
return 1;
}
CMakeLists.txt:
CMakeLists.txt:
TRY_RUN(RUN_RESULT_VAR COMPILE_RESULT_VAR ${your_temp_dir} size.cpp RUN_OUTPUT_VARIABLE IS_64_SYSTEM)
IF(IS_64_SYSTEM)
MESSAGE(FATAL_ERROR "64 compiling not allowed!")
ENDIF(IS_64_SYSTEM)
It will work on all standard compiler.
它适用于所有标准编译器。
回答by Rafael Kitover
I used malat's approach and made a Toolchain file.
我使用了malat的方法并制作了一个工具链文件。
I have a Toolchain file that works on some Linux dists, perhaps it will give you inspiration. It may work for you as is, or you may need other ugly hacks to get other cmake scripts you rely on to work or w/e:
我有一个适用于某些 Linux 发行版的 Toolchain 文件,也许它会给你灵感。它可能对你有用,或者你可能需要其他丑陋的黑客来让你依赖的其他 cmake 脚本工作或 w/e:
https://github.com/visualboyadvance-m/visualboyadvance-m/blob/master/cmake/Toolchain-cross-m32.cmake
https://github.com/visualboyadvance-m/visualboyadvance-m/blob/master/cmake/Toolchain-cross-m32.cmake