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

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

问题描述

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

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:

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

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

链接器将先搜索libfoo.dylib,然后搜索libfoo.a.

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天全站免登陆