1

I am trying to generate a c++ library using the g++ compiler. My library has another C library as dependency and I have compiled it in order to obtain the .so files.

I have the following structure:

src:
    include/linux:
        libcustom.a
        libcustom.la
        libcustom.so
        libcustom.so.0
        libcustom.so.0.0.0

Now, when I have all the .o files of my cpp classes, and I want to link the library, I execute the following command:

g++ -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o mylibrary.so File1.o File2.o File3.o -L./include/linux -lc++ -lutil -lm -lcustom -Wl,-rpath='$ORIGIN/include/linux' -L/usr/lib/R/lib -lR

But it throws me the error:

libcustom.so.0: cannot open shared object file: No such file or directory

I am executing the command from the src directory.

I know it could be fixed editing the LD_LIBRARY_PATH, but the idea it is someone can use my library without the need of configuring anything, so I am trying to do that with the c++'s -rpath flag.

Any idea how can I fix it, or the reason for the error?

bra_racing
  • 622
  • 1
  • 8
  • 32
  • You use relative path `-Wl,-rpath=./include/linux`, which is not recognized by the `ld` loader in Linux. Use absolute path instead. For correct way of using relative path, goto http://stackoverflow.com/a/4018559/6238076 – gdlmx May 14 '16 at 11:25

1 Answers1

3

The error message you got seems to come from the run-time loader ld.so instead of the linker ld (I know the names are confusing). You have to distinguish between finding so's at link-time and at run-time. The -L flag you give at link-time has nothing to do with localizing the library at run-time.

Your rpath=./include/linux value is not correct, because dot is not recognized by the ld as relative path. Relative searching path should be given like

-Wl,-rpath='$ORIGIN/include/linux'

where the $ORIGIN represents the folder where your executable (not mylibrary.so) locates. Make sure to use single quote and not double quote because the string $ORIGIN should be passed to the linker literally and hard coded into the executable file.

More details goes

  1. how to link to shared lib from shared lib with relative path

  2. ld: Using -rpath,$ORIGIN inside a shared library (recursive)

Community
  • 1
  • 1
gdlmx
  • 6,479
  • 1
  • 21
  • 39
  • You are totally right, that is the problem, because now I have tested to change the -rpath indicating the absolute path of the library and it works. But `$ORIGIN` does not work for me, because I am using a Makevars file and it "translates" the -rpath from `-Wl,-rpath="'$$ORIGIN/include/linux'"` to `-Wl,-rpath="'/include/linux'"`. I have read the webpage attached in the post that you suggest, and I can not find the solution. Do you know how can I fix it? Thank you very much – bra_racing May 14 '16 at 13:14
  • 1
    In Makefile you need to escape the `$` symbol with `$$`: that is `-rpath='$$ORIGIN/include/linux'` – gdlmx May 14 '16 at 13:17
  • 1
    Also you don't need to double quote the path, use single quote only. Check the echo of your make command and ensure the parameter is passed to g++ correctly – gdlmx May 14 '16 at 13:26
  • Now I have written `-rpath='$$ORIGIN/include/linux'` in my Makevars, and it executes `-rpath=$ORIGIN/include/linux` without the quotes. How can add them? If I write `-rpath="'$$ORIGIN/include/linux"'` (double quotes plus single quotes) it executes `rpath=/include/linux` – bra_racing May 14 '16 at 13:28
  • 1
    Seems that you need to write `'-Wl,-rpath=\'$$ORIGIN/include/linux\''` to also escape single quote `'`. http://stackoverflow.com/a/7860705/6238076 – gdlmx May 14 '16 at 13:36
  • With `'-Wl,-rpath='\''$$ORIGIN/include/linux'\'''` it executes `'$ORIGIN/include/linux'` (great!), but still throwing the error because it does not find the `.so` file. Do I need to configure something else in order to use the $ORIGIN param? Sorry if I am annoying you, I appreciate a lot your help – bra_racing May 14 '16 at 13:58
  • Can you update the question including the echo of your make system (exact commands issued to g++ and the corresponding error message). – gdlmx May 14 '16 at 17:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/111941/discussion-between-gdlmx-and-bra-racing). – gdlmx May 14 '16 at 17:29