使用make编译多个C文件

发布于 2024-08-28 01:22:56 字数 235 浏览 10 评论 0原文

(我运行的是 Linux Ubuntu 9.10,因此可执行文件的扩展名是executablefile.out)我刚刚开始使用 C 进行模块化编程(使用多个文件进行编程),我想知道如何在单个 makefile 中编译多个文件。例如,编译这些文件的 makefile 是什么:main.c、dbAdapter.c、dbAdapter.h? (顺便说一句,如果您还没有弄清楚,主要函数位于 main.c 中)还有人可以发布一个 makefile 文档的链接吗?

(I am running Linux Ubuntu 9.10, so the extension for an executable is executablefile.out) I am just getting into modular programming (programming with multiple files) in C and I want to know how to compile multiple files in a single makefile. For example, what would be the makefile to compile these files: main.c, dbAdapter.c, dbAdapter.h? (By the way, If you haven't figured it out yet, the main function is in main.c) Also could someone post a link to the documentation of a makefile?

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

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

发布评论

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

评论(2

单身狗的梦 2024-09-04 01:22:56

发布的链接都很好。对于您的特殊情况,您可以尝试这个。基本上所有 Makefile 都遵循这种模式。其他一切都是快捷方式和宏。

program: main.o dbAdapter.o
   gcc -o program main.o dbAdapter.o

main.o: main.c dbAdapter.h
   gcc -c main.c

dbAdapter.o dbAdapter.c dbAdapter.h
   gcc -c dbAdapter.c

这里的关键是 Makefile 按顺序查看规则并根据需要进行构建。

它首先会查看程序并发现要构建程序,它需要名为 main.o 和 dbAdapter.o 的东西。

然后它会找到main.o。但是,要构建 main.o,它将需要 main.c 和 dbAdapter.h (我假设 dbAdapter.h 包含在 main.c 中)。

它将使用这些源通过使用 gcc 编译来构建 main.o。 -c 表示我们只想编译。

它对 dbAdapter.o 执行相同的操作。当它拥有这两个目标文件时,就可以链接它们了。此步骤也使用 gcc 编译器。 -o 表示我们正在创建一个名为program的文件。

The links posted are all good. For you particular case you can try this. Essentially all Makefiles follow this pattern. Everything else is shortcuts and macros.

program: main.o dbAdapter.o
   gcc -o program main.o dbAdapter.o

main.o: main.c dbAdapter.h
   gcc -c main.c

dbAdapter.o dbAdapter.c dbAdapter.h
   gcc -c dbAdapter.c

The key thing here is that the Makefile looks at rules sequentially and builds as certain items are needed.

It will first look at program and see that to build program, it needs something called main.o and dbAdapter.o.

It will then find main.o. However, to build main.o, it will need main.c and dbAdapter.h (I assume dbAdapter.h is included in main.c).

It will use those sources to build main.o by compiling it using gcc. The -c indicates the we only want to compile.

It does the same thing with dbAdapter.o. When it has those two object files, it is ready to link them. It uses the gcc compiler for this step as well. The -o indicates that we are creating a file called program.

末蓝 2024-09-04 01:22:56

GNU make 应该就是您正在寻找的。

GNU make should be what you're looking for.

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