如何以平台中立的方式确定架构?

发布于 2024-08-16 11:34:16 字数 111 浏览 5 评论 0原文

我有一个使用 wxWidgets 的 C++ 应用程序。对于 32 位和 64 位主机,应用程序的某些部分有所不同。目前我使用 sizeof(void *),但是有没有更好的方法使用条件编译并且是平台中立的?

I have a C++ app that uses wxWidgets. Certain parts of the app differ for 32 and 64 bit hosts. Currently I use sizeof(void *), but is there a better way that uses conditional compilation and is platform neutral?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

被你宠の有点坏 2024-08-23 11:34:16

通常人们使用#defines 来确定位数(确切的定义取决于编译器)。这比使用 sizeof(void*) 的运行时方法更好。

至于平台中立,有些编译器可以在多个平台上运行。

Typically people use #defines to determine bitness (the exact define will depend on the compiler). This is better than a runtime approach using sizeof(void*).

As for platform neutral, well, some compilers are on multiple platforms..

指尖凝香 2024-08-23 11:34:16

根据您的编译器,您可能可以访问特定于平台的宏。尝试查找他们的文档。

Depending on your compiler you may have access to platform specific macros. Try to look up their documentation.

自由如风 2024-08-23 11:34:16

所有常见编译器都具有识别平台的预定义预处理器宏。例如,如果您正在使用 GCC,您可以轻松地检查它们:

touch foo.h; g++ -E -dM foo.h

#define linux 1
#define __x86_64 1

对我来说是有利的,因为我现在使用的是 64b linux,而且

#define __APPLE__ 1
#define __i386 1

我听说在 32b OS X 上。

对于 Sun Studio 12,它们记录在此处< /a>.此外,Sun Microsystems 将它们视为编译器 API 的一部分,因此确保了兼容性。例如,在我的 Solaris 机器上,我定义了 __SunOS_5_10__sparcv9(意味着 64b)。

在带有 IBM xlc 编译器的 AIX 系统上,查看 /etc/vac.cfg 及其 options 关键字字段以找出预定义的宏。在我有权访问的系统上至少定义了 _AIX 和更具体的 _AIX61 以及 _POWER(在 64b PPC 上)。

在 HP-UX 及其 aCC 编译器上,itanium 上至少有 __ia64 宏。其他一些特定于 aCC 的预定义宏记录在此处

All common compilers have predefined preprocessor macros that identify the platform. For example, if you are using GCC, you can check them all easily:

touch foo.h; g++ -E -dM foo.h

which yields among others

#define linux 1
#define __x86_64 1

for me, since I'm using a 64b linux at the moment, and

#define __APPLE__ 1
#define __i386 1

on a 32b OS X, I hear.

For Sun Studio 12, they are documented here. Also, Sun Microsystems considers them as part of the API of the compiler so compatibility is ensured. For example, on my Solaris box, I have __SunOS_5_10 and __sparcv9 defined (implies 64b).

On AIX systems with the IBM xlc compiler, take a look at /etc/vac.cfg and its options keyword fields to find out the predefined macros. There are at least _AIX and more specific _AIX61 as well as _POWER (on a 64b PPC) defined on a system where I have access.

On HP-UX and its aCC compiler, there's at least the __ia64 macro on itanium. Some other aCC specific predefined macros are documented here.

允世 2024-08-23 11:34:16

在大小很重要的地方使用 sizeof() 有什么问题吗?编译器很乐意将其优化为常量。

What's wrong with using sizeof() where the size matters? The compiler will happily optimise it away to a constant.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文