c - GCC:对 xxx 的 undefined reference

标签 c gcc linker makefile undefined-reference

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

引用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使用在该特定库中定义的函数。

关于c - GCC:对 xxx 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29769538/

相关文章:

c - 如何用语法理解指向数组的指针

c - 进入函数作用域但不在函数调用时指针参数为 NULL

c++ - 如何进行依赖于平台的检查并且没有 -Wtype-limits 警告?

c - 某些 .c 文件的一个 header 会导致链接器错误

windows - 为什么 g++ 找不到/链接到 qjpeg4.dll?

c - for 循环 - 无效的语句

在运行时检查 libxml2.so 的构建参数

linux - linux下gcc使用静态库的方法

c++ - Allegro:链接器未正确链接库 [Code::Blocks]

c - 检查从其他进程收到的字符串时进程崩溃