2012-05-14 80 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 –