简体   繁体   中英

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.

  1. 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
  2. Went to Build Phases, Link Binary with Libraries, Chose the dynamic libraries I needed
  3. Went to Build settings and modified the Header Search path to the header files directory
  4. 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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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