Makefile 编译多个C 程序?
这是一个非常简单的问题,但我对 makefile 很陌生。我正在尝试制作一个 makefile 来编译两个独立的程序:
program1:
gcc -o prog1 program1.c
program2:
gcc -o prog2 program2.c
所有在线示例都比我需要的详细得多,并且令人困惑!我真正想要它做的就是运行两行 gcc
行。我做错了什么?
This is an incredibly simple question, but I'm new to makefiles. I am trying to make a makefile that will compile two independent programs:
program1:
gcc -o prog1 program1.c
program2:
gcc -o prog2 program2.c
All the examples online go into way more details than I need and are confusing! All I really want it to do is to run the two gcc
lines. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这样做
你说你不需要高级的东西,但你也可以根据一些默认规则来缩短它。
Do it like so
You said you don't want advanced stuff, but you could also shorten it like this based on some default rules.
模式规则让您编译多个c使用
make
需要相同编译命令的文件如下:Pattern rules let you compile multiple c files which require the same compilation commands using
make
as follows:我更喜欢 ansi 和迂腐的,可以更好地控制你的程序。当你仍然有警告时它不会让你编译!
I rather the ansi and pedantic, a better control for your program. It wont let you compile while you still have warnings !!
一个简单程序的编译流程很简单,我可以把它画成一个小图:源码-> [编译]->对象[链接] ->可执行的。此图中有文件(源、对象、可执行文件)和规则(make的术语)。该图在 Makefile 中定义。
当您启动 make 时,它会读取 Makefile,并检查已更改的文件。如果有的话,它会触发依赖于它的规则。该规则可能会生成/更新更多文件,这可能会触发其他规则等等。如果您创建了一个好的 makefile,则只会运行必要的规则(编译器/链接命令),这些规则代表依赖路径中修改后的文件的“下一个”。
选择一个Makefile示例,阅读语法手册(无论如何,一目了然,无需手册),然后绘制图表。您必须了解编译器选项才能找到结果文件的名称。
制作图应该像您想要的那样复杂。你甚至可以无限循环(不要这样做)!您可以告诉 make,哪个规则是您的目标,因此只有剩下的文件将被用作触发器。
再次:绘制图表!
A simple program's compilation workflow is simple, I can draw it as a small graph: source -> [compilation] -> object [linking] -> executable. There are files (source, object, executable) in this graph, and rules (make's terminology). That graph is definied in the Makefile.
When you launch make, it reads Makefile, and checks for changed files. If there's any, it triggers the rule, which depends on it. The rule may produce/update further files, which may trigger other rules and so on. If you create a good makefile, only the necessary rules (compiler/link commands) will run, which stands "to next" from the modified file in the dependency path.
Pick an example Makefile, read the manual for syntax (anyway, it's clear for first sight, w/o manual), and draw the graph. You have to understand compiler options in order to find out the names of the result files.
The make graph should be as complex just as you want. You can even do infinite loops (don't do)! You can tell make, which rule is your target, so only the left-standing files will be used as triggers.
Again: draw the graph!.
make all
即可:如果设置了
CXX
和CXXFLAGS
变量,make
将使用它们。make all
will do:If you set
CXX
andCXXFLAGS
variablesmake
will use them.这会将
make
上的所有*.c
文件编译为不带.c
扩展名的可执行文件,如gcc program.c -o program 中所示
。make
将自动添加您添加到CFLAGS
的任何标志,例如CFLAGS = -g Wall
。如果您不需要任何标志
CFLAGS
可以留空(如下所示)或完全省略。This will compile all
*.c
files uponmake
to executables without the.c
extension as ingcc program.c -o program
.make
will automatically add any flags you add toCFLAGS
likeCFLAGS = -g Wall
.If you don't need any flags
CFLAGS
can be left blank (as below) or omitted completely.