4
votes

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?

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.

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.

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

2 Answers

4
votes

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 enter image description here

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

enter image description here

  1. And finally add Runtime Search Paths in Build Settings

enter image description here

4
votes

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.