I am writing a Makefile for a project that uses a third-party dynamic library. The library lies in a specific directory, not included in the list of defaults.

Compilation is successful. Problems, obviously, with the launch - can not find the library.

Is it possible to make a Makefile so that it performs a function similar to the link_directories function in CMake? That is, so that the path to the library is “sewn up” in the executable file.

    2 answers 2

    According to man ld , the linker needs to tell the linker the parameter -rpath=тот_самый_путь to the libraries that you need to sew into a binary. This can be done in two ways. Through the option gcc -Xlinker -rpath=тот_самый_путь . If you use the default make build rules, the LDFLAGS variable in the Makefile is appropriate for these options:

     LDFLAGS+= -Xlinker -rpath=тот_самый_путь 

    The second way is bypassing gcc through the environment variable LD_RUN_PATH which ld reads itself if the -rpath option was not present. In the Makefile for this you can write:

     export LD_RUN_PATH=тот_самый_путь 

    Or write LD_RUN_PATH=тот_самый_путь before $(CC) directly in the link rule.

    The path may contain the substring $ORIGIN , instead of which the path to the directory containing the executable file will be substituted. See man ld-linux.so . An example of a line in the Makefile:

     myprogram: LDFLAGS+= -Xlinker -rpath='$$ORIGIN/' myprogram: myprogram.o 
    • Thank you I will try! - AccumPlus
    • Everything works great! Thank! - AccumPlus

    the make program (and, moreover, cmake ) doesn’t have a relationship as such, as I understand it.

    for link_directories is an indication to the builder that runs at compile time.

    you want to give instructions to the loader . but the system loader searches for libraries by the names specified in the executable file in those directories in which it (the loader) is configured to search for them at boot time (that is, even before the program runs). You can reconfigure the system bootloader using the ldconfig program (see man ldconfig ).

    without reconfiguring the bootloader, you can give such an indication using the environment variable, for example, by running not the program directly, but the script in any programming language you prefer (for example, the shell language) in which to call your program by specifying the path using the environment variable LD_LIBRARY_PATH (paths) to your library (s) :

     LD_LIBRARY_PATH=путь программа 

    so that the path to the library is "wired" in the executable file

    you can dynamically load the library at runtime (specifying a relative or full path to it) using the dlopen() function.

    • Yes, I know this method, thanks. But it is precisely the possibility of writing a team-analog CMake-ovsky link_directories. - AccumPlus
    • I suspected that it was so. But nevertheless, CMake, which generates the Makefile, generates it in such a way that the further assembly of the program by the Make utility leads to the creation of an executable file, in which the paths to the libraries are already stitched. Isn't it supposed to be spelled out somehow in the Makefile? - AccumPlus
    • Got it. But nevertheless, CMake somehow copes with it ... Somehow, these paths are still indicated in the executable file. Thanks for the dlopen, but this is also not that. I am not perfect in formulating questions, but I still hope that you understand what I want to receive. - AccumPlus
    • Yes, thanks, you are right in this, this is an analogue of the -L flag of the compiler. But the essence of the question is different, and you most likely understand this) - AccumPlus
    • one
      @AccumPlus, google RPATH, cmake and uses it. But this is an ugly decision, to say the least. - ixSci pm