我知道已经被问了很多次了,但我真的无法解决...
所以我有一个 src
文件夹,我的 main.c
源是一个 srclib
我的 lib.c
文件存储了一个 include
目录,其中存储了我的 lib.h
文件。现在 makefile 正确编译了 lib 并将 lib.a
文件放入 lib
文件夹中。 main.c
包含这样的库:
#include "lib.h"
它是使用 -I ../include
选项编译的,但是当我编译它时,我得到了库中每个函数的 undefined reference to xxx
错误
那我错过了什么?
最佳答案
没有。 -I
用于包含头文件。您还需要使用 -l
链接到图书馆选项。
注意:您可能需要使用 -L
提供库路径选项。
-llibrary
Search the library named library when linking...... The linker searches a standard list of directories for the library, which is actually a file named
liblibrary.a
. The linker then uses this file as if it had been specified precisely by name.
编辑:
引用同一手册的剩余部分
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus,
foo.o -lz bar.o
searches libraryz
after filefoo.o
but beforebar.o
. Ifbar.o
refers to functions inz
, those functions may not be loaded.
所以,你需要把-l<libanme>
在编译语句的最后,作为 s_echo.c
使用在该特定库中定义的函数。
关于c - GCC:对 xxx 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29769538/