如何在 OS X 上进行静态链接 [英] How to static link on OS X

查看:13
本文介绍了如何在 OS X 上进行静态链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试链接到 OS X 上的静态库.我在 gcc 命令中使用了 -static 标志,但收到以下错误消息:

<上一页>ld_classic:找不到文件:-lcrt0.ocollect2: ld 返回 1 个退出状态

我查看了手册页,内容如下:

<块引用>

除非所有库(包括 libgcc.a)也使用 -static 编译,否则此选项在 Mac OS X 上不起作用.由于既没有提供静态版本的 libSystem.dylib 也没有提供 crt0.o,因此该选项对大多数人来说没有用处.

还有其他方法可以链接到这个静态库吗?

解决方案

为了链接到存档库(有时也称为静态库),只需将其添加到链接行:

gcc main.o ... -lfoo ...

链接器将搜索 libfoo.dylib,然后搜索 libfoo.a,这就是您所需要的.

如果您有 两个 版本的库,并且想要链接存档版本而不是动态版本,只需在链接行指定存档的完整路径:

gcc main.o .../path/to/libfoo.a ...

I'm trying to link to a static library on OS X. I used the -static flag in the gcc command but I get the following error message:

ld_classic: can't locate file for: -lcrt0.o
collect2: ld returned 1 exit status

I looked in the man pages and it reads something like:

This option will not work on Mac OS X unless all libraries (including libgcc.a) have also been compiled with -static. Since neither a static version of libSystem.dylib nor crt0.o are provided, this option is not useful to most people.

Is there another way to link to this static library?

解决方案

In order to link to an archive library (sometimes also called static library), just add it to the link line:

gcc main.o ... -lfoo ...

The linker will search for libfoo.dylib, and then libfoo.a, which is all you need.

If you have both versions of the library, and want to link with an archive version in preference of the dynamic one, just specify the full path to the archive on the link line:

gcc main.o ... /path/to/libfoo.a ...

这篇关于如何在 OS X 上进行静态链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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