Xcode linking against static and dynamic library

*爱你&永不变心* 提交于 2021-02-07 08:07:33

问题


I have some problems with linking my macOS app against C libraries. 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. 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/<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"

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. 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.

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.

UPDATE

  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?

回答1:


Finally, I have linked this Xcode macOS project with multiple dynamic C libraries (.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/*.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.

DYNAMIC C LIBRARIES LINKIN IN XCODE

  1. Add dynamic libraries to Build Phases tab and Link Binaries with Libraries section as on the image

  2. Embed all this dynamic libraries in output 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




回答2:


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

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

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 ...

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

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

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 ...

Which linked the libraries statically.



来源:https://stackoverflow.com/questions/45699389/xcode-linking-against-static-and-dynamic-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!