GCC:对 xxx 的未定义引用

GCC: undefined reference to xxx

我知道已经问了很多次了,但我真的无法解决... 所以我有一个 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 选项对库进行 link。

注意:您可能需要使用 -L 选项提供 库路径

引用 online gcc manual

-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 library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded.

因此,您需要将 -l<libanme> 放在编译语句的最后,因为 s_echo.c 使用在该特定库中定义的函数。