Solaris 10 上的 64 位与 32 位 C 代码
在我的 Solaris 10 update 9 系统上,会产生以下命令:
#isainfo -b
64
但是,如果我用 C 语言创建以下程序并包含 limit.h,我会得到:
#include <stdio.h>
#include <limits.h>
int main(void)
{
printf("Maximum integer value on this system is = %d\n", INT_MAX);
}
gcc on64.c -o on64 ./on64 Maximum integer value on this system is = 2147483647
我期望得到更大的结果,因为系统在 64 位上运行。这看起来像是 32 位结果。这是编译器问题吗?
On my Solaris 10 update 9 system the following command yields:
#isainfo -b
64
But if I create the following program in C with limits.h is included I get:
#include <stdio.h>
#include <limits.h>
int main(void)
{
printf("Maximum integer value on this system is = %d\n", INT_MAX);
}
gcc on64.c -o on64 ./on64 Maximum integer value on this system is = 2147483647
I was expecting a much bigger result because the system runs on 64 bit. This seems like a 32 bit result. Is this a compiler issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
64 位平台有多种编程模型,http://www.unix .org/version2/whatsnew/lp64_wp.html,包括:
64 -bit Solaris 10 使用 LP64 模型 (http://www.sun.com/software/solaris/faqs/64bit.xml#q4):
除了“64 位编程模型:为什么选择 LP64?”上面引用的论文中,您可能需要查看 Raymond Chen 对 Win64 为什么选择 LLP64 模型的解释,因为它可能有助于支持 unix.org 文档中的各种基本原理和论点:http://blogs.msdn.com/b/oldnewthing/archive/2005/01/31/363790。 ASPX
There are a variety of programming models for 64-bit platforms, http://www.unix.org/version2/whatsnew/lp64_wp.html, including:
64-bit Solaris 10 uses the LP64 model (http://www.sun.com/software/solaris/faqs/64bit.xml#q4):
In addition to the "64-Bit Programming Models: Why LP64?" paper referenced above, you might want to look at Raymond Chen's explanation for why Win64 chose the LLP64 model, as it might help bolster the various rationales and arguments in the unix.org document: http://blogs.msdn.com/b/oldnewthing/archive/2005/01/31/363790.aspx
无论平台如何,“int”类型在 gcc 上都是 32 位。 “long”类型在 32 位平台上为 32 位,在 64 位平台上为 64 位。
为了减少歧义,您可以使用 C99 类型:
The "int" type is 32-bits on gcc regardless of the platform. The "long" type is 32 bits on 32-bit platforms, and 64 bits on 64-bit platforms.
To be less ambiguous, you could use C99 types:
来自 gcc 文档:
From the gcc documentation:
您可以在 Solaris 10 上编译 32 位或 64 位程序。默认情况下,它们编译为 32 位。
使用 GCC 和更新的 Sun 编译器时,选项“
-m32
”和“-m64
”指示使用哪个选项。因此,尝试:然后运行:
You can compile programs on Solaris 10 for 32-bit or 64-bit. By default, they are compiled 32-bit.
Using both GCC and the more recent Sun compilers, the options '
-m32
' and '-m64
' dictate which option is used. Hence, try:Then run:
如果您想要最大整数类型的大小,即
intmax_t
:它始终至少为 2^63 - 1。
If you want the size of the largest integral type, that's
intmax_t
:This will always be at least 2^63 - 1.
首先,您必须确保编译器在 64 位模式下运行。某些编译器默认为 32 位目标平台模式,即使它们能够生成 64 位代码。
其次,一些编译器更喜欢 64 位类型模型,其中
int
类型仍然是 32 位。 GCC实际上就是其中之一。因此,您对int
类型在 64 位模式下成为 64 位类型的期望是完全没有根据的。同样,一切都取决于编译器,并且仅取决于编译器(以及编译器设置)。您对操作系统所做的事情完全无关。您可以将 Solaris 更新到 237 位或 1001 位版本,但 GCC 将继续生成 32 位代码,直到 GCC 默认值更改或直到您明确请求不同的目标平台。
Firstly, you have to make sure you are running your compiler in 64-bit mode. Some compilers default to 32-bit target platform mode, even if they are capable of generating 64-bit code.
Secondly, some compilers prefer to 64-bit type model where type
int
remains 32-bit. GCC is actually one of them. So, your expectations forint
type becoming 64-bit type in 64-bit mode are completely unfounded.Again, everything depends on the compiler and only on the compiler (and on the compiler settings). What you did to your OS is totally irrelevant. You can update your Solaris to 237-bit or 1001-bit version, but GCC will continue to generate 32-bit code, until the GCC default is changed or until you explicitly request a different target platform.
如果您想查看“long”类型(在 32 位架构上为 32 位,在 64 位架构上为 64 位,而不是始终为 32 位的“int”),您可以打印
:我用“-m64”编译时是这个
,用“-m32”编译时是这个
If you want to see the "long" type (which is 32bits on 32-bit architecture and 64-bits on 64-bit architecture, as opposed to "int" which is always 32-bit), you could have printed:
that gives me this when compiled with '-m64'
and this when compiled with '-m32'