在64位Linux机器上,在gcc中创建'long'4个字节 [英] Making 'long' 4 bytes in gcc on a 64-bit Linux machine

查看:440
本文介绍了在64位Linux机器上,在gcc中创建'long'4个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将应用程序移植到Linux平台上的64位。该应用程序目前支持Linux,Windows,Mac 32位和Windows 64位。我们经常遇到的一个问题是int对long的使用,反之亦然。直到现在,这并不是一个问题,因为long和int在应用程序当前支持的平台上可以互换(都是4个字节)。代码库是一个巨大的代码库,有许多遗留代码用于许多数据类型的#defines,因此搜索long的所有用法并用int适当地替换会很麻烦。


  1. 作为一个短期解决方案,有没有办法让GCC使用4个字节而不是8个'long'?

  2. 如果有,有什么问题我们可能会面对?如果没有,是否有更简单的方法来解决long和int问题?


解决方案


  1. 没有。在Linux x86_64上,ABI指定long是8字节类型(LP64)。事实上,大多数(如果不是所有的)64位Unix系统(包括64位OS X,AFAIK)都是LP64,所以这不是Linux特有的。

    如果你需要一个足够大的可移植整数类型来存储一个指针值,请使用intptr_t或uintptr_t(但通常希望将指针值存储到整数中意味着您做错了某些事情,所以请仔细考虑!)。对于能够表示两个指针之间差异的整数类型,请使用ptrdiff_t。对于大小的对象,请使用size_t。


    I am working on porting an application to 64-bit on Linux platform. The application is currently supported on Linux, Windows, Mac 32-bit and Windows 64-bit. One of the issues we are frequently encountering is the usage of long for int and vice versa. This wasn't a problem till now since long and int are interchangeable (both are 4 bytes) in the platforms the application is currently supported on. The codebase being a huge one, with lots of legacy code with #defines for many data types, makes it cumbersome to search all usage of long and replace appropriately with int.

    1. As a short term solution, is there a way to make GCC use 4 bytes instead of 8 for 'long'?
    2. If it has, what are issues that we might face? If not, is there an easier way to fix the long and int problem?

    解决方案

    1. No. On Linux x86_64 the ABI specifies that long is a 8 byte type (LP64). In fact, most if not all 64-bit Unix systems (including 64-bit OS X, AFAIK) are LP64 so this is nothing specific to Linux.

    2. Beyond fixing your code, no.

    If you need a portable integer type which is large enough to store a pointer value, use intptr_t or uintptr_t (but usually wanting to store a pointer value into an integer means that you're doing something wrong, so think twice!). For an integer type which is capable of representing the difference between two pointers, use ptrdiff_t. For sizes of objects, use size_t.

    这篇关于在64位Linux机器上,在gcc中创建'long'4个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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