C语言 未定义的引用也许 makefile 是错误的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4130681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Undefined reference maybe makefile is wrong?
提问by Jeremy
I had some issues earlier with declaring my array set of records. Now I think there is something wrong with my Makefile or something.
我之前在声明我的记录数组集时遇到了一些问题。现在我认为我的 Makefile 有什么问题。
Here is my 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
There are more functions I have in my set.c file but these are the functions I am testing at the moment:
我的 set.c 文件中有更多功能,但这些是我目前正在测试的功能:
DisjointSet *CreateSet(int numElements);
DisjointSet *MakeSet(DisjointSet *S,int ele, int r);
void Print(DisjointSet *S);
And the errors I am receiving in the terminal is:
我在终端中收到的错误是:
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'
采纳答案by David Gelhar
The errors that you're getting are linker errors, telling you that while linking your program the linker can't find a function named 'CreateSet' (etc.). It's not immediately obvious why that should be the case, because it appears that you're including "set.o" in the build command.
您得到的错误是链接器错误,告诉您在链接程序时链接器找不到名为“CreateSet”(等)的函数。为什么会出现这种情况并不是很明显,因为您似乎在构建命令中包含了“set.o”。
To troubleshoot build problems, it's often useful to figure out what make is trying to do, and then run the commands individually one at a time so you can see where things go wrong. "make -n" will show you what commands "make" would run, without actually doing them. I would expect to see a command like:
要解决构建问题,弄清楚 make 试图做什么通常很有用,然后一次一个地运行命令,这样您就可以看到哪里出了问题。“make -n”将向您显示“make”将运行哪些命令,而无需实际执行它们。我希望看到如下命令:
gcc -o proj1 main.o set.o
try running that by hand and see where it gets you.
尝试手动运行它,看看它会让你在哪里。
回答by Mud
If these are all on one line in the makefile:
如果这些都在 makefile 中的一行:
EEXEC = proj1 CC = gcc CFLAGS = -c -Wall
Then you have one macro EEXECwhose value is proj1 CC = gcc CFLAGS = -c -Wall, and you have no CCor CFLAGSmacro. CCprobably has a default, which is why that much is working.
然后你有一个EEXEC值为 的宏proj1 CC = gcc CFLAGS = -c -Wall,而你没有CC或CFLAGS宏。CC可能有一个默认值,这就是为什么这么多工作。
回答by slashmais
Make sure you have included set.h in main.c
Also you declare EEXEC but use EXEC...
确保您在 main.c 中包含了 set.h
并且您声明了 EEXEC 但使用了 EXEC ...

