【发布时间】:2014-03-29 17:17:41
【问题描述】:
我尝试编译一些非常简单的 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
我搜索和搜索,但找不到最有可能是一个非常简单的问题的答案,对于我的无知,我提前道歉!
【问题讨论】:
-
尝试将
-lbfd放在命令行末尾(./getsections.c之后)。 -
I was not sure if I should make a separate question to Stack Overflow?绝对是一个新问题
标签: c linux linker static-libraries bfd