`
zealotds
  • 浏览: 119521 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

Linux Shared Library (DLL) & -rpath,'$ORIGIN'

阅读更多
The Argument -rpath,'$ORIGIN'

Needs to be prefixed with "-Wl," for most Linux OS (or "-R<path>" for Solaris, -brtl for AIX and "-Wl,+b<path>" for HPUX). It is to pass linker options to your linker through gcc or g++. It is significant if you need to build a binary that depends on other dynamic library. "rpath" means runtime path (to load dependent dynamic library). And the very useful variable that we frequently used is "$ORIGIN" which means the path where your (link) target binary is loaded.

# assuming your target lib is libb.so and it depends on liba.so
# and their deployment structure is like:
<install_root>
|
libb.so
|
<sub_dir>
  |
  liba.so

# then you should supply below argument to your gcc or g++:
# the double '$' is necessary if it is written in makefile (as escaping char)
# -Wl,-rpath,'$$ORIGIN/<sub_dir>'
# or, without single quote, you can write it like:
# -Wl,-rpath,\$$ORIGIN/<sub_dir>
# and -shared and -fPIC is usually need to build dynamic linkable library
gcc -o libb.so -shared -fPIC -L. -la -Wl,-rpath,\$$ORIGIN/<sub_dir> libb.c




Check RPATH with 'readelf':
# -d means dynamic
readelf -d <dll_name>
# output looks like:
#  Tag               Type                 Name/Value
# 0x0000000000000001 (NEEDED)             Shared library: [libb.so]
# 0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
# 0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
# 0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
# 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
# 0x000000000000000f (RPATH)              Library rpath: [$ORIGIN/libb]
# 0x000000000000000c (INIT)               0x7d8
# 0x000000000000000d (FINI)               0xa88
# 0x000000006ffffef5 (GNU_HASH)           0x158
# 0x0000000000000005 (STRTAB)             0x3c8
# 0x0000000000000006 (SYMTAB)             0x1a0
# 0x000000000000000a (STRSZ)              479 (bytes)
# 0x000000000000000b (SYMENT)             24 (bytes)
# 0x0000000000000003 (PLTGOT)             0x200e28
# 0x0000000000000002 (PLTRELSZ)           216 (bytes)
# 0x0000000000000014 (PLTREL)             RELA
# 0x0000000000000017 (JMPREL)             0x700
# 0x0000000000000007 (RELA)               0x628
# 0x0000000000000008 (RELASZ)             216 (bytes)
# 0x0000000000000009 (RELAENT)            24 (bytes)
# 0x000000006ffffffe (VERNEED)            0x5d8
# 0x000000006fffffff (VERNEEDNUM)         2
# 0x000000006ffffff0 (VERSYM)             0x5a8
# 0x000000006ffffff9 (RELACOUNT)          3
# 0x0000000000000000 (NULL)               0x0


Sample
// liba.h
#ifndef LIBA_GPGH7YWD_H
#define LIBA_GPGH7YWD_H
namespace dlltest {
void foo();
class A {
public:
    A();
};
} /* dlltest */
#endif /* end of include guard: LIBA_GPGH7YWD_H */

// liba.cpp
#include <iostream>
#include "liba.h"
namespace dlltest {
void foo() {
    std::cout << "liba:foo" << std::endl;
}
A::A() {
    foo();
}
}

// libb.h
#ifndef LIBB_5NC56UVQ_H
#define LIBB_5NC56UVQ_H
#include "liba.h"
namespace dlltest {
void goo();
class B : public A {
public:
    B();
};
}
#endif /* end of include guard: LIBB_5NC56UVQ_H */

// libb.cpp
#include <iostream>
#include "libb.h"
namespace dlltest {
void goo() {
    std::cout << "libb:goo" << std::endl;
}
B::B() {
    goo();
}
}

// libc1.h: 
// you never want to name it libc.so because it is 
// the name of standard C library
#ifndef LIBC_1Y6MU09S_H
#define LIBC_1Y6MU09S_H
#include "libb.h"
extern "C" void hoo();
extern "C" void hoo2();
namespace dlltest {
class C : public B {
public:
    C();
};
}
#endif /* end of include guard: LIBC_1Y6MU09S_H */

// libc1.cpp
#include <iostream>
#include "libc1.h"
void hoo() {
    std::cout << "libc:hoo" << std::endl;
}
void hoo2() {
    dlltest::C c;
}
namespace dlltest {
C::C() {
    hoo();
}
}

// main.cpp
// implicit dll dependency
#include "libc1.h"

using namespace dlltest;

int main(int argc, const char *argv[])
{
    C c;

    return 0;
}

// main2.cpp
// load dll with dlopen
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

typedef void(*FP)();

int main(int argc, char **argv) {
  void *handle;
  char *error;

  handle = dlopen ("./libc1.so", RTLD_LAZY);
  if (!handle) {
      fprintf (stderr, "%s\n", dlerror());
      exit(1);
  }

  dlerror();    /* Clear any existing error */
  FP fp = (FP)dlsym(handle, "hoo2");
  if ((error = dlerror()) != NULL)  {
      fprintf (stderr, "%s\n", error);
      exit(1);
  }

  (*fp)();
  dlclose(handle);
  return 0;
}

// makefile
# Dynamic loading shared library
#   dynamic libraries could be independent on each other when built
#   but, in this case, executable binary depends on all of them
dll: dir
    g++ -fPIC -shared -o bin/liba.so liba.cpp
    g++ -fPIC -shared -o bin/libb.so libb.cpp
    g++ -fPIC -shared -o bin/libc1.so libc1.cpp
    g++ -o bin/dlink main.cpp -Lbin -lc1 -lb -la -Wl,-rpath,\$$ORIGIN

# libc1.so is loaded by dlopen in main2.cpp
# libc1.so implicitly depends on libb.so, without RPATH libc1 
#     doesn't know where to find it
# libb.so implicitly depends on liba.so, without RPATH libb doesn't 
#     know where to find it
# need to link libb.so and liba.so to demo, so demo will load libb
#     and liba when it needs them
dlopen: dir2
    g++ -fPIC -shared -o bin/liba.so liba.cpp
    g++ -fPIC -shared -o bin/libb/libb.so libb.cpp
    g++ -fPIC -shared -o bin/libc1.so libc1.cpp
    g++ -o bin/dldemo main2.cpp -Lbin/libb -Lbin -la -lb -ldl -Wl,-rpath,\$$ORIGIN -Wl,-rpath,\$$ORIGIN/libb

# alternative to dlopen case:
# add libb dependency explicitly when build libc1:
#     -Lbin/libb and -lb is essential because you want libc1 know    libb
#     has the symbols it needs and -rpath tells libc1 where to find libb
# but liba's search path is not built into libb, it's needed to 
#     build the dependency into dldemo
# otherwise,  update the rpath of libb(for liba) should also work, check dlopen3
dlopen2: dir2
    g++ -fPIC -shared -o bin/liba.so liba.cpp
    g++ -fPIC -shared -o bin/libb/libb.so libb.cpp
    g++ -fPIC -shared -o bin/libc1.so libc1.cpp -Lbin/libb -lb -Wl,-rpath,\$$ORIGIN/libb
    g++ -o bin/dldemo main2.cpp -Lbin -la -ldl -Wl,-rpath,\$$ORIGIN

# second alternative to dlopen case:
# dldemo implicitly depends on none of 3 libs, but you still need to tell it where to
#     find libc1 with -rpath. otherwise, if you specify correct path to libc1.so when 
#     dlopen (and start the program in correct working diretory if you use indirect 
#     file path in dlopen), then -rpath is not needed 
# libc1 implicitly depends on libb and libc1 know where to find libb with -rpath setup
# libb implicitly depends on liba and libb know where to find liba with -rpath setup
dlopen3: dir2
    g++ -fPIC -shared -o bin/liba.so liba.cpp
    g++ -fPIC -shared -o bin/libb/libb.so libb.cpp -Lbin -la -Wl,-rpath,\$$ORIGIN/..
    g++ -fPIC -shared -o bin/libc1.so libc1.cpp -Lbin/libb -lb -Wl,-rpath,\$$ORIGIN/libb
    #g++ -o bin/dldemo main2.cpp -ldl -Wl,-rpath,\$$ORIGIN
    g++ -o bin/dldemo main2.cpp -ldl

# demostrate how to use -rpath to organize libraries into separate folders
strc1: dir2
    g++ -fPIC -shared -o bin/liba.so liba.cpp
    g++ -fPIC -shared -o bin/libb/libb.so libb.cpp
    g++ -fPIC -shared -o bin/libc1.so libc1.cpp
    g++ -o bin/strc1 main.cpp -Lbin -lc1 -la -Lbin/libb -lb \
    -Wl,-rpath,\$$ORIGIN -Wl,-rpath,\$$ORIGIN/libb

# mix static lib with dynamic lib
mix1: dir
    g++ -fPIC -shared -o bin/liba.so liba.cpp
    g++ -fPIC -c libb.cpp
    g++ -fPIC -shared -o bin/libc1.so libc1.cpp libb.o
    g++ -o bin/mix1 main.cpp -Lbin -lc1 -la -Wl,-rpath,\$$ORIGIN
    rm *.o

# static link:
# if you build 'dll' before 'mix2' without clean, you can find the below -lc1
# will link dynamic lib libc1.so instead of static lib libc1.a
# the conclusion is shared lib link is default behavior
mix2: dir clean
    g++ -fPIC -shared -o bin/liba.so liba.cpp
    g++ -fPIC -shared -o bin/libb.so libb.cpp
    g++ -c libc1.cpp
    ar -cvq bin/libc1.a libc1.o
    g++ -o bin/mix2 main.cpp -Lbin -lc1 -lb -la -Wl,-rpath,\$$ORIGIN
    rm *.o

dir:
    if [ ! -d bin ]; then mkdir bin; fi

dir2:
    if [ ! -d bin/libb ]; then mkdir -p bin/libb; fi

clean:
    rm -rf bin/*


For more information:
分享到:
评论

相关推荐

    05-rpath解决so动态库依赖1

    1. 赖库程动态库或可执件,是何找赖的库的是下录顺来• the RPATH binary header (set at build-time) of the l

    patchelf:一个用于修改ELF可执行文件的动态链接器和RPATH的小实用程序

    $ patchelf --set-interpreter /lib/my-ld-linux.so.2 my-program 更改可执行文件和库的RPATH : $ patchelf --set-rpath /opt/my-libs/lib:/other-libs my-program 缩小可执行文件和库的RPATH : $ patchelf --...

    gmssl.tar.gz

    ubuntu20编译Gmssl时,“version OPENSSL_1_1_0d not defined”问题的...修改Gmssl的Makefile文件,在CFLAGS赋值时添加-Wl,-rpath=$(LIBRPATH),LIB_LDFLAGS赋值LIB_LDFLAGS=-Wl,-rpath=$(LIBRPATH) -znodelete -m64。

    rPath简化定制Linux.pdf

    rPath简化定制Linux.pdf

    patchelf1.0-0.9

    patchelf 当前最新版本的 patchelf 源码,方便编译移植到各个平台

    linux全志R16的linux系统编译的资料_20170502_1655.7z

    全志R16平台编译linux系统V1.0.txt 2017/4/11 13:36 (编译请使用编译android的lichee的选项编译生成的.config文件,不然直接编译会报错!!!!) rootroot@cm-System-Product-Name:/home/wwt/linux_r16$ tar...

    npl:帮助大脑成像的“神经”程序和图书馆

    然后运行以下命令: $ git submodule init$ git submodule update$ ./waf configure --prefix=INSTALLDIR --release$ ./waf install -j 4安装后,您需要将 INSTALLDIR/lib 添加到 LD_LIBRARY_PATH。 或者,您可以在...

    php-7.0.27.tar.gz

    -with-pdo-mysql=shared,mysqlnd --with-gd --with-iconv --with-zlib --enable-zip --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-xml --enable-bcmath --enable-shmop ...

    php-7.3.0.tar.gz

    pdo-mysql=shared,mysqlnd --with-gd --with-iconv --with-zlib --enable-zip --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-xml --enable-bcmath --enable-shmop --...

    php-7.1.29.tar.gz

    -with-pdo-mysql=shared,mysqlnd --with-gd --with-iconv --with-zlib --enable-zip --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-xml --enable-bcmath --enable-shmop ...

    php-5.6.29.tar.gz

    -with-pdo-mysql=shared,mysqlnd --with-gd --with-iconv --with-zlib --enable-zip --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-xml --enable-bcmath --enable-shmop ...

    php-7.2.20.tar.gz

    -with-pdo-mysql=shared,mysqlnd --with-gd --with-iconv --with-zlib --enable-zip --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-xml --enable-bcmath --enable-shmop ...

    php-7.3.7.tar.gz

    with-pdo-mysql=shared,mysqlnd --with-gd --with-iconv --with-zlib --enable-zip --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-xml --enable-bcmath --enable-shmop -...

    lichee_20170502_1607_全志R16的linux系统编译需要改动的文件_使用parrotv1.1的内核_没有外层目录.7z

    rootroot@cm-System-Product-Name:/home/wwt/linux_r16$ tar zxvf lichee_parrotv1.1_20161202.tar.gz rootroot@cm-System-Product-Name:/home/wwt/linux_r16$ rootroot@cm-System-Product-Name:/home/wwt/linux_...

    Debenu Quick PDF Library for Mac 10.12

    The dylib for Debenu Quick PDF Library has a dependency on the Delphi restributable (libcgunwind.1.0.dylib) and both the .dylib files use @rpath for their install_path. Both dyblib files must use the ...

    qt帮助文档 qt-help

    [-nomake ] [-R ] [-l ] [-no-rpath] [-rpath] [-continue] [-verbose] [-v] [-silent] [-no-nis] [-nis] [-no-cups] [-cups] [-no-iconv] [-iconv] [-no-pch] [-pch] [-no-dbus] [-dbus] [-dbus-linked] [-no-...

    libboost_thread.so.1.58.0

    libboost_thread.so.1.58.0,系统缺库的时候需要用到,ldd可以检测

    PBC (Pairing-Based Cryptography) library 实现异或xor

    (pbc)Pairing-Based Cryptography是实现双线性对运算的 函數库,但没有异或XOR函數...编译命令: gcc -o xor xor.c -I /usr/local/include/pbc -L /usr/local/lib -Wl,-rpath /usr/local/lib -lpbc 运 行 :./xor &lt;a.param

    CentOS7.2.1511 gcc4.8.5 通过编译的 tfs2.2.16

    See any operating system documentation about shared libraries for more information, such as the ld(1) and ld.so(8) manual pages. ---------------------------------------------------------------------- ...

    install_name_tool:苹果的install_name_toolLinux端口

    苹果的install_name_toolLinux端口该端口装饰了NixOS PatchElf功能,以在Linux上提供Apple的标准install_name_tool实用程序接口。 这样,我们可以在Linux上部署特定于MacOS的CMake逻辑,例如fixup_bundle 。建筑git ...

Global site tag (gtag.js) - Google Analytics