maximusfz

导航

记录一下g++的编译选项

假设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目录路径配置是正确的,有可能于查找路径顺序有关

posted on 2010-12-01 14:41  maximusfz  阅读(2670)  评论(1编辑  收藏  举报