Question

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 <stdio.h>

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 ./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
Was it helpful?

Solution

One concrete option that works would be to set the install_name flag when linking the .dylib.

gcc -dynamiclib -install_name '$(CURDIR)/hidelib/libmylibrary.dylib' -current_version 1.0 mylibrary.o -o libmylibrary.dylib

Then you can just link to the library normally:

gcc main.o -L '$(CURDIR)/hidelib' -lmylibrary -o main

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top