C Libelf:函数的未定义引用

C Libelf:函数的未定义引用,c,undefined-reference,C,Undefined Reference,我正在尝试使用Libelf库来获取一些elf文件的信息。但我一直在得到这些“对[…]的未定义引用”。我从synaptic安装了libelf(也尝试从网站上获取),lib似乎还可以安装 我的代码: #include <err.h> #include <fcntl.h> #include <sysexits.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h>

我正在尝试使用Libelf库来获取一些elf文件的信息。但我一直在得到这些“对[…]的未定义引用”。我从synaptic安装了libelf(也尝试从网站上获取),lib似乎还可以安装

我的代码:

#include <err.h>
#include <fcntl.h>
#include <sysexits.h>
#include <unistd.h>

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

int main(int argc, char * argv[]) {
    Elf *elf_file;
    Elf_Kind  elf_kind_file;
    int file_descriptor;

    if((argc!=2)) 
        printf("Argumento em formato nao esperado\n");

    file_descriptor = open(argv[1], O_RDONLY, 0);
    elf_file = elf_begin(file_descriptor, ELF_C_READ, NULL);
    elf_kind_file = elf_kind(elf_file);

    elf_end(elf_file);
    close(file_descriptor);


    return 0;
}
更新

在编译过程中将-lelf放在文件之前,解决了除一个以外的所有问题。最后一个问题我只能解决将我的libelf更新为新版本的问题(Synaptic Manager上没有该版本):


您可能需要链接到库。执行此操作的标准方法是:

gcc sample.c -l elf -o sample

这将在标准库目录中搜索libelf.a,并将该归档中的相关对象文件包含到构建中。假设库已正确安装,libelf.a或类似名称的文件应该存在。

感谢您的回复,解决了除一个错误以外的所有错误。另一个(函数elf_getshdrstrndx)我只能解决安装另一个版本的libelf的问题。我是如何做到的:wget wget sudo dpkg-I libelf-dev_0.153-1_i386.deb libelf 1_0.153-1_i386.deb
wget http://ftp.br.debian.org/debian/pool/main/e/elfutils/libelf-dev_0.153-1_i386.deb
wget http://ftp.br.debian.org/debian/pool/main/e/elfutils/libelf1_0.153-1_i386.deb
sudo dpkg -i libelf-dev_0.153-1_i386.deb libelf1_0.153-1_i386.deb
gcc sample.c -l elf -o sample