简体   繁体   中英

loading dynamic library path error in mac

I am now building a dynamic library and an command line illustration program that uses this dynamic library. The library and the illustration program are in the same folder:

/user/xxx/develop/debug/libdynamic.dylib
/user/xxx/develop/debug/illustration

When the illustration program can work very well in my computer, I send the illustration program as well as the dynamic library to my colleague and he will run the illustration program in his computer. However, every time he runs the illustration program in the command window, the program also reminds that it can not load the libdynamic.dylib as it tries to find the library in /user/xxx/develop/debug/ , which is unavailable in my colleague's computer. I was wondering what I should do. Many thanks.

EDIT: The output using otool for the illustration program is as follows:

 /Users/xxx/develop/debug/libdynamic.dylib (compatibility version 0.0.0, current version 0.0.0)

    /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 744.18.0)

    /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 56.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)

You need to tell illustration where to find libdynamic.dylib which you can do post-build using install_name_tool ( manpage ). You'll want to set the new path to @executable_path/libdynamic.dylib , with (something like):

$ install_name_tool -change /user/xxx/develop/debug/libdynamic.dylib \
  @executable_path/libdynamic.dylib \
  /user/xxx/develop/debug/illustration

(the exact old name value to pass to install_name_tool will depend on what it's currently set to, which can be determined using otool -L /user/xxx/develop/debug/illustration ).

One way to avoid this nonsense (and the way I do it myself) is to use the -install_name linker option:

$(BINDIR)/libdynamic.dylib: $(OBJS)
    $(CXX) -dynamiclib -current_version $(MAJOR_MINOR_VERSION) \
        -compatibility_version $(MAJOR_MINOR_VERSION) \
        -install_name @executable_path/libdynamic.dylib \
        $(LDFLAGS) -o $@ $(OBJS) $(LIBS)

( Makefile fragment taken from here ).

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