Xcode 链接静态和动态库 [英] Xcode linking against static and dynamic library

查看:109
本文介绍了Xcode 链接静态和动态库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将我的 macOS 应用程序与 C 库链接时遇到了一些问题.我有几个与此问题相关的问题.

I have some problems with linking my macOS app against C libraries. I have several question related to this issue.

  1. 考虑到这将是非常自定义的库而不是与其他应用共享,最好将应用与动态或静态库链接?

  1. It is better to link app against dynamic or static libraries taking into account that this will be very custom libraries rather not shared with other apps?

我已将我的 macOS Xcode 应用程序与约 14 个静态库 .a 相关联,并且运行良好.我已经重新配置了 CMakeLists.txt 来制作这个库,现在 Xcode 项目不起作用.主要的变化是改变我拥有的目录

I have linked my macOS Xcode app against ~14 static libraries .a and it works fine. I have reconfigured CMakeLists.txt making this libraries and now Xcode project doesn't work. The main change was changing the directory I have

"$(SRCROOT)/../../c//outputs/lib/apple/static"

但现在我在同一路径中同时拥有静态 (.a) 和动态 (.dylib) 库"$(SRCROOT)/../../c/server/outputs/lib/apple"

But now I have both static (.a) and dynamic (.dylib) libraries in the same path "$(SRCROOT)/../../c/server/outputs/lib/apple"

我不知道这是否重要,但是链接静态库会导致在运行我的 Xcode 项目后它抱怨无法加载 lib.dylib 所以也许它在 Library 下找到了这个动态库搜索路径和轮胎以加载它们但没有找到它们链接?

I don't know whether this should matter, but linking against static libraries causes that after running my Xcode project it complains that it cannot load lib.dylib So maybe it finds this dynamic library under Library Search Paths and tires to load them but don't find them linked?

  1. 所以我厌倦了将 Xcode macOS 应用程序链接到 .dylib 库,而不是静态 .a 库将它们添加到 Link Binary with Libraries 中.问题是现在也出现了没有找到库的错误.

也许我应该在这里改变一些东西?但是,如果我将我的应用程序分发到其他一些在此特定位置没有库的计算机会怎样.我如何在 Xcode 包中包含动态库以便始终可以找到.

Maybe I should change something here ? But what If I will distribute my app to some other computers that will not have libraries in this specific location. How can I include dynamic libraries in Xcode bundle in order to be always findable.

我知道我可能添加了很多问题.但是想知道如何最好地解决这个问题?最好静态或动态链接,然后如何正确实现这一点,避免此错误.

I know I added maybe to many question. But would like to know how to the best solve this issue? Better to link statically or dynamically and then how to correctly achieve this avoiding this error.

更新

  1. 似乎当链接到 .dylib 时,它仅在我将此库目录的路径添加到 Runpath Search Paths 时才有效.
  2. 似乎当我链接到静态库 .a 时,它在 .dylib 不在同一目录中时起作用(我移动了 .a 库进入 /static 子目录),然后对于这个移动的库错误不再显示.但是,当同一目录中有 .a.dylib 库时,难道没有办法静态链接吗?
  1. It seems that when linking against .dylib it only works when I add path to this library directory to Runpath Search Paths.
  2. It also seems that when I link against static library .a it works when .dylib isn't in the same directory (I moved .a library into /static subdirectory) and then for this moved library error isn't showing any more. But isn't there way to link statically when there is .a and .dylib libraries inside the same directory?

推荐答案

我知道这是一个老问题,但它是在谷歌搜索Xcode 静态链接"时的最佳结果之一.

I know this is an old question but it's one of the top results when searching google for "Xcode static linking."

我最近在与英特尔 IPP 集成时遇到了这个问题,它将静态和动态库放在同一目录中.

I recently encountered this issue when integrating with Intel IPP, which puts static and dynamic libs in the same directory.

如果我使用标准 Xcode 链接方法通过构建阶段 | 将二进制文件与库链接"来添加库,Xcode 会将 UI 转换为如下所示的命令行:

If I used the standard Xcode linking method of adding the library via the "Build Phases | Link Binary with Libraries," Xcode translated that UI into a command line that looked like this:

clang++ ... -L/my/path -lstatic1 -lstatic2 ...

但这会导致链接器更喜欢 DLL 而不是同一目录中的静态库.

But that causes the linker to prefer the DLL instead of the static library in the same directory.

我通过从Build Phases | Link Binary with Libraries"窗口中删除条目,并在Build Settings | Other linker flags"条目中添加库的完整相对路径来解决此问题:

I worked around this by removing the entries from the "Build Phases | Link Binary with Libraries" window, and adding full relative paths to the libraries in the "Build Settings | Other linker flags" entry:

../../path/to/lib/libstatic1.a ../../path/to/lib/libstatic2.a

这导致 Xcode 将 UI 转换为如下所示的命令行:

This caused Xcode to translate the UI into a command line that looked like this:

clang++ ... ../../path/to/lib/libstatic1.a ../../path/to/lib/libstatic1.a ...

静态链接库.

这篇关于Xcode 链接静态和动态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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