简体   繁体   English

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

[英]Force static linking of library linked to Xcode target?

My Xcode target links against hdf5 library (using the Link Binary with Libraries build phase).我的 Xcode 目标链接到 hdf5 库(使用链接二进制与库构建阶段)。 libhdf5 is installed using MacPorts, thus /opt/local/lib contains both the dynamic (.dylib) and static (.a) versions of the library. libhdf5 使用 MacPorts 安装,因此 /opt/local/lib 包含库的动态 (.dylib) 和静态 (.a) 版本。

The text output from the build shows that there is, as expected, a -lhdf5 in the linking step of the build.构建的文本输出显示,正如预期的那样,在构建的链接步骤中有一个-lhdf5 gcc seems to take the dynamic linked library over the static, however.然而,gcc 似乎将动态链接库置于静态之上。 Is there any way to force gcc (via a compiler switch or via Xcode) to statically link with libhdf5.a?有没有办法强制 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 复制到项目(或其他)目录并链接到该副本,从而避免在同一位置具有动态和静态版本。

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.通常,您只是向链接器提供开关“-static”,但是,使用 Xcode 这会导致包括 crt 在内的所有库都被静态链接。 I got the error:我得到了错误:

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 被删除。

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

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 -l flags:当我将静态 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 .链接失败,因为没有/path/to/libfoodir/libfoo.dylib The linker command can be seen from:链接器命令可以从:

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

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

Under XCode 11.3.1, targeting MacOS在 XCode 11.3.1 下,针对 MacOS

Tried to add absolute lib path to Other linker flags .试图将绝对库路径添加到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.所以我将所需的 *.a 文件(ln -s 也可以工作)复制到项目目录中,它的作用就像一个魅力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM