#ifdef 用于 32 位平台 [英] #ifdef for 32-bit platform

查看:39
本文介绍了#ifdef 用于 32 位平台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我维护的一个应用程序中,我们遇到了影响 stdlib 的文件描述符限制问题.此问题仅影响标准库的 32 位版本.

In an application I maintain, we've encountered a problem with file descriptor limitations affecting the stdlib. This problem only affects the 32-bit version of the standard lib.

我已经为我的代码设计了一个修复程序并希望实现它,但仅限于为 32 位可执行文件编译时.我可以用什么预处理器符号 #ifdef 来确定代码是为 32 位还是 64 位目标编译的?

I have devised a fix for my code and would like to implement it, but only when compiling for 32-bit executable. What pre-processor symbol can I #ifdef for to determine whether the code is being compiled for a 32 or 64-bit target?

编辑

抱歉,没提,代码是跨平台的,linux,windows,solaris等一些unix的风格,大多使用GCC编译.我可以跨平台使用任何事实上的标准吗?

Sorry, didn't mention, the code is cross-platform, linux, windows, solaris and a few other unix flavors, mostly using GCC for compilation. Any de-facto standards I can use cross-platform?

编辑 2

我发现了一些定义__ILP23"和__LP64",它们似乎可以工作……讨论这里 解释了 unix 平台的背景.任何人都有使用这些定义的经验?这个能用吗?

I've found some definitions "__ILP23" and "__LP64" that seem like they may work... a discussion here explains the background on the unix platform. Anyone had any experience with using these defines? Is this going to be usable?

推荐答案

我不确定是否有合适的通用 #if def.C++ 标准几乎肯定没有定义.当然也有平台特定的.

I'm not sure if there is a universal #if def that is appropriate. The C++ standard almost certainly does not define one. There are certainly platform spcefic ones though.

例如,Windows

#if _WIN64 
// 64 bit build
#else
// 32 bit build
#endif

编辑 OP 提到这是使用 GCC 和其他编译器在 Windows 和非 Windows 之间进行交叉编译

EDIT OP mentioned this is a cross compile between Windows and Non-Windows using GCC and other compilers

没有可用于所有平台和编译器的通用宏.一点预处理魔法虽然可以做到这一点.假设您只在 x86 和 amd64 芯片上工作,以下应该可以解决问题.不过它可以很容易地扩展到其他平台

There is no universal macro that can be used for all platforms and compilers. A little bit of preprocessor magic though can do the trick. Assuming you're only working on x86 and amd64 chips the following should do the trick. It can easily be expanded for other platforms though

#if _WIN64 || __amd64__
#define PORTABLE_64_BIT
#else
#define PORTABLE_32_BIT
#endif

这篇关于#ifdef 用于 32 位平台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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