先解释一下上下文。我有一个带有函数声明的 header 、一个带有函数主体的 .c 程序和主程序。
foo.h
#ifndef _FOO_H_
#define _FOO_H_
void foo();
#endif
foo.c
#include<stdio.h>
#include "include/foo.h"
void foo()
{
printf("Hello\n");
}
mainer.c
#include <stdio.h>
#include "include/foo.h"
int main()
{ foo();
return 0;
}
为了这个程序的目的,头文件和静态库都需要在单独的文件夹中,所以头文件在/include/foo.h
上,生成的静态库将在/lib/libfoo.a
,以及主目录中的两个 .c 程序。思路是生成目标程序,然后生成静态库,然后链接静态库创建可执行文件,最后执行程序。
我创建目标程序和静态库都没有问题。
$ gcc -c foo.c -o foo.o
$ ar rcs lib/libfoo.a foo.o
但是当我尝试链接静态库时...
$ gcc -static mainer.c -L. -lfoo -o mainfoo
它给了我一个错误,声称找不到静态库
/usr/bin/ld: cannot find -lfoo
collect2: ld returned 1 exit status
这很奇怪,考虑到 I asked before how to work with static libraries and headers on separate folders and in this case the static libraries were found .知道我做错了什么吗?
最佳答案
将 -L.
更改为 -Llib
,因为看起来您在那里创建了 .a 文件。
关于c - 找不到静态库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15913827/