C/C++链接静态库报错:dangerous relocation: unsupported relocation(-fPIC)

Posted Dontla

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C/C++链接静态库报错:dangerous relocation: unsupported relocation(-fPIC)相关的知识,希望对你有一定的参考价值。

root@ubuntu:/kyai_nv# Building project kyai...
make[2]: Warning: File 'kyai/CMakeFiles/kyai.dir/flags.make' has modification time 30937670 s in the future
make[2]: warning:  Clock skew detected.  Your build may be incomplete.
make[2]: Warning: File 'kyai/CMakeFiles/kyai.dir/flags.make' has modification time 30937670 s in the future
/usr/bin/ld: /userdata/temp/libky_ai_socket.a(ky_ai_socket.o): relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `filename' which may bind externally can not be used when making a shared object; recompile with -fPIC
/userdata/temp/libky_ai_socket.a(ky_ai_socket.o): In function `ky_ai_restart_algo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
ky_ai_socket.cpp:(.text+0xe68): dangerous relocation: unsupported relocation
/usr/bin/ld: /userdata/temp/libky_ai_socket.a(ky_ai_socket.o): relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `_ZGVZ9KY_AI_LOGIA23_cJNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEA2_cEEvRKT_DpRKT0_E10s_ky_ai_fs' which may bind externally can not be used when making a shared object; recompile with -fPIC
/userdata/temp/libky_ai_socket.a(ky_ai_socket.o): In function `void KY_AI_LOG<char [23], std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, char [2]>(char const (&) [23], std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const (&) [2])':
ky_ai_socket.cpp:(.text._Z9KY_AI_LOGIA23_cJNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEA2_cEEvRKT_DpRKT0_[_Z9KY_AI_LOGIA23_cJNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEA2_cEEvRKT_DpRKT0_]+0x2c): dangerous relocation: unsupported relocation
/usr/bin/ld: /userdata/temp/libky_ai_socket.a(ky_ai_socket.o): relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `_ZGVZ9KY_AI_LOGIA23_cJNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEA2_cEEvRKT_DpRKT0_E10s_ky_ai_fs' which may bind externally can not be used when making a shared object; recompile with -fPIC
ky_ai_socket.cpp:(.text._Z9KY_AI_LOGIA23_cJNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEA2_cEEvRKT_DpRKT0_[_Z9KY_AI_LOGIA23_cJNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEA2_cEEvRKT_DpRKT0_]+0x54): dangerous relocation: unsupported relocation
/usr/bin/ld: /userdata/temp/libky_ai_socket.a(ky_ai_socket.o): relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `_ZZ9KY_AI_LOGIA23_cJNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEA2_cEEvRKT_DpRKT0_E10s_ky_ai_fs' which may bind externally can not be used when making a shared object; recompile with -fPIC
ky_ai_socket.cpp:(.text._Z9KY_AI_LOGIA23_cJNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEA2_cEEvRKT_DpRKT0_[_Z9KY_AI_LOGIA23_cJNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEA2_cEEvRKT_DpRKT0_]+0x78): dangerous relocation: unsupported relocation
/usr/bin/ld: /userdata/temp/libky_ai_socket.a(ky_ai_socket.o): relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `_ZGVZ9KY_AI_LOGIA23_cJNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEA2_cEEvRKT_DpRKT0_E10s_ky_ai_fs' which may bind externally can not be used when making a shared object; recompile with -fPIC
ky_ai_socket.cpp:(.text._Z9KY_AI_LOGIA23_cJNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEA2_cEEvRKT_DpRKT0_[_Z9KY_AI_LOGIA23_cJNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEA2_cEEvRKT_DpRKT0_]+0x84): dangerous relocation: unsupported relocation
/usr/bin/ld: /userdata/temp/libky_ai_socket.a(ky_ai_socket.o): relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `_ZZ9KY_AI_LOGIA23_cJNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEA2_cEEvRKT_DpRKT0_E10s_ky_ai_fs' which may bind externally can not be used when making a shared object; recompile with -fPIC

解决方案:都是说什么加上-fPIC(与位置无关的代码)

我编译.o的时候加上-fPIC就好了

#!/bin/bash

list="ky_ai_api ky_ai_parse_json ky_ai_socket"
set -- `echo $list`
for((i=1;i<4;i++))
do
	echo $1
	gcc -c $1.cpp -o $1.o -fPIC -I/root/.ngrest/ngrest-build/deploy/include/
	ar rcs -o lib$1.a $1.o
	shift
done

参考文章:dangerous relocation: unsupported relocation 解决方案

以上是关于C/C++链接静态库报错:dangerous relocation: unsupported relocation(-fPIC)的主要内容,如果未能解决你的问题,请参考以下文章

cv::resize Xcode找不到lib库报错

PCL链接到不存在的库报错

PCL链接到不存在的库报错

PCL链接到不存在的库报错

PCL链接到不存在的库报错

iOS 加载动态库报错问题