我可以为单个 C 文件工作的最简单的 MAKEFILE 是什么?
我有一个名为“main.c”的文件。我可以将此文件编译成可以像 ./blah
一样运行的可执行文件的最简单的 Makefile 是什么?
I have a file called "main.c". Whats the simplest Makefile I can have to compile this file into an executable that I can run like ./blah
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这里不需要 makefile,简单的 shell 脚本就可以了。
You don't need makefile here, simple shell script is OK.
如果您正在运行 GNU Make 并且不需要链接额外的库,那么最简单的 makefile 根本就是没有 makefile。尝试:
如果您不想指定
main
,那么您可以使用以下一行 Makefile:GNU Make 有几个隐式规则,如果您自己不定义它们,它就会使用它们。使这项工作有效的方法类似于:
有关更多信息,请参阅: http://www.gnu.org/software/make/manual/make.html#Using-Implicit
If you're running GNU Make and if you don't need to link in extra libraries, the simplest makefile is no makefile at all. Try:
If you don't want to have to specify
main
, then you can use the following one-line Makefile:GNU Make has several implicit rules that it uses if you don't define them yourself. The one that makes this work is something like:
For more info, see: http://www.gnu.org/software/make/manual/make.html#Using-Implicit
我建议的最简单的方法是:
通过隐式规则,这将导致在链接之前将 myprog.c 编译为 myprog.o 的单独步骤。我比其他答案(Arnaud)更喜欢这个的原因是它可以扩展到更多源文件,而不必显着扩大 makefile:
The simplest I would recommend is:
Via implicit rules, this will result in a separate step that compiles
myprog.c
tomyprog.o
before linking it. The reason I like this better than the other answer (by Arnaud) is that it scales to more source files without having to enlarge the makefile significantly:正如 Jander 所说,如果您的源代码是一个名为
blah.c
的文件,您可以使用它来输出名为blah
的可执行文件:甚至:
使用 GNU 时,其他所有内容都是隐式的
make
虽然这非常简单,但它确实比一些更复杂的答案有一些优势:
gcc
!*.o
文件。也就是说,我强烈建议您通过至少添加
clean
和all
规则以及-Wall
来改进您的Makefile
旗帜。这可以通过以下Makefile
来完成,仍然保留上述所有优点:As Jander said, if your source is a single file named
blah.c
you could use this to output an executable namedblah
:Or even:
Everything else is implicit when using GNU
make
While this is extremely simple, it does have a few advantages over some of the more complex answers:
gcc
for you!*.o
files which are not needed for this simple project.That said, I strongly suggest you to improve your
Makefile
by adding at leastclean
andall
rules and-Wall
flags. This can be done with the followingMakefile
, still keeping all the above advantages: