osx - clang, change dependent shared library install name at link time -


related, not answer question:

on osx, have dynamic library provided packager manager, installed in non standard directory, install_name filename. example:

$ root=$pwd $ mkdir $root/foo  $ cd $root/foo $ echo 'int foo(int a, int b){return a+b;}' > foo.c $ clang foo.c -dynamiclib -install_name libfoo.dylib -o libfoo.dylib 

i don't want change (absolute path, @rpath, ...) install_name of libfoo.dylib using install_name_tool -id.

now link program library, example:

$ mkdir $root/bar $ cd $root/bar $ echo 'int foo(int,int); int main(){return foo(2,4);}' > main.c $ clang main.c -l../foo -lfoo    

the program can't run:

$ ./a.out dyld: library not loaded: libfoo.dylib   referenced from: $root/bar/./a.out   reason: image not found trace/bpt trap: 5 

because:

$ otool -l ./a.out ./a.out:         libfoo.dylib (compatibility version 0.0.0, current version 0.0.0)         /usr/lib/libsystem.b.dylib (compatibility version 1.0.0, current version 1197.1.1) 

i can change path of dependant library:

$ install_name_tool -change libfoo.dylib ../foo/libfoo.dylib a.out 

so:

$ otool -l ./a.out ./a.out:         ../foo/libfoo.dylib (compatibility version 0.0.0, current version 0.0.0)         /usr/lib/libsystem.b.dylib (compatibility version 1.0.0, current version 1197.1.1) 

and program can execute:

$ ./a.out $ echo $? 6 

is there clang option can add command:

$ clang main.c -l../foo -lfoo  

to avoid having run:

$ install_name_tool -change libfoo.dylib ../foo/libfoo.dylib a.out 

note: don't want modify dyld_library_path or such other environment variable.

?

i've been hitting head against time , think have figured out how without using install_name_tool, @ least mac os 10.9 , later (as far i've tested).

while may have figured out, i'm posting here in case else needs it.

basically have 2 options:

  1. you can when compile library, defining install_name in terms of @executable_path

    root=$pwd mkdir $root/foo mkdir $root/bar  cd $root/foo echo 'int foo(int a, int b){return a+b;}' > foo.c clang foo.c -dynamiclib -install_name @executable_path/../foo/libfoo.dylib -o libfoo.dylib  cd $root/bar echo 'int foo(int,int); int main(){return foo(2,4);}' > main.c clang main.c -l../foo -lfoo -o main  ./main echo $? # output '6' 
  2. or can in 2 steps using @rpath, set when compile executable:

    root=$pwd mkdir $root/foo mkdir $root/bar  cd $root/foo echo 'int foo(int a, int b){return a+b;}' > foo.c clang foo.c -dynamiclib -install_name @rpath/libfoo.dylib -o libfoo.dylib  cd $root/bar echo 'int foo(int,int); int main(){return foo(2,4);}' > main.c clang main.c -l../foo -lfoo -rpath @executable_path/../foo/ -o main  ./main echo $? # output '6' 

the end result same in both cases:

bar $ otool -l main main:     @executable_path/../foo/libfoo.dylib (compatibility version 0.0.0, current version 0.0.0)     /usr/lib/libsystem.b.dylib (compatibility version 1.0.0, current version 1213.0.0) 

the second 1 preferable, since can compile library once, , have executable uses define load using own rpath.

please check here detailed explanations of @executable_path, @rpath , @load_path (which did not use here).


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -