makefile:未定义的主要参考
我正在研究一个简单的项目,该项目生成2个可执行文件,主服务器和服务器。
Main.c中的程序使用USER_INTERFACE.C中存在的代码通过标题文件User_interface.h。
我编写的makefile如下所示,
all: main user_interface.o Server
main: user_interface.o main.c
gcc main.c user_interface.o -o main
user_interface.o: user_interface.c user_interface.h
gcc user_interface.c -o user_interface.o
Server: Server.c
gcc Server.c -o Server
clean:
rm -rf main *.o Server
当我在终端上键入MAKE时,我会收到以下错误:
gcc user_interface.c -o user_interface.o
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [Makefile:18: user_interface.o] Error 1
如何通过此错误导航?
I am working on a simple project that generates 2 executable files, main and Server.
The program in Main.c makes use of code present in user_interface.c through the header file user_interface.h.
The Makefile I have written is as follows,
all: main user_interface.o Server
main: user_interface.o main.c
gcc main.c user_interface.o -o main
user_interface.o: user_interface.c user_interface.h
gcc user_interface.c -o user_interface.o
Server: Server.c
gcc Server.c -o Server
clean:
rm -rf main *.o Server
When I type make on the terminal, I get the following error:
gcc user_interface.c -o user_interface.o
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [Makefile:18: user_interface.o] Error 1
How do I navigate past this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您对
user_interface.o
的规则是错误的。您需要使用-C
选项来告诉它它是创建对象文件而不是可执行文件,因此它不需要main()
函数。make
实际上具有编译.c
.o
的内置规则,因此您实际上不需要这些规则。Your rule for
user_interface.o
is wrong. You need to use the-c
option to tell it that it's creating an object file rather than an executable, so it doesn't need amain()
function.make
actually has a built-in rule for compiling.c
to.o
, so you don't actually need those rules.我认为您无法正确使用makefiles。
因此,您的Makefile的一部分看起来像这样:
在结肠前的这些东西称为目标。可以使用以下方式来调用:
例如
,对于目标的第一行,您的内部源文件出于某种原因,似乎您正在尝试编译User_interface.o作为二进制文件,而不是对象,但是您'不将其链接到main.o。
因此,我怀疑您想要类似的东西:
应该做的是使Main.o(IE Main.c)的源代码同样为Interface.o(IE Interface.c)寻找源代码,然后将其编译到对象文件中(即.O文件)。
然后,您将这些对象文件链接到二进制文件中,将GCC与-o一起使用-o在这种情况下指定二进制输出文件“ main”。
而且您需要对服务器进行类似的操作。
I think you're not using makefiles properly.
so a section of your makefile might look something like this:
These things before the colon, are called targets. The can be called with:
e.g.
So for the first line of your target you've got source files inside there for some reason and it looks like you're trying to compile user_interface.o as a binary, rather than an object, but you're not linking it to main.o.
So I suspect you want something like:
And what that should do is cause make to look for the source code for main.o (i.e. main.c) likewise for interface.o (i.e. interface.c) and then compile these into object files (i.e. .o files).
Then you'd link these object files into a binary, using gcc with the -o to specify the binary output file in this case, "main".
And you'd need to do something similar with the server.