Xcode 链接静态和动态库

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

Xcode linking against static and dynamic library

c++xcodestatic-librariesdylibdynamic-library

提问by Micha? Ziobro

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

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

  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. I have reconfigured CMakeLists.txt making this libraries and now Xcode project doesn't work. The main change was changing the directory I have

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

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

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

"$(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"

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

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.dylibSo maybe it finds this dynamic library under Library Search Paths and tires to load them but don't find them linked?

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

enter image description here

在此处输入图片说明

  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. The problem is that the error not finding library now also occurs.
  1. 所以我厌倦了将 Xcode macOS 应用程序与 .dylib 库链接起来,而不是静态的 .a 库将它们添加到 Link Binary with Libraries 中。问题是现在也出现了找不到库的错误。

enter image description here

在此处输入图片说明

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 包中包含动态库以便始终可以找到。

enter image description here

在此处输入图片说明

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 .dylibit 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 .ait works when .dylibisn't in the same directory (I moved .alibrary into /staticsubdirectory) and then for this moved library error isn't showing any more. But isn't there way to link statically when there is .aand .dyliblibraries inside the same directory?
  1. 似乎.dylib只有当我将此库目录的路径添加到Runpath Search Paths.
  2. 似乎当我链接到静态库时,.a它在.dylib不在同一目录中时起作用(我将.a库移到/static子目录中),然后不再显示此移动的库错误。但是当同一个目录中有.a.dylib库时,难道没有办法静态链接吗?

回答by Avi Tevet

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.

其中静态链接库。

回答by Micha? Ziobro

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! path/project_name/outputs/lib/apple/*.dyliband then place static libs under path: path/project_name/outputs/lib/apple/static/.aAs XCode tries to link dynamic libraries if they found them on Libraries Search Pathin Build Settings.

为了与静态库链接 (.a) 它们不能与动态库并排放置!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 enter image description here

  2. Embed all this dynamic libraries in output macOS Application Wrapper

  1. 将动态库添加到 Build Phases 选项卡和 Link Binaries with Libraries 部分,如图所示 在此处输入图片说明

  2. 将所有这些动态库嵌入到输出 macOS Application Wrapper 中

enter image description here

在此处输入图片说明

  1. You get something like this: enter image description here

  2. Then in Build setting add Libraries Search Paths

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

  2. 然后在构建设置中添加库搜索路径

enter image description here

在此处输入图片说明

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

enter image description here

在此处输入图片说明