简体   繁体   English

使用SUSE Linux和C的静态库链接问题

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

I have some very simple C code I was trying to compile to try to understand how static libraries work and also BFDs. 我尝试编译一些非常简单的C代码,以了解静态库以及BFD的工作方式。

I built the code using this gcc command and it could not find libbfd, and libbfd.a static library is located in both /usr/lib64 and I also copied it locally, and given I specified -L to both those directories to find the static library, it still could not find it: 我使用此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

I searched and searched and could not find the answer to what is most likely a very simple question, and I apologize in advance for my ignorance! 我搜寻了一下,却找不到最可能是一个非常简单的问题的答案,对于我的无知,我事先表示歉意!

Ah, right. 嗯对 Your problem is that you specified libraries before getsections.c which uses them. 您的问题是您在使用它们的getsections.c之前指定了库。

The way linker works it pulls in from libraries only those object files that resolve some of the symbols linker is looking for and linker processes libraries and object files in order you specified them on command line. 链接器的工作方式仅从库中提取可解决某些符号的对象文件,而链接器正在寻找这些对象,并且链接器会按您在命令行上指定的顺序处理库和对象文件。 In this case when linker got to look at libbfd.a, there were no unresolved symbols linker needed to resolved, so it skipped all objects. 在这种情况下,链接器必须查看libbfd.a时,链接器不需要解析所有未解析的符号,因此它会跳过所有对象。 Then linker got to process getsections.c which used bfd_init and bfd_openr. 然后链接器处理使用bfd_init和bfd_openr的getsections.c。 Alas that was the last item on the command line and linker still had those symbols unresolved. las,这是命令行和链接器上的最后一项,但这些符号仍未解析。

Put -lbfd after your source file and linking should work. -lbfd源文件后,并链接应该工作。

This command line is incorrect: 此命令行不正确:

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

You need to move -lbfd to the end of the link line. 您需要将-lbfd移动到链接行的末尾 To understand why, read this . 要了解原因,请阅读此内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM