是否有一个GCC预处理器指令来检查代码是否正在64位机器上编译? [英] Is there a GCC preprocessor directive to check if the code is being compiled on a 64 bit machine?

查看:380
本文介绍了是否有一个GCC预处理器指令来检查代码是否正在64位机器上编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试做如下事情:

#ifdef 64-bit
    #define DECIMAL_FORMAT %ld
#else
    #define DECIMAL_FORMAT %d
#endif
.
intptr_t d;  
.
printf("Some message with DECIMAL_FORMAT in the middle of it\n", d);

类型为'intptr_t'的变量'd'需要'%d'格式说明符32位机器和格式说明符'%ld'在64位机器上。我正在寻找一个解决方案,以便能够在32位机器和64位机器上编译代码,而无需更改GCC命令行或源代码。

The variable 'd' being of the type 'intptr_t' needs '%d' format specifier on 32 bit machines and format specifier '%ld' on 64 bit machines. I am looking a solution to be able to compile the code on both 32 bit machines and 64 bit machines without making changes to the GCC command line or the source code.

推荐答案

我认为__LP64__可能是你要找的。请参见 http://gcc.gnu.org/onlinedocs/gcc-4.1 .2 / cpp / Common-Predefined-Macros.html

I think __LP64__ might be what you're looking for. See http://gcc.gnu.org/onlinedocs/gcc-4.1.2/cpp/Common-Predefined-Macros.html

也许更好的方法是使用%p标识符printf )而不是%ld或%d。然后你甚至不必担心你的指针大小是什么。

Maybe a better way to go, though, is to use the %p specifier to printf() instead of %ld or %d. Then you don't even have to worry what your pointer size is.

这篇关于是否有一个GCC预处理器指令来检查代码是否正在64位机器上编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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