Mac OSX 10.6 编译器:32 位与 64 位的令人困惑的体验

发布于 2024-08-06 05:03:26 字数 975 浏览 6 评论 0原文

我无法理解 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

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

$ arch
i386

,编译器目标为 i686-apple-darwin10

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

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

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

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

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

感谢您的帮助

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.

$ 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)

$ arch
i386

and the compiler targets 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.

$ 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 ?

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)?

Thanks for the help

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

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

发布评论

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

评论(4

勿挽旧人 2024-08-13 05:03:26

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

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

内核的位数与任何东西都没有关系。当在 32 位内核上启动时,您可以构建并运行 64 位二进制文​​件,反之亦然。

重要的是当你链接时,你是否有适当的链接架构。您无法将 32 位构建链接到 64 位二进制文​​件,反之亦然。因此,重要的是查看链接库的架构是什么,确保它们是一致的,然后构建相同架构的二进制文件,以便您可以链接到您拥有的库。

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.)

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.)

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.

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.

梦纸 2024-08-13 05:03:26

i686-apple-darwin10.0.0 包含一个 x86_64 文件夹,大多数版本的 autotools 都无法理解该文件夹。换句话说,不幸的是,我想说 gcc 编译器只不过是 Snow Leopard 上的一个笑话。为什么要将 32 位和 64 位库捆绑到 i686-apple-darwin10.0.0 中,我无法理解。

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

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

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

$arch
i386

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.

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

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

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.

$arch
i386
久随 2024-08-13 05:03:26

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

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

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_64 to gcc. The compiler will compile your code twice for both platforms in one go.

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

情丝乱 2024-08-13 05:03:26

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

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

哦,还有Snow Leopard 仅适用于 Intel 芯片。

编辑:显然 Snow Leopard 以 32 位模式启动。

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 had a 32-bit version, as all of Apple's Intel chips are Core 2 or Xeon, which support the x86_64 architecture.

Oh, and Snow Leopard only works on Intel chips.

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

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