0

I cannot make rpath work properly and make my binary to search for the library in the specified folder:

I have 3 very simple files:

main.c

#include <stdio.h>
#include <func.h>
int main() {
    testing();
return 1;
}

func.h

void testing();

func.c

#include "func.h"
void testing(){

    printf(testing\n");
}

Then I proceed to create a shared library as it follows:

gcc -c -fpic func.c -o ../release/func.o
gcc -shared -o ../release/lib/lib_func.so ../release/func.o

And then compile the program:

gcc main.c ../release/lib/lib_time_mgmt.so -Wl,-rpath=/home/root/ -o ../release/main

I receive the next warning:

main.c:7:2: warning: implicit declaration of function ‘testing’ [-Wimplicit-function-declaration]
testing();

But besides it, the program works fine.

However, my problem is that if now I want to move the library to /home/root (as specified in rpath) it does not work and the library is still searched only in the path specified when I compiled the main.c file which is ../release/lib/lib_time_mgmt.so

What am I doing wrong?


EDIT: After accepting the answer, I leave here the exact line as I used it and made it work for whoever might find it useful:

gcc main.c -L/home/root -Wl,-rpath,'/home/root/' -l:libtime_mgmt -o ${OUT_FILE}

Note: the rpath was used with the path betwen simple '. Not sure if that was the reason why it was not working before, but it worked this way now.

Daniel Ortega
  • 425
  • 5
  • 11

1 Answers1

2

rpath is not used at compile time, but rather at link/runtime... thus you probably need to use both of these:

  • -L /home/root - to link correctly at build time
  • -Wl,-rpath=/home/root - to link correctly at run-time

You should use the -l ${lib} flag to link with libraries, don't specify their path as an input.

In addition to this, convention states that the libraries are named libNAME.so - e.g:

  • -l func will try to link with libfunc.so
  • -l time_mgmt will try to link with libtime_mgmt.so

Once you've addressed the above points, try the following:

gcc main.c -L/home/root -Wl,-rpath=/home/root -lfunc -ltime_mgmt -o ${OUT_FILE}

As a final point, I'd advise that you try not to use rpath, and instead focus on installing libraries in the correct places.


Unrelated to your question, but worth noting. Your use of #include <...> vs #include "..." is questionable. See: What is the difference between #include <filename> and #include "filename"?

Attie
  • 6,690
  • 2
  • 24
  • 34
  • 1
    Maybe just link to an existing topic about that instead, since it isn't the main point of the question. For example: https://stackoverflow.com/questions/21593/what-is-the-difference-between-include-filename-and-include-filename – John Zwinck Feb 13 '18 at 13:29
  • Thank you for your answer. Indeed, your clarifications made me understand the topic deeper and understand the reason of my mistake. However, although I am not totally sure, I think it was not working initially also due to some typo problem in the rpath. I used this format now: rpath , ' $PATH ' and it worked. I leave here the complete line for whoever might need it: gcc main.c -L/home/root -Wl,-rpath,'/home/root/' -l:libtime_mgmt -o ${OUT_FILE} – Daniel Ortega Feb 14 '18 at 13:55