简体   繁体   English

Xcode 链接静态和动态库

[英]Xcode linking against static and dynamic library

I have some problems with linking my macOS app against C libraries.我在将 macOS 应用程序与 C 库链接时遇到了一些问题。 I have several question related to this issue.我有几个与此问题相关的问题。

  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?考虑到这将是非常自定义的库而不是与其他应用程序共享,最好将应用程序与动态或静态库链接起来?

  2. I have linked my macOS Xcode app against ~14 static libraries .a and it works fine.我已将我的 macOS Xcode 应用程序与约 14 个静态库 .a 相关联,并且工作正常。 I have reconfigured CMakeLists.txt making this libraries and now Xcode project doesn't work.我已经重新配置了 CMakeLists.txt 来制作这个库,现在 Xcode 项目不起作用。 The main change was changing the directory I have主要的变化是改变我的目录

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

But now I have both static (.a) and dynamic (.dylib) libraries in the same path "$(SRCROOT)/../../c/server/outputs/lib/apple"但是现在我在同一路径"$(SRCROOT)/../../c/server/outputs/lib/apple"同时拥有静态(.a)和动态(.dylib)库

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?我不知道这是否重要,但是链接静态库会导致在运行我的 Xcode 项目后它抱怨它无法加载lib.dylib所以也许它在库搜索路径下找到了这个动态库,并且轮胎加载它们但不没有找到它们之间的联系?

在此处输入图片说明

  1. So I tired to link Xcode macOS app against .dylib libraries and instead of static .a libraries added them in Link Binary with Libraries.所以我厌倦了将 Xcode macOS 应用程序链接到 .dylib 库,而不是静态的 .a 库将它们添加到 Link Binary with Libraries 中。 The problem is that the error not finding library now also occurs.问题是现在也出现了找不到库的错误。

在此处输入图片说明

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.我如何在 Xcode 包中包含动态库以便始终可以找到。

在此处输入图片说明

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.最好静态或动态链接,然后如何正确实现这一点,避免此错误。

UPDATE更新

  1. It seems that when linking against .dylib it only works when I add path to this library directory to Runpath Search Paths .似乎当链接到.dylib它仅在我将此库目录的路径添加到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.似乎当我链接静态库.a它在.dylib不在同一目录中时起作用(我将.a库移动到/static子目录中),然后不再显示此移动的库错误。 But isn't there way to link statically when there is .a and .dylib libraries inside the same directory?但是当同一个目录中有.a.dylib库时,难道没有办法静态链接吗?

Finally, I have linked this Xcode macOS project with multiple dynamic C libraries (.dylib).最后,我将这个 Xcode macOS 项目与多个动态 C 库 (.dylib) 联系起来。

REMARK备注

In order to link with static libraries (.a) They cannot be placed side by side with dynamic libraries!为了与静态库链接 (.a) 它们不能与动态库并排放置! path/project_name/outputs/lib/apple/*.dylib and then place static libs under path: path/project_name/outputs/lib/apple/static/.a As XCode tries to link dynamic libraries if they found them on Libraries Search Path in Build Settings. path/project_name/outputs/lib/apple/*.dylib然后将静态库放在路径下: path/project_name/outputs/lib/apple/static/.a因为 XCode 尝试链接动态库,如果他们在库搜索路径上找到它们在构建设置中。

DYNAMIC C LIBRARIES LINKIN IN XCODE XCODE 中的动态 C 库链接

  1. Add dynamic libraries to Build Phases tab and Link Binaries with Libraries section as on the image将动态库添加到 Build Phases 选项卡和 Link Binaries with Libraries 部分,如图所示在此处输入图片说明

  2. Embed all this dynamic libraries in output macOS Application Wrapper在输出 macOS Application Wrapper 中嵌入所有这些动态库

在此处输入图片说明

  1. You get something like this:你会得到这样的东西: 在此处输入图片说明

  2. Then in Build setting add Libraries Search Paths然后在构建设置中添加库搜索路径

在此处输入图片说明

  1. And finally add Runtime Search Paths in Build Settings最后在构建设置中添加运行时搜索路径

在此处输入图片说明

I know this is an old question but it's one of the top results when searching google for "Xcode static linking."我知道这是一个老问题,但它是在谷歌搜索“Xcode 静态链接”时的最佳结果之一。

I recently encountered this issue when integrating with Intel IPP, which puts static and dynamic libs in the same directory.我最近在与 Intel IPP 集成时遇到了这个问题,它将静态和动态库放在同一目录中。

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

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

But that causes the linker to prefer the DLL instead of the static library in the same directory.但这会导致链接器更喜欢 DLL 而不是同一目录中的静态库。

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:我通过从“Build Phases | Link Binary with Libraries”窗口中删除条目,并在“Build Settings | Other linker flags”条目中添加库的完整相对路径来解决这个问题:

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

This caused Xcode to translate the UI into a command line that looked like this:这导致 Xcode 将 UI 转换为如下所示的命令行:

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

Which linked the libraries statically.其中静态链接库。

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

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