Xcode linking against static and dynamic library - c++

I have some problems with linking my macOS app against C libraries.
I have several question related to this issue.
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?
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?
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
It seems that when linking against .dylib it only works when I add path to this library directory to Runpath Search Paths.
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?

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.

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
Add dynamic libraries to Build Phases tab and Link Binaries with Libraries section as on the image
Embed all this dynamic libraries in output macOS Application Wrapper
You get something like this:
Then in Build setting add Libraries Search Paths
And finally add Runtime Search Paths in Build Settings

Related

XCode - C++ Static & Dynamic library linking

I compiled a program by statically linking external libraries on XCode. However, I am having errors on how to dynamically link those libraries.
For Static Linking, I did
1. Went to Build Phases, Link Binary with Libraries, Chose the static .a libraries
2. Went to Build settings and modified the Header Search path to the header files directory
3. Modified the Library search path to the static library directory.
I compiled the program and it works.
Then I am trying to dynamic linking
1. Went to Build Phases, Link Binary with Libraries, removed the static .a libraries and added the .dylib libraries (they are in the same directory)
2. Tried compiling - But I am getting 'Library not loaded:#loader_path/(lib name).dylib.. Reason: image not found'.
Am I missing one of the paths? What am I doing wrong?
Solved it by adding DYLD_LIBRARY_PATH environment variable.
Make sure that dynamic libraries are not in the same directory as the static libraries. This creates a conflict. I created a separate directory and copied all the dynamic libraries I needed to that directory
Went to Build Phases, Link Binary with Libraries, Chose the dynamic libraries I needed
Went to Build settings and modified the Header Search path to the header files directory
Modified the Library search path to the dynamic library directory.
Created an environment variable by (menu) Product -> Scheme -> Edit Scheme. Under 'Environment Variables', created a 'DYLD_LIBRARY_PATH' variable and pointed it to my dynamic library directory.
It works :)

dyld: Library not loaded: on OS X

I'm building with g++ from the command line in OS X, and not using XCode. When I built my executable I had to include the linker flag to the library where the dynamic library was stored, but when I went to run the executable the dynamic library was not found, and I got the error dyld: Library not loaded: (foo).
There were a few questions on here already about dynamic libraries related to specific software packages and how to get dynamic libraries built into XCode projects, but nothing generic explaining how Mac OS X looks for dynamic libraries in general and how to get a new .dylib into the search path for the executable.
Could someone fill in that gap. How does Mac OS X look for dynamic libraries when one is built with an executable and how would I put a link to my dynamic library in the right place so the executable will run?
I'm on OSX 10.9 - Mavericks.
A good place to start would be the Apple documentation on Dynamic Library Programming Topics
and especially Run-Path Dependent Libraries.
"When the dynamic loader (dyld) loads the executable, it looks for run-path dependent libraries in the run-path search paths in the order in which they were specified at link time."
The first think is to check the libraries dependencies for you binary using otool:
otool -L /path/to/binary
Then, you have different solutions depending on how you want to distribute your library.
You can specify the absolute path of the library in the binary. It should be done with a tool, CMake for instance. Otherwise, you can use
install_name_tool to change the path to a library by hand.
Most of the time, you want to deploy your binary and put the dylibs in a separated folder. Those libraries are loaded relatively to where the binary is.
For example:
#loader_path/../Frameworks/Sparkle.framework/Versions/A/Sparkle
For more info about paths, I suggest you read this blog.

Error building Static Library with OpenCV and Xcode on C++

I'm trying to build a DLL library for Unity3D (in 32-bit) but first of all I want to make a .a static library to make a C++ Wrapper for C#.
Well, this is what I've done.
I've built OpenCV unchecking BUILD_SHARED_LIBS in order to get the STATIC LIBS, with architecture i386.
I've made my static library code and built it, Linking the static libraries that I needed (I really import them all because I'm having errors and I want to know if it's an error lib-based):
(included from /lib/ and /3rdparty/lib/)
I'm linking my library and header in a new project (Command Line Tool) in order to test my new static library, but my errors are:
(pastebin link)
Well, I have no idea what's wrong.
This is my settings on the static library:
This is my settings on my test Command Line Tool (for testing my library)
Does anyone have idea about this?
Thank you very much in advance.
Regards.
EDIT: tried to manipulate the static libraries for 64 bits as well but nothing happened.
The solution was removing several paths on the Library paths since it was messing up some libraries.

I got an error library not found , if library dosnt exists under usr/lib

I have 3rd parties libraries that don't placed under "/usr/lib".
I defined their path in eclipse library search path .
The project compile and linkage well, but when i run project i got exception that library doesn't found.
if i copy the 3rd party library to "/usr/lib" than it run ok.
I believe that this is path issue (i am new to cpp), how do i configure this in eclipse ?
Thank you
First of all, since it's crashing during runtime, you are linking against dynamic libraries (libWhatever.so), so you have to add your library path to the LD_LIBRARY_PATH environment variable. Otherwise, you could force the linker to link statically to static libraries (libWhatever.a), using the -static flag.

eclipse sfml library issues

I pulled out an application that I wrote in C++ using the sfml library, but I'm having trouble setting up the library in Eclipse. I specified the include path, the lib path and included all the necessary .so libraries to link to. the application compiles fine but it complains at runtime about missing libraries. Why is this happening? Didn't I include the path to the libraries in the project settings already? I have even tried to place all the .so's in the executable directory with no luck.
There is only the name of the shared lib stored in the executable. At program startup the dynamic linker then searches for the specified libs in its search paths. You can add/specify search paths by placing them colon separated in the environment variable LD_LIBRARY_PATH or by specifying them in /etc/ld.so.conf (at least if you use some unix based OS). On windows the whole PATH environment variable is used when searching for dynamic-link libraries (DLL).
To see the paths of shared libraries used by a given application run ldd applicationPath.