有人可以告诉我如何创建这个非常简单的 makefile 吗?

发布于 2024-11-07 07:27:48 字数 240 浏览 0 评论 0原文

这是用 C 编写的,我通过 Putty 运行 UNIX,并想要创建一个 Makefile。

所以我有一个程序,它只在一个文件“recommender.c”中,我需要制作一个 makefile,使用 all 函数创建一个名为recommender 的可执行文件。我不知道该怎么做的部分是我使用行 gccRecommender.c -std=gnu99 编译recommender.c 我不知道如何确保它也执行 -std=gun99 部分。请帮忙? :)

This is in C, am running UNIX through Putty and want to create a Makefile.

So I have a program that is in only one file "recommender.c" and i need to make a makefile that creates an executable file called recommender using the all function. The part i don't know how to do is that i compile recommender.c using the line gcc recommender.c -std=gnu99
I don't know how to make sure it does the -std=gun99 part too. please help? :)

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

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

发布评论

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

评论(2

深府石板幽径 2024-11-14 07:27:48
CFLAGS=-std=gnu99 -Wall -Wextra -O3

all: recommender

recommender: recommender.o

请注意,文件扩展名必须是 .c,如果它的扩展名为 .cpp,则需要 CXXFLAGS 而不是 CFLAGS

一些解释:

make 文件是基于依赖关系的,您可以看到您可以在 make 文件顶部设置变量。然后,当调用 make 程序时,它将在当前目录中搜索 Makefile

由于默认操作是 all ,因此它将搜索 all 规则。
当它找到 all 规则时,它将检查需要哪些依赖项(在本例中是 recommender 规则/文件),以便它再次开始搜索规则,并发现它需要 < code>recommender.o 规则/文件,但找不到任何内容,现在这就是神奇发生的地方。

make 足够聪明,知道 .o(对象)文件是由 .c 或 .cpp(源)文件生成的,因此现在它将开始在当前目录中搜索源文件。他通过将 .o 替换为 .c 来实现这一点,如果找到该文件,它将使用 gcc 编译器对其进行编译(如果它在哪里找到 .cpp) 文件,它将使用 g++ 编译器进行编译。

现在我们知道了,我们可以通过添加更多目标文件作为依赖项来简单地将多个源文件添加到项目中。

现在,您实际上并不需要首先创建目标文件,因为您是从一个源文件进行编译的,您还可以删除 recommender:recommender.o 规则,并且 make 将尝试创建 recommender< /code> 通过编译 recommender.c.cpp 应用程序

CFLAGS=-std=gnu99 -Wall -Wextra -O3

all: recommender

recommender: recommender.o

Note that the file extension must be .c if it has the extension .cpp it will need the CXXFLAGS instead of CFLAGS

Some explanation:

The make file is dependency based as you can see you can set your variables on top of the make file. Then when the make program is invoked it will search for a Makefile in the current directory.

Since the default action is all it will search for the all rule.
When it found the all rule it will check which dependencies are needed in this case the recommender rule / file so it starts searching the rules again and it finds out it needs the recommender.o rule / file but it won't find any, now here is where the magic happens.

make is smart enough to know that an .o (object) file is made from an .c or .cpp (source) file so now it will start searching for the source file in the current directory. This he does by replacing the .o with an .c if it finds the file it will compile it with the gcc compiler if it where to find an .cpp file it will compile it with the g++ compiler.

So now we know this we can simply add multiple source files to the project by adding more object file as dependencies.

Now you don't really need to create an object file first since you compile from one source file you can also remove the recommender: recommender.o rule and make will try to create the recommender application by compiling recommender.c or .cpp

终难遇 2024-11-14 07:27:48

你甚至不需要 makefile; CFLAGS="-std=gnu99" makeRecommender 应该“正常工作”。

You don't even need a makefile; CFLAGS="-std=gnu99" make recommender should "just work".

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