Antzi
Antzi

Reputation: 13414

Dylib ok at link time but not at runtime

I have a problem linking correctly my project. The project is built with CMAKE. Linking seems fine, but at run time an error is thrown.

Here is the command that was used for linking:

$ /usr/bin/clang -Wl,-search_paths_first -Wl,-headerpad_max_install_names  \
      CMakeFiles/project.dir/src/conf.c.o CMakeFiles/project.dir/src/tun-compat.c.o \
      CMakeFiles/project.dir/src/compress.c.o CMakeFiles/project.dir/src/mc.c.o \
      CMakeFiles/project.dir/src/hexdump.c.o CMakeFiles/project.dir/src/server.c.o \
      CMakeFiles/project.dir/sys/unix/log.c.o CMakeFiles/project.dir/sys/unix/imsg.c.o \
      CMakeFiles/project.dir/ sys/unix/imsg-buffer.c.o CMakeFiles/project.dir/sys/unix/toto.c.o \
      CMakeFiles/project.dir/sys/unix/toto.c.o CMakeFiles/project.dir/sys/unix/util.c.o \
      CMakeFiles/project.dir/sys/unix/conf.c.o CMakeFiles/project.dir/sys/unix/tntsocket.c.o \
      -o bin/project/opt/local/lib/libevent_openssl.dylib /opt/local/lib/libevent_core.dylib \
      /usr/lib/libz.dylib /opt/local/lib/libyajl.dylib /opt/local/lib/libtapcfg.dylib  

The error I get:

$ ./bin/project                                                                                                                                                                                             
dyld: Library not loaded: build/libtapcfg.dylib                                                                                                                                                             
  Referenced from: /Users/Antoine/project/./bin/project                                                                                                                                                     
  Reason: image not found                                                                                                                                                                                   
zsh: trace trap  ./bin/project                                                                                                                                                                              

Additional infos:

 $ dyldinfo -dylibs bin/projectattributes                                                                                                                                                            
         dependent dylibs                                                                                                                                                                                        
                        /opt/local/lib/libevent_openssl-2.0.5.dylib                                                                                                                                         
                        /opt/local/lib/libevent_core-2.0.5.dylib                                                                                                                                                                    
                        /usr/lib/libz.1.dylib                                                                                                                                                               
                        /opt/local/lib/libyajl.2.dylib                                                                                                                                                      
                        build/libtapcfg.dylib                                                                                                                                                               
                        /usr/lib/libSystem.B.dylib                                                                                                                                                          

It looks like black magic to me. The linker is able to find the symbols, but end up by changing the path of the library even if I can't see any difference in the way it is handled in the linking command...

The lib location is /opt/local/lib/libtapcfg.dylib

$ ls -lhF /opt/local/lib/libtapcfg.dylib                                                                                                                                                                    
-rwxr-xr-x  1 root  admin    20K 14 jui 18:05 /opt/local/lib/libtapcfg.dylib*                                                                                                                               

Does the problem maybe come from the lib I am linking to?

Upvotes: 0

Views: 2265

Answers (1)

mmmmmm
mmmmmm

Reputation: 32661

The issue is with how you built and installed libtapcfg.

You seem to have copied it from your local directory to /opt/local/lib and thus its file system path does not match what is stored in the library.

Either leave the lib in your local path or when building it tell the linker where it will end up or copy to /usr/local/lib which is hard coded in the linker. See Apple developer's note on dynamic libraries

You should only put things in /opt/local when using macports by means of a port. In this case create a local portfile which does not have to do much and macports standard work will put the correct information in the library to make it work from /opt/local/lib

Upvotes: 1

Related Questions