强制静态链接链接到 Xcode 目标的库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/458805/
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-09-14 18:27:53  来源:igfitidea点击:

Force static linking of library linked to Xcode target?

xcodegcclinker

提问by Barry Wark

My Xcode target links against hdf5 library (using the Link Binary with Libraries build phase). libhdf5 is installed using MacPorts, thus /opt/local/lib contains both the dynamic (.dylib) and static (.a) versions of the library.

我的 Xcode 目标链接到 hdf5 库(使用链接二进制与库构建阶段)。libhdf5 是使用 MacPorts 安装的,因此 /opt/local/lib 包含库的动态 (.dylib) 和静态 (.a) 版本。

The text output from the build shows that there is, as expected, a -lhdf5in the linking step of the build. gcc seems to take the dynamic linked library over the static, however. Is there any way to force gcc (via a compiler switch or via Xcode) to statically link with libhdf5.a?

构建的文本输出显示,正如预期的那样,-lhdf5构建的链接步骤中有 a。然而,gcc 似乎将动态链接库置于静态之上。有没有办法强制 gcc(通过编译器开关或通过 Xcode)与 libhdf5.a 静态链接?

The only solution I've found is to copy libhdf5.a to the project (or other) directory and link against that copy, thus avoiding having dynamic and static versions in the same location.

我找到的唯一解决方案是将 libhdf5.a 复制到项目(或其他)目录并链接到该副本,从而避免在同一位置具有动态和静态版本。

采纳答案by yungchin

In reaction to your comment on Eduard Wirch' answer: you can also control static linking for this one library only, if you replace -lhdf5 by -l/full/path/to/libhdf5.a

回应您对 Eduard Wirch 回答的评论:如果您将 -lhdf5 替换为 -l/full/path/to/libhdf5.a,您还可以仅控制该库的静态链接

回答by user500515

Had this exact same problem and despite this being an old post, I thought I'd share what I had to do to make this work.

遇到了完全相同的问题,尽管这是一篇旧帖子,但我想我会分享我必须做的事情才能完成这项工作。

Usually you do just provide the switch '-static' to the linker however, with Xcode this causes all libs including the crt to be linked statically. I got the error:

通常,您只是向链接器提供开关“-static”,但是,对于 Xcode,这会导致包括 crt 在内的所有库都静态链接。我收到错误:

can't locate file for: -lcrt0.o

When I tried this.

当我尝试这个时。

The thing which worked for me was to replace:

对我有用的是替换:

-lmylib

with

/path/to/libmylib.a

/path/to/libmylib.a

Note: the -l is dropped.

注意: -l 被删除。

回答by user272735

My case with Xcode 4.5:

我的 Xcode 4.5 案例:

When I drag and drop a static C library (a 3rd party library compiled with GNU Autotools) to project's frameworks (Project Navigator > Frameworks) the linker seems to thinks that's a dynamic library and adds -L -lflags:

当我将静态 C 库(使用 GNU Autotools 编译的第 3 方库)拖放到项目的框架 ( Project Navigator > Frameworks) 时,链接器似乎认为这是一个动态库并添加了-L -l标志:

-L/path/to/libfoodir -lfoo

The linking fails because there is no /path/to/libfoodir/libfoo.dylib. The linker command can be seen from:

链接失败,因为没有/path/to/libfoodir/libfoo.dylib. 链接器命令可以从:

Log Navigator > select a Build log > select a Link line and expand it

The linking succeeds when I add a full path (/path/to/libfoodir/libfoo.a) to the linker settings:

当我/path/to/libfoodir/libfoo.a向链接器设置添加完整路径 ( )时,链接成功:

Targets > Build Settings (all) > Other linker flags

回答by Eduard Wirch

Use the "-static" switch for linking: GCC link options

使用“-static”开关进行链接: GCC 链接选项

回答by zwlxt

Under XCode 11.3.1, targeting MacOS

在 XCode 11.3.1 下,针对 MacOS

Tried to add absolute lib path to Other linker flags. But that doesn't work. So I copied required *.a files (ln -s may also work) to project dir and it worked like a charm.

试图将绝对 lib 路径添加到Other linker flags. 但这不起作用。所以我将所需的 *.a 文件(ln -s 也可以工作)复制到项目目录中,它的作用就像一个魅力。