假设main.cpp,hello.h,hello.cpp,其中main.cpp调用了hello类中的方法

1 生成hello.so

  g++ -shared hello.cpp -olibhello.so

2 编译main.cpp,并链接,并指定运行时libhello.so的位置

  g++ main.cpp -lhello -L./ -Wl,-rpath=./ -o main

 

值得一提的是,如果采用带版本号的库,例如libhello.so.2

链接命令可使用g++ main.cpp libhello.so.2 -L./ -Wl,-rpath=./ -o main

 

2)加入第二个so库

g++ main.cpp -L./second/ -Wl,-rpath=./second/ -lsecond -L./hello/ -Wl,-rpath=./hello/ -lhello  -o main

 

ps,遇到过一个奇怪的问题,就是假设libhello.so还用到了libother.so,由于在/etc/ld.so.conf里配置错误了libother.so的目录路径,导致一直产生undefined reference to错误,但是在工程里对libother目录路径配置是正确的,有可能于查找路径顺序有关

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-20
  • 2022-12-23
  • 2022-12-23
  • 2021-12-12
  • 2021-12-22
  • 2021-09-02
猜你喜欢
  • 2021-12-12
  • 2021-12-12
  • 2022-12-23
  • 2021-11-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案