Linux C++ trys to load one specific library using an absolute path, while all otheres are linked using a relative one

后端 未结 1 1960
野的像风
野的像风 2021-01-16 06:39

I have the following Problem: I\'m trying to create a portable version of my program, so I set rpath to \".\" so all libraries are linked using the relative file path. And t

相关标签:
1条回答
  • 2021-01-16 07:20

    I set rpath to . so all libraries are linked using the relative file path

    Using . in rpath is a poor idea:

    • Usability: the application must be run from a specific working directory.
    • Security: an attacker may place modified .so files in another directory and run your application from there.

    The correct way is to use -rpath=$ORIGIN feature. See man ld.so:

    $ORIGIN (or equivalently ${ORIGIN}) This expands to the directory containing the program or shared object. Thus, an application located in somedir/app could be compiled with

    gcc -Wl,-rpath,'$ORIGIN/../lib'
    

    so that it finds an associated shared object in somedir/lib no matter where somedir is located in the directory hierarchy. This facilitates the creation of "turn-key" applications that do not need to be installed into special directories, but can instead be unpacked into any directory and still find their own shared objects.

    $ORIGIN syntax is a bit unfortunate because it gets expanded as a variable by both make and bash, so you may need to quote it appropriately.


    what could lead to linux trying to find the library at the original location instead of at the relative one like all the others

    When linking, the library may be specified as -lmylib or -l:libmylib.so or -l<path>/libmylib.so. In the latter case the runtime linker looks for the library in that particular path <path>/libmylib.so only. See man ld, option -l for full details. You may like to review your build system linker commands.

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