我尝试编译一些非常简单的 C 代码,以尝试了解静态库的工作原理以及 BFD。
我使用这个 gcc 命令构建了代码,它找不到 libbfd,而 libbfd.a 静态库位于 /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
我搜索和搜索,找不到最有可能是一个非常简单的问题的答案,我提前为我的无知道歉!