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

前端 未结 2 1818
陌清茗
陌清茗 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.

提交回复
热议问题