makefile:未定义的主要参考

发布于 2025-01-21 10:30:44 字数 795 浏览 2 评论 0原文

我正在研究一个简单的项目,该项目生成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 技术交流群。

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

发布评论

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

评论(2

装纯掩盖桑 2025-01-28 10:30:44

您对user_interface.o的规则是错误的。您需要使用-C选项来告诉它它是创建对象文件而不是可执行文件,因此它不需要main()函数。

all: main Server

main: user_interface.o main.o
    gcc main.o user_interface.o -o main

main.o: main.c
    gcc -c main.c

user_interface.o: user_interface.c
    gcc -c user_interface.o

Server: Server.c
    gcc Server.c -o Server

clean:
    rm -rf main *.o Server

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 a main() function.

all: main Server

main: user_interface.o main.o
    gcc main.o user_interface.o -o main

main.o: main.c
    gcc -c main.c

user_interface.o: user_interface.c
    gcc -c user_interface.o

Server: Server.c
    gcc Server.c -o Server

clean:
    rm -rf main *.o Server

make actually has a built-in rule for compiling .c to .o, so you don't actually need those rules.

一袭白衣梦中忆 2025-01-28 10:30:44

我认为您无法正确使用makefiles。

因此,您的Makefile的一部分看起来像这样:

core: WitchCraft NeuralServer AmoebaServer CDS NLPServer HollyServer

test: ENiX4NeuralCLI DataInjector NNTestServer ENiX4AmoebaCLI CLINLPTest

WitchCraft: ENiX_Net.o ENiX_IPC.o ENiX_SHM.o ENiX_Seq.o ENiX_Packet.o WitchCraft.o ENiX_Config.o ENiX_Disk.o ENiX_Binary.o
        g++ -ggdb -O0 ENiX_Net.o ENiX_IPC.o ENiX_SHM.o ENiX_Seq.o ENiX_Packet.o WitchCraft.o ENiX_Config.o ENiX_Disk.o ENiX_Binary.o -o WitchCraft.bin -std=c++11 -lreadline

在结肠前的这些东西称为目标。可以使用以下方式来调用:

make <target>

例如

make WitchCraft

,对于目标的第一行,您的内部源文件出于某种原因,似乎您正在尝试编译User_interface.o作为二进制文件,而不是对象,但是您'不将其链接到main.o。

因此,我怀疑您想要类似的东西:

main: main.o user_interface.o
    gcc main.o user_interface.o -o main

应该做的是使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:

core: WitchCraft NeuralServer AmoebaServer CDS NLPServer HollyServer

test: ENiX4NeuralCLI DataInjector NNTestServer ENiX4AmoebaCLI CLINLPTest

WitchCraft: ENiX_Net.o ENiX_IPC.o ENiX_SHM.o ENiX_Seq.o ENiX_Packet.o WitchCraft.o ENiX_Config.o ENiX_Disk.o ENiX_Binary.o
        g++ -ggdb -O0 ENiX_Net.o ENiX_IPC.o ENiX_SHM.o ENiX_Seq.o ENiX_Packet.o WitchCraft.o ENiX_Config.o ENiX_Disk.o ENiX_Binary.o -o WitchCraft.bin -std=c++11 -lreadline

These things before the colon, are called targets. The can be called with:

make <target>

e.g.

make WitchCraft

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:

main: main.o user_interface.o
    gcc main.o user_interface.o -o main

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.

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