在Debian Bullseye编译时遇libunwind: undefined reference错误
问题描述
尝试编译以下C程序:
#include <stdio.h> #include <stdlib.h> #include <libunwind.h> int main(int argc, char **argv) { unw_cursor_t cursor; unw_context_t context; // 获取机器上下文并初始化游标 if (unw_getcontext(&context) < 0) printf("ERROR: cannot get local machine state\n"); if (unw_init_local(&cursor, &context) < 0) printf("ERROR: cannot initialize cursor for local unwinding\n"); return 1; }
使用编译命令:
gcc -lunwind main.c -o test
出现如下链接错误:
/usr/bin/ld: /tmp/ccFDiWBz.o: in function `main': main.c:(.text+0x23): undefined reference to `_Ux86_64_getcontext' /usr/bin/ld: main.c:(.text+0x4c): undefined reference to `_Ux86_64_init_local' collect2: error: ld returned 1 exit status
已安装libunwind-dev,且能在libunwind.so.8.0.1中找到对应符号,询问是否需要更换链接方式或存在其他问题。
解决方案
调整链接顺序
GCC链接器按命令行顺序处理文件,库需要放在依赖它的源文件之后。修改编译命令为:
gcc main.c -lunwind -o test
显式链接平台专用子库
部分架构下需要额外链接平台相关的libunwind组件,x86_64架构可添加-lunwind-x86_64参数:
gcc main.c -lunwind -lunwind-x86_64 -o test
指定库搜索路径(可选)
如果libunwind安装在非默认路径,需用-L参数指定库目录:
gcc main.c -L/your/libunwind/path -lunwind -o test
内容的提问来源于stack exchange,提问作者user3379855
相关产品推荐
相关产品推荐

