https://access.redhat.com/solutions/4887
How to compile a 32-bit C application using gcc on the 64-bit version of Red Hat Enterprise Linux?
SOLUTION 已验证 - 已更新 2015年十一月26日19:18 -
环境
- Red Hat Enterprise Linux (all versions)
问题
- How to compile a 32-bit C application using
gccon the 64-bit version ofRed Hat Enterprise Linux?
决议
-
To compile a 32-bit C application for
Red Hat Enterprise Linuxrunning on a 64-bit platform, use thegccoption-m32.- According to the
manpage forgcc, the-m32option sets int, long, and pointer variables to 32 bits (4 bytes). The-m64option sets int to 32 bits (4 bytes) and long & pointer to 64 bits (8 bytes).
- According to the
-
The following segment of code demonstrates this:
#include <stdio.h> int main() { int i; long l; char *p; printf("Integer size: %i bytes\\n", sizeof(i)); printf("Long Integer size: %i bytes\\n", sizeof(l)); printf("Pointer size: %i bytes\\n", sizeof(p)); return 0; } -
The following is a comparison of the output when compiled with the
-m32and-m64options:# gcc -o ./test.64 -m64 test.c # gcc -o ./test.32 -m32 test.c # # ./test.64 Integer size: 4 bytes Long Integer size: 8 bytes Pointer size: 8 bytes # # ./test.32 Integer size: 4 bytes Long Integer size: 4 bytes Pointer size: 4 bytesNOTE: in order to get correct results when compiling applications, make sure that the libraries linked against are installed in 32-bit versions. As an example, the packages
glibc,glibc-develandlibgccare needed for most C applications. -
To install the 32-bit versions on
Red Hat Enterprise Linux 5:# yum install libgcc.i386 glibc.i686 glibc-devel.i386 -
On
Red Hat Enterprise Linux 6and7:# yum install libgcc.i686 glibc.i686 glibc-devel.i686 -
On older versions of
Red Hat Enterprise Linuxthe same should be done withup2date:# up2date --arch=i386 --install glibc-devel libgcc

本文介绍如何使用gcc在64位Red Hat Enterprise Linux上编译32位C应用程序,通过添加-m32选项,可以确保整型、长整型和指针变量的大小为32位。同时,需确保链接的库也为32位版本,如glibc、glibc-devel和libgcc等。

被折叠的 条评论
为什么被折叠?



