g++的编译选项:-Wl,-rpath=

本文详细介绍了如何使用g++生成C++ SO库,并指导如何编译、链接main.cpp文件,同时涉及多库链接及解决库路径配置错误的技巧。

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


http://www.cnblogs.com/maximusfz/archive/2010/12/01/1893384.html

Command line: -release -opensource -opengl es2 -confirm-license -platform linux-g++ -device linux-rdk-x5-g++ -device-option CROSS_COMPILE=/opt/gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu- --sysroot=/home/wanjuxiong/rootfs -prefix /usr/local/qt5 -nomake examples -nomake tests -v 'QMAKE_LFLAGS=-Wl,-rpath-link,/home/wanjuxiong/rootfs/lib/aarch64-linux-gnu -Wl,-rpath-link,/home/wanjuxiong/rootfs/usr/lib/aarch64-linux-gnu' loaded result for config test config.qtbase.tests.machineTuple + /opt/gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-g++ -dumpmachine > aarch64-none-linux-gnu test config.qtbase.tests.machineTuple succeeded loaded result for config test config.qtbase.tests.verifyspec + cd /home/wanjuxiong/Qt/qt-everywhere-src-5.15.2/config.tests/verifyspec && /home/wanjuxiong/Qt/qt-everywhere-src-5.15.2/qtbase/bin/qmake "CONFIG -= qt debug_and_release app_bundle lib_bundle" "CONFIG += shared warn_off console single_arch" "QMAKE_CFLAGS += --sysroot=/home/wanjuxiong/rootfs" "QMAKE_CXXFLAGS += --sysroot=/home/wanjuxiong/rootfs" "QMAKE_LFLAGS += --sysroot=/home/wanjuxiong/rootfs" 'QMAKE_LFLAGS=-Wl,-rpath-link,/home/wanjuxiong/rootfs/lib/aarch64-linux-gnu -Wl,-rpath-link,/home/wanjuxiong/rootfs/usr/lib/aarch64-linux-gnu' -early "CONFIG += cross_compile" /home/wanjuxiong/Qt/qt-everywhere-src-5.15.2/qtbase/config.tests/verifyspec + cd /home/wanjuxiong/Qt/qt-everywhere-src-5.15.2/config.tests/verifyspec && MAKEFLAGS= /usr/bin/gmake clean && MAKEFLAGS= /usr/bin/gmake > rm -f verifyspec.o > rm -f *~ core *.core > /opt/gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-g++ -c -pipe -march=armv8-a+simd --sysroot=/home/wanjuxiong/rootfs -O2 -w -fPIC -I/home/wanjuxiong/Qt/qt-everywhere-src-5.15.2/qtbase/config.tests/verifyspec -I. -I/home/wanjuxiong/rootfs/usr/include -I/home/wanjuxiong/Qt/qt-everywhere-src-5.15.2/qtbase/mkspecs/devices/linux-rdk-x5-g++ -o verifyspec.o /home/wanjuxiong/Qt/qt
03-18
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值