首页 > 解决方案 > 在 Ubuntu 上链接精灵库失败

问题描述

我尝试使用 elf 库在 Ubuntu 上编写一些代码。

但是,它有编译错误,我不知道出了什么问题。

请帮助我,谢谢。

sudo apt-get install libelf-dev

$ gcc -O3 -Wall -lelf test.c
/tmp/ccRqW5Qo.o: In function `main':
test.c:(.text.startup+0xa): undefined reference to `elf_version'
collect2: error: ld returned 1 exit status

这是 test.c 的代码

#include <stdlib.h>
#include <stdio.h>
#include <libelf.h>

int main(void)
{
        elf_version(EV_CURRENT);
        return 0;
}

标签: ubuntuelf

解决方案


您对命令行参数使用了错误的顺序。您需要在库之前gcc -O3 -Wall test.c -lelf列出源文件:会起作用。


推荐阅读