How to Include Correctly -Wl,-Rpath,$Origin Linker Argument in a Makefile

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

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

Is it possible to have clang++ link dylib's relatively and not use install_name_tool?

It is indeed possible:

When compiling my library I set its install_name to be in terms of @rpath:

clang++ -dynamiclib lib_src/myprint.cpp -o myprint.dylib -install_name @rpath/myprint.dylib

And then in when compiling the executable I set rpath to be in terms of @executable_path, which can be done in a single step:

clang++ main_src/main.cpp -o main -I ./lib_src myprint.dylib -rpath @executable_path 

For a detailed explanation of @rpath and @executable_path please check this wiki: https://wincent.com/wiki/@executable_path,_@load_path_and_@rpath

Recursive build with QMake

The simplest way is to let qmake generate the qmake file for you.

After making a backup copy of any exist *.pro files you may need to reference, go to the top level of your directory structure and issue the command qmake -project. This tells qmake to recurse the tree and locate everything it needs to build and create a qmake project file from it.

Next, edit the generated qmake file. You will at least need to change the TEMPLATE line to be "lib" instead of "app". You will also want to specify the name of the TARGET. There may also be some other things you wish to change.

Now that you have a qmake file, you need to generate a make file. Run qmake again, but this time just say qmake without any arguments.

Finally, you should be able to just run make and have things build. [For future readers running Windows with the MingW tools, make should be replaced with mingw32-make]



Related Topics



Leave a reply



Submit