出现以下报错:
/usr/bin/ld: ../3rdparty/libsndfile/Linux/aarch64/libsndfile.a(libsndfile_la-sndfile.o): relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `stderr@@GLIBC_2.17' which may bind externally can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: ../3rdparty/libsndfile/Linux/aarch64/libsndfile.a(libsndfile_la-sndfile.o)(.text+0x3b4): unresolvable R_AARCH64_ADR_PREL_PG_HI21 relocation against symbol `stderr@@GLIBC_2.17'
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/test_car.dir/build.make:253: test_car] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/test_car.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
该错误表明,静态库 libsndfile.a
中的对象文件未使用 -fPIC
编译,导致在链接时无法被用于生成共享目标或位置无关代码
可行的解决方案
方法 1: 重新构建支持 -fPIC
的静态库
如果可以找到 libsndfile.a
的编译来源,建议重新编译该库,添加 -fPIC
选项。
方法 2: 将静态库打包为动态库
如果不能重新编译,可以将静态库封装为动态库,这通常可以绕过 -fPIC
问题。
临时解决方法
仅供调试使用:
-
跳过静态库的 PIC 检查: 修改链接选项,添加
-no-pie
(不推荐,仅用于快速测试):target_link_libraries(test PRIVATE -no-pie ${LIBSNDFILE_PATH}/libsndfile.a)
-
如果链接通过,则说明问题来源于链接器对
-fPIC
的限制。但这种方法可能会导致程序在加载时出现运行时错误。