Why LD_LIBRARY_PATH is BAD and the correct way to load dynamic libraries

前端 未结 2 1815
陌清茗
陌清茗 2021-02-02 14:21

So, I have a program that runs with OpenBlas and I want to compile it. The linking process looks like this:

gcc -o prog prog.o -O3 -I/opt/OpenBLAS/include -L/opt         


        
相关标签:
2条回答
  • 2021-02-02 14:48

    On linux, it is also possible to use $ORIGIN in rpath to mean the directory path to the application and build a relative rpath from there. You would then move the library to a known relative path to the binary.

    gcc -o prog prog.o -O3 -I/opt/OpenBLAS/include -Wl,-rpath=\$ORIGIN/lib -L/opt/OpenBLAS/lib -lopenblas
    

    You can also use the full path to the libary, and it will be linked in:

    gcc -o prog prog.o -O3 -I/opt/OpenBLAS/include /opt/OpenBLAS/lib/libopenblas.so
    

    If you run "ldd" on the executable, you should see the full path encoded.

    0 讨论(0)
  • 2021-02-02 15:04

    Add the path to the runtime library search path.

    gcc -Wl,-rpath=/opt/OpenBlas/lib ...
    

    What the -L option does at link time, the -rpath option does at run time.

    0 讨论(0)
提交回复
热议问题