在 64 位 Linux 机器上的 gcc 中生成“长"4 个字节 [英] Making 'long' 4 bytes in gcc on a 64-bit Linux machine

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

问题描述

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

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. 作为短期解决方案,有没有办法让 GCC 使用 4 个字节而不是 8 个字节来表示长"?
  2. 如果有,我们可能面临哪些问题?如果没有,是否有更简单的方法来解决 long 和 int 问题?

推荐答案

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

  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.

除了修复你的代码,没有.

Beyond fixing your code, no.

如果您需要一个大到足以存储指针值的可移植整数类型,请使用 intptr_t 或 uintptr_t(但通常要将指针值存储到整数中意味着您做错了什么,所以请三思!).对于能够表示两个指针之间差异的整数类型,请使用 ptrdiff_t.对于对象的大小,使用 size_t.

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 中生成“长"4 个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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