ld 链接生成 so时报错: undefined reference to '__dso_handle'

本文详细解析了在链接阶段遇到的__dso_handle符号未定义的问题,常见于特定配置下的Android NDK编译环境。通过创建并包含一个提供弱定义__dso_handle的头文件,可以有效解决该问题,确保项目顺利编译。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题贴图,附上遇到的情况

ld -shared -o liba.so a.o -L ./lib/zmq -L /usr/lib/x86_64-linux-gnu/ -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_objdetect -lopencv_calib3d -lzmq -lstdc++ -lrt -ldl -lpthread
a.o: In function `__static_initialization_and_destruction_0(int, int)':
a.cpp:(.text+0x3e): undefined reference to `__dso_handle'
ld: a.o: relocation R_X86_64_PC32 against undefined hidden symbol `__dso_handle' can not be used when making a shared object
ld: final link failed: Bad value
make: *** [all] Error 1
error: qrc_qml.obj: requires unsupported dynamic reloc R_ARM_REL32; recompile with -fPIC

error: undefined reference to '__dso_handle'

 

解决方案,建一个头文件,代码如下,包含到项目里:

extern "C" {
/*
 * To work around http://code.google.com/p/android/issues/detail?id=23203
 * we don't link with the crt objects. In some configurations, this means
 * a lack of the __dso_handle symbol because it is defined there, and
 * depending on the android platform and ndk versions used, it may or may
 * not be defined in libc.so. In the latter case, we fail to link. Defining
 * it here as weak makes us provide the symbol when it's not provided by
 * the crt objects, making the change transparent for future NDKs that
 * would fix the original problem. On older NDKs, it is not a problem
 * either because the way __dso_handle was used was already broken (and
 * the custom linker works around it).
 */
  NS_EXPORT __attribute__((weak)) void *__dso_handle;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值