relocation R_X86_64_TPOFF32 against symbol `_ZN6xxxE' + xxx.so: cannot open shared object file: No such file or directory --grpc_out: protoc-gen-grpc: Plugin failed with status code 127.的一类解决办法
问题一:
报错:
/usr/bin/ld: /usr/local/lib/libprotobuf.a(arena.cc.o): relocation R_X86_64_TPOFF32 against symbol `_ZN6google8protobuf8internal9ArenaImpl13thread_cache_E' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /usr/local/lib/libprotoc.a(code_generator.cc.o): relocation R_X86_64_PC32 against symbol `_ZN6google8protobuf8internal26fixed_address_empty_stringB5cxx11E' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: 最后的链结失败: bad value
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/grpc_plugin_support.dir/build.make:195:libgrpc_plugin_support.so.1.28.0-pre3] 错误 1
make[1]: *** [CMakeFiles/Makefile2:1094:CMakeFiles/grpc_plugin_support.dir/all] 错误 2
make[1]: *** 正在等待未完成的任务....
出错原因是因为编译的时候没有加上-fPIC,-fPIC的作用是用于生成位置无关代码(Position Independent Code,PIC)。
位置无关代码是一种可以在内存中的任意位置加载和执行的机器码。它主要用于动态链接库(shared library)。当多个程序或库需要共享同一个动态库时,位置无关代码可以确保每个程序或库都可以在内存中独立地加载该动态库,而不会发生地址冲突或符号重定义的问题。
而我们刚才编译的时候没有加,所以报错:relocation R_X86_64_TPOFF32 against symbol `_ZN6google8protobuf8internal9ArenaImpl13thread_cache_E',猜测是因为这个符号在重定位时出现问题。
联系到操作系统学到的知识,重定位简单来说是把相对位置映射到物理地址,使得代码可以在任何时候被使用,而不仅仅只在编译时存放的那个位置才能使用。
解决办法是把项目重新编译一遍
进入到ptotubuf项目的根目录(这里我的项目是ptotubuf)
Cd cmake/build //或者你之前make时新建的文件夹
Make clean
cmake -Dprotobuf_BUILD_TESTS=OFF -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS="-fPIC" .. //带上参数重新make
ok编译成功。
问题二
输入make命令后
-- Using protobuf
-- Using gRPC 1.28.0-pre3
-- Configuring done
-- Generating done
-- Build files have been written to: /home/liyishui/grpc/examples/cpp/helloworld/build
[ 4%] Generating helloworld.pb.cc, helloworld.pb.h, helloworld.grpc.pb.cc, helloworld.grpc.pb.h
/usr/local/bin/grpc_cpp_plugin: error while loading shared libraries: libgrpc_plugin_support.so.1: cannot open shared object file: No such file or directory
--grpc_out: protoc-gen-grpc: Plugin failed with status code 127.
make[2]: *** [CMakeFiles/greeter_client.dir/build.make:74:helloworld.pb.cc] 错误 1
make[1]: *** [CMakeFiles/Makefile2:90:CMakeFiles/greeter_client.dir/all] 错误 2
make: *** [Makefile:91:all] 错误 2
报错:找不到libgrpc_plugin_support.so.1,先在系统里用命令找一下
find /usr/local/lib -name libgrpc_plugin_support.so.1
/usr/local/lib/libgrpc_plugin_support.so.1
输出了/usr/local/lib/libgrpc_plugin_support.so.1,说明这个库存在,但是它找不到,所以是libxxx.so库的位置没有告知ld-linux.so,原理见:https://blog.csdn.net/mahoon411/article/details/113565482
只需修改环境变量即可
Sudo vim ~/.bashrc
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH //这里要写自己的路径
Source ~/.bashrc
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)