未定义的引用可能makefile错了?

Jer*_*emy 6 c makefile

我之前在声明我的数组记录时遇到了一些问题.现在我认为我的Makefile或其他东西有问题.

这是我的Makefile:

EEXEC = proj1  
CC = gcc  
CFLAGS = -c -Wall  

$(EXEC) :   main.o set.o 
    $(CC) -o $(EXEC) main.o set.o 

main.o  :   main.h main.c
    $(CC) $(CFLAGS) main.c  

set.o   :   set.h set.c
    $(CC) $(CFLAGS) set.c   
Run Code Online (Sandbox Code Playgroud)

我的set.c文件中有更多的函数,但这些是我正在测试的函数:

DisjointSet *CreateSet(int numElements);  
DisjointSet *MakeSet(DisjointSet *S,int ele, int r);  
void Print(DisjointSet *S);
Run Code Online (Sandbox Code Playgroud)

我在终端收到的错误是:

main.o: In function `main':  
main.c:(.text+0x19): undefined reference to `CreateSet'  
main.c:(.text+0x43): undefined reference to `MakeSet'  
main.c:(.text+0x5f): undefined reference to `Print'  
Run Code Online (Sandbox Code Playgroud)

Dav*_*har 7

您获得的错误是链接器错误,告诉您在链接程序时链接器找不到名为"CreateSet"的函数(等).现在并不是很明显为什么会出现这种情况,因为看起来你在构建命令中包含了"set.o".

要解决构建问题,找出make尝试做什么通常很有用,然后逐个单独运行命令,这样你就可以看到出错的地方."make -n"将显示"make"将运行的命令,而不实际执行它们.我希望能看到如下命令:

gcc -o proj1 main.o set.o
Run Code Online (Sandbox Code Playgroud)

尝试手动运行,看看它在哪里.