How to compile a 32-bit C application using gcc on the 64-bit version of Red Hat Enterprise Linux?

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 - 

English 

环境

  • Red Hat Enterprise Linux (all versions)

问题

  • How to compile a 32-bit C application using gcc on the 64-bit version of Red Hat Enterprise Linux?

决议

  • To compile a 32-bit C application for Red Hat Enterprise Linux running on a 64-bit platform, use the gcc option -m32.

    • According to the man page for gcc, the -m32 option sets int, long, and pointer variables to 32 bits (4 bytes). The -m64 option sets int to 32 bits (4 bytes) and long & pointer to 64 bits (8 bytes).
  • The following segment of code demonstrates this:

    Raw

    #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 -m32 and -m64 options:

    Raw

    # 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 bytes
    

    NOTE: 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 glibcglibc-devel and libgcc are needed for most C applications.

  • To install the 32-bit versions on Red Hat Enterprise Linux 5 :

    Raw

    # yum install libgcc.i386 glibc.i686 glibc-devel.i386
    
  • On Red Hat Enterprise Linux 6 and 7 :

    Raw

    # yum install libgcc.i686 glibc.i686 glibc-devel.i686
    
  • On older versions of Red Hat Enterprise Linux the same should be done with up2date:

    Raw

    # up2date --arch=i386 --install glibc-devel libgcc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值