MacOS — how to link a dynamic library with a relative path using gcc/ld?

You probably need the -L compiler/linker flag which adds to the search path for libraries.

Up vote 2 down vote favorite share g+ share fb share tw.

If you are trying to understand dynamic linking, this question is likely to be of interest. One of the answers to that question provides a wonderful example of creating and using a dynamic library. Based on it, I some simple files: main.

C: extern void someFunction (int x); int main (int argc, char** argv ) { someFunction(666); } mylibrary. C: #include void someFunction (int x) { printf ("\nsomeFunction called with x=%d\n", x); } makefile: main: mylibrary. C main.

C gcc -c mylibrary. C gcc -dynamiclib -current_version 1.0 mylibrary. O -o libmylibrary.

Dylib gcc -c main. C gcc -v main. O .

/hidelib/libmylibrary. Dylib -o main clean: rm *. O rm main rm *.

Dylib So far, everything works great. If I make and then enter . /main at the command prompt, I see the expected output: someFunction called with x=666 Now, I want to mix things up a little.

I've created a directory hidelib, which is a subdirectory of my main directory. And I'm adding one line to my makefile: main: mylibrary. C main.

C gcc -c mylibrary. C gcc -dynamiclib -current_version 1.0 mylibrary. O -o libmylibrary.

Dylib gcc -c main. C mv libmylibrary. Dylib hidelib # this is the new line clean: rm *.

O rm main rm hidelib/*. * Now, I want to add another line to the makefile so it will find libmylibrary. Dylib in the hidelib subdirectory.

I want to be able to run . /main in the same way. How can I do that?

EDIT: Thanks for the response. Having lots of options is wonderful, but a beginner just wants one concrete option that works. Here is what I am trying for the last line, but clearly I don't understand something.

The makefile executes without errors, but at runtime it says "library not found. " gcc main. O -rpath,'$$ORIGIN/hidelib' -lmylibrary -o main osx gcc linker ld dynamic-library link|improve this question edited Apr 5 at 3:29 asked Apr 5 at 1:13William Jockusch2,30522161 71% accept rate.

This question has an open bounty worth +100 reputation from William Jockusch ending in 4 days. The current answers do not contain enough detail.

You probably need the -L compiler/linker flag which adds to the search path for libraries. Are trying to move things after linking, you'll need a dyld environment variable for where to search. Man dyld and you should be able to get more information on DYLD_LIBRARY_PATH and other environment variables.

Typically, though, you set the install name of the library before linking to something with the -install_name linker flag to something like @rpath/mylibrary. Dylib as the value, then set the run path search paths on the main executable while compiling with the -rpath flag to @executable_path/hidelib. For more information see install_name_tool and the -rpath & -install_name arguments of ld.

Basically, there are a lot of options for what you're trying to do.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions