2012-05-14 77 views
1

我正在尝试使用Libelf库来获取一些精灵文件的一些信息。但我不断收到这些“未定义的参考”[...]“。我从突触安装了libelf(也试图从网站上获取),并且lib似乎已经安装好了。Libelf:未定义函数参考

我的代码:

#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; 
} 

以下是我从终端(目前使用Ubuntu 11.4)获得:

gcc sample.c -o sample 
/tmp/ccP7v2DT.o: In function `main': 
sample.c:(.text+0x57): undefined reference to `elf_begin' 
sample.c:(.text+0x67): undefined reference to `elf_kind' 
sample.c:(.text+0x77): undefined reference to `elf_end' 
collect2: ld returned 1 exit status 

UPDATE

解决一切,但我的问题之一在编译期间将-lelf放在我的文件之前。最后一个,我只能解决更新我了libelf到一个新的版本(这是不可用的突触经理):

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 

回答

3

你可能需要链接到库。这样做的标准方法是:

gcc sample.c -l elf -o sample 

它会搜索标准库目录的libelf.a,并包括在归档到构建相关的目标文件。假定库已经正确安装,libelf.a或一个类似命名的文件应该存在。

+0

感谢您的回复,解决了其中一个错误。另一个(函数elf_getshdrstrndx)我只能解决安装另一个版本的libelf。 我是如何做到的: 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 –