簡體   English   中英

使用SUSE Linux和C的靜態庫鏈接問題

[英]static library linking issue using SUSE Linux and C

我嘗試編譯一些非常簡單的C代碼,以了解靜態庫以及BFD的工作方式。

我使用此gcc命令構建了代碼,但找不到libbfd,並且libbfd。一個靜態庫位於/ usr / lib64中,並且我也在本地復制了它,並給我指定了-L到這兩個目錄中以找到靜態庫庫,它仍然找不到:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bfd.h"

unsigned int number_of_sections(bfd *abfd)
{
  return bfd_count_sections(abfd);
}

int main (int argc, char *argv[])
{
  bfd *ibfd = NULL;
  unsigned int numSections = 0;

  if (argc < 2)
    {
      printf("Argc < 2\n");
      exit(EXIT_FAILURE);
    }
  else
    {
      bfd_init();
      printf("filename = %s\n", argv[1]);
      ibfd = bfd_openr(argv[1], NULL);
      numSections = number_of_sections(ibfd);
      printf("num sections = %d\n", numSections);
    }
  return 1;
}

gcc -g -Wall -llibbfd -I. -I/usr/include -L. -L/usr/lib64 -o getsections ./getsections.c
/usr/lib64/gcc/x86_64-suse-linux/4.7/../../../../x86_64-suse-linux/bin/ld: cannot find -llibbfd
collect2: error: ld returned 1 exit status
make: *** [build] Error 1

I also made sure the path was on the LD_LIBRARY_PATH Linux environment variable:
printenv LD_LIBRARY_PATH
/usr/lib64:/usr/lib64/mpi/gcc/openmpi/lib64

I changed the gcc command slightly (-lbfd instead of -llibbfd) and here was the error:

gcc -g -Wall -lbfd -I. -I/usr/include -L. -L/usr/lib64 -o getsections ./getsections.c
/tmp/ccM9GYqT.o: 
In function `main':
/home/karen/dev/cs410_spring2014/./getsections.c:253: undefined reference to `bfd_init'
/home/karen/dev/cs410_spring2014/./getsections.c:255: undefined reference to `bfd_openr'
collect2: error: ld returned 1 exit status
make: *** [build] Error 1

我搜尋了一下,卻找不到最可能是一個非常簡單的問題的答案,對於我的無知,我事先表示歉意!

嗯對 您的問題是您在使用它們的getsections.c之前指定了庫。

鏈接器的工作方式僅從庫中提取可解決某些符號的對象文件,而鏈接器正在尋找這些對象,並且鏈接器會按您在命令行上指定的順序處理庫和對象文件。 在這種情況下,鏈接器必須查看libbfd.a時,鏈接器不需要解析所有未解析的符號,因此它會跳過所有對象。 然后鏈接器處理使用bfd_init和bfd_openr的getsections.c。 las,這是命令行和鏈接器上的最后一項,但這些符號仍未解析。

-lbfd源文件后,並鏈接應該工作。

此命令行不正確:

gcc -g -Wall -lbfd -I. ...

您需要將-lbfd移動到鏈接行的末尾 要了解原因,請閱讀此內容

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM