macos Mac OSX 10.6 编译器:32 位与 64 位的令人费解的体验

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1455720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:25:23  来源:igfitidea点击:

Mac OSX 10.6 compiler: a puzzling experience with 32 vs 64 bit

macosgccosx-snow-leopard32bit-64bit

提问by Stefano Borini

I have trouble understanding the gcc compiler provided by OSX 10.6 snow leopard, mainly because of my lack of experience with 64 bits environments.

我对 OSX 10.6 雪豹提供的 gcc 编译器理解有困难,主要是因为我缺乏 64 位环境的经验。

$ cat >foo.c
main() {}
$ gcc foo.c -o foo
$ file foo
foo: Mach-O 64-bit executable x86_64
$ lipo -detailed_info foo
input file foo is not a fat file
Non-fat file: foo is architecture: x86_64

However, my architecture is seen as an intel i386 type (I have one of the latest Intel Core2 duo MacBook)

但是,我的架构被视为 intel i386 类型(我有最新的 Intel Core2 duo MacBook 之一)

$ arch
i386

and the compiler targets i686-apple-darwin10

并且编译器针对 i686-apple-darwin10

$ gcc --version 
i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5646)

Of course, if I compile 32 bits I get a 32 bit executable.

当然,如果我编译 32 位,我会得到一个 32 位的可执行文件。

$ gcc -m32 foo.c -o foo
$ file foo
foo: Mach-O executable i386

but I don't get the big picture. The default setup for the compiler is to produce x86_64 executables, even if I have arch saying I have a 32 bit machine (why? Core2 is 64); even if (I guess) I am running a 32 bit kernel; even if I have a compiler targeting the i686-apple-darwin platform. Why? How can they run ? Should I compile 64 or 32 ?

但我不明白大局。编译器的默认设置是生成 x86_64 可执行文件,即使我有 arch 说我有一台 32 位机器(为什么?Core2 是 64);即使(我猜)我运行的是 32 位内核;即使我有一个针对 i686-apple-darwin 平台的编译器。为什么?他们怎么跑?我应该编译 64 还是 32 ?

This question is due to my attempt to compile gcc 4.2.3 on the mac, but I am having a bunch of issues with gmp, mpfr and libiberty getting (in some cases) compiled for x86_64. Should I compile everything x86_64 ? If so, what's the target (not i686-apple-darwin10 I guess)?

这个问题是由于我试图在 mac 上编译 gcc 4.2.3,但是我在为 x86_64 编译 gmp、mpfr 和 libiberty 时遇到了一堆问题(在某些情况下)。我应该编译所有 x86_64 吗?如果是这样,目标是什么(我猜不是 i686-apple-darwin10)?

Thanks for the help

谢谢您的帮助

回答by cdespinosa

The default compiler on Snow Leopard is gcc4.2, and its default architecture is x86_64. The typical way to build Mac software is to build multiple architectures in separate passes, then use lipo to combine the results. (lipo only compiles single-arch files into a multiple-arch file, or strips archs out of a multi-arch file. It has no utility on single-arch files, as you discovered.)

Snow Leopard 上的默认编译器是 gcc4.2,其默认架构是 x86_64。构建 Mac 软件的典型方法是在单独的通道中构建多个架构,然后使用 lipo 组合结果。(lipo 仅将单架构文件编译为多架构文件,或从多架构文件中剥离架构。正如您所发现的,它对单架构文件没有实用程序。)

The bitness of the compiler has nothing to do with anything. You can build 32-bit binaries with a 64-bit compiler, and vice versa. (What you think is the "target" of the compiler is actually its executable, which is different.)

编译器的位数与任何事情无关。您可以使用 64 位编译器构建 32 位二进制文​​件,反之亦然。(你认为编译器的“目标”实际上是它的可执行文件,这是不同的。)

The bitness of the kernel has nothing to do with anything. You can build and run 64-bit binaries when booted on a 32-bit kernel, and vice versa.

内核的位数与任何事情无关。在 32 位内核上启动时,您可以构建和运行 64 位二进制文​​件,反之亦然。

What matters is when you link, whether you have the appropriate architectures for linking. You can't link 32-bit builds against 64-bit binaries or vice versa. So the important thing is to see what the architectures of your link libraries are, make sure they're coherent, then build your binary of the same architecture so you can link against the libraries you have.

重要的是何时进行链接,是否有合适的链接架构。您不能将 32 位版本与 64 位二进制文​​件链接起来,反之亦然。所以重要的是看看你的链接库的架构是什么,确保它们是连贯的,然后构建相同架构的二进制文件,这样你就可以链接到你拥有的库。

回答by Eamonn Kenny

i686-apple-darwin10.0.0 contains an x86_64 folder which is not understood by most versions of autotools. In other words, I'd say that the gcc compiler is unfortunately nothing short of a joke on Snow Leopard. Why you would bundle 32-bit and 64-bit libraries into i686-apple-darwin10.0.0 is beyond me.

i686-apple-darwin10.0.0 包含一个 x86_64 文件夹,大多数版本的 autotools 无法识别该文件夹。换句话说,不幸的是,我会说 gcc 编译器简直就是雪豹的笑话。我无法理解为什么要将 32 位和 64 位库捆绑到 i686-apple-darwin10.0.0 中。

$ ls /usr/lib/gcc
i686-apple-darwin10 powerpc-apple-darwin10

You need to change all your autotools configure files to handle looking in *86-darwindirectories and then looking for 64-bit libraries I'd imagine.

您需要更改所有 autotools 配置文件以处理在 *86- darwin目录中的查找,然后查找我想象的 64 位库。

As with your system, my mac mini says its i386 even though its obviously using a 64-bit platform, again another mistake since its distributed with 64-bit hardware.

和你的系统一样,我的 mac mini 说它的 i386 即使它显然使用的是 64 位平台,这又是一个错误,因为它是用 64 位硬件分发的。

$arch
i386

回答by Sven

Apple toolchains support multiple architectures. If you want to create a fat binary that contains x86 and x86_64 code, then you have to pass the parameters -arch i386 -arch x86_64to gcc. The compiler will compile your code twice for both platforms in one go.

Apple 工具链支持多种架构。如果要创建包含 x86 和 x86_64 代码的胖二进制文件,则必须将参数传递-arch i386 -arch x86_64给 gcc。编译器将一次性为两个平台编译两次代码。

Adding -arch i386 -arch x86_64to CFLAGS may allow you to compile gmp, mpfr, and whatnot for multiple archs in one go. Building libusb that way worked for me.

添加-arch i386 -arch x86_64到 CFLAGS 可能允许您一次性为多个拱形编译 gmp、mpfr 等。以这种方式构建 libusb 对我有用。

回答by Powerlord

This answer is wrong, but see comments below

这个答案是错误的,但请参阅下面的评论

The real question is... how did you get a 32-bit version of OSX? I wasn't aware that Snow Leopard hada 32-bit version, as all of Apple's Intel chips are Core 2 or Xeon, which support the x86_64 architecture.

真正的问题是……你是如何获得 32 位版本的 OSX 的?我不知道 Snow Leopard32 位版本,因为 Apple 的所有 Intel 芯片都是 Core 2 或 Xeon,支持 x86_64 架构。

Oh, and Snow Leopard only works on Intel chips.

哦,雪豹只适用于英特尔芯片。

Edit: Apparently Snow Leopard starts in 32-bit mode.

编辑:显然雪豹以 32 位模式启动。