**未定义的参考**链接两个库的错误

发布于 2025-02-04 01:35:02 字数 1602 浏览 3 评论 0 原文

我会在尝试编译两个库的 main 不确定的参考错误。我有两个文件 lib1/func1.c lib2/func2.c 在单独的文件夹中。这些文件包含两个函数 print1() print2(),函数 print1()是调用 print2() 。 我将它们分别编译成两个库 libfunc1.a libfunc2.a 。 但是,当我尝试编译 main 时,它正在调用 print1()时,我会收到以下错误:

/usr/bin/ld: /home/sv/ztest2/lib1/libfunc1.a(func1.o): in function print1:
/home/sv/ztest2/lib1/func1.c:7: undefined reference to print2
collect2: error: ld returned 1 exit status
make: *** [Makefile:21: DP] Error 1

这是代码和makefiles:

makefile

TARGET = DP
HOME = /home/slav/FORECAST/ztest2

INCDIRS = -I./ \
      -I$(HOME)/lib1 \
      -I$(HOME)/lib2

LIBDIRS = -L$(HOME)/lib1 \
      -L$(HOME)/lib2

SRCFILES = DP.c
OBJFILES = DP.o

CFLAGS = -g -O3 $(INCDIRS)

all: $(TARGET)

$(TARGET): $(OBJFILES)
    cc $(CFLAGS) -o $(TARGET) $(OBJFILES) $(LIBDIRS) -lfunc2 -lfunc1

clean:
    -rm *.o $(TARGET)

dp.c

#include "func1.h"
int main()
{
    print1();
    return 0;
}

func1.h

void print1();

func1.c

#include <stdio.h>
void print1()
{
    printf("print1 is called!\n");
    print2();
}

func2.h

extern void print2();

func2。 C

#include <stdio.h>
void print2()
{
    printf("print2 is called!\n");
}

I am getting undefined reference error while trying to compile main that refers to two libraries. I have two files lib1/func1.c and lib2/func2.c in separate folders. Those files contain two functions print1() and print2(), function print1() is calling print2().
I am compiling those separately into two libraries libfunc1.a and libfunc2.a.
But when I am trying to compile main which is calling print1(), I get the following error:

/usr/bin/ld: /home/sv/ztest2/lib1/libfunc1.a(func1.o): in function print1:
/home/sv/ztest2/lib1/func1.c:7: undefined reference to print2
collect2: error: ld returned 1 exit status
make: *** [Makefile:21: DP] Error 1

Here is the code and Makefiles:

Makefile:

TARGET = DP
HOME = /home/slav/FORECAST/ztest2

INCDIRS = -I./ \
      -I$(HOME)/lib1 \
      -I$(HOME)/lib2

LIBDIRS = -L$(HOME)/lib1 \
      -L$(HOME)/lib2

SRCFILES = DP.c
OBJFILES = DP.o

CFLAGS = -g -O3 $(INCDIRS)

all: $(TARGET)

$(TARGET): $(OBJFILES)
    cc $(CFLAGS) -o $(TARGET) $(OBJFILES) $(LIBDIRS) -lfunc2 -lfunc1

clean:
    -rm *.o $(TARGET)

DP.c:

#include "func1.h"
int main()
{
    print1();
    return 0;
}

func1.h:

void print1();

func1.c:

#include <stdio.h>
void print1()
{
    printf("print1 is called!\n");
    print2();
}

func2.h:

extern void print2();

func2.c:

#include <stdio.h>
void print2()
{
    printf("print2 is called!\n");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林迷了鹿 2025-02-11 01:35:02

必须按顺序列出库,需要其符号。

命令 cc $(cflags)-o $(target)$(objfiles)$(libdirs)-lfunc2 -lfunc1 告诉链接器首先使用 func2 库来解决可执行文件中的任何未决引用正在构建,然后使用 func1 库。

由于链接器流程 func2 首先,并且在此过程中没有待处理的引用 print2 ,因此链接器不包括 print2 print2 /代码>在可执行文件中。

稍后,当链接器正在处理 func1 时,它在可执行文件中包含 print1 的模块,因为 main 使用它。该模块 print1 使用 print2 ,因此包括该模块将新的引用添加到 print2 中。然后,当链接器完成处理 func1 时,它具有未解决的参考。链接器不会返回到 func2 再次检查它。

由于 func1 库取决于 func2 ,请将链接命令更改为 cc $(cflags)-O $(target)$(objfiles)$(libdirs) - lfunc1 -lfunc2

(如果 func2 库也取决于 func1 ,那是一个不好的设计,应重新考虑。如果没有更改,请要求链接器多次重新考虑库,与 -lfunc1 -lfunc2 -lfunc1 一样,可能会解决直接问题,但可能会出现其他问题。)

Libraries must be listed in the order their symbols are needed.

The command cc $(CFLAGS) -o $(TARGET) $(OBJFILES) $(LIBDIRS) -lfunc2 -lfunc1 tells the linker to first use the func2 library to resolve any pending references in the executable it is building and then to use the func1 library.

Since the linker processes func2 first and, at the time it does so, there is no pending reference to print2, the linker does not include the module with print2 in the executable.

Later, when the linker is processing func1, it includes the module with print1 in the executable because main uses it. That module print1 uses print2, so including that module adds a new reference to print2. Then, when the linker is done processing func1, it has an unresolved reference. The linker does not go back to func2 to check it again.

Since the func1 library depends on func2, change the link command to cc $(CFLAGS) -o $(TARGET) $(OBJFILES) $(LIBDIRS) -lfunc1 -lfunc2.

(If the func2 library also depends on func1, that is a bad design and should be reconsidered. If it is not changed, asking the linker to reconsider the libraries multiple times, as with -lfunc1 -lfunc2 -lfunc1, might fix the immediate problem, but others can arise.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文