C语言 如何编写 Makefile 来编译一个简单的 C 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21548464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:44:03  来源:igfitidea点击:

How to write a Makefile to compile a simple C program

cunixmakefile

提问by Zhivago

Compile the following program

编译以下程序

#include <stdio.h>
int main(void)
{
   printf ("Hello from your first program!\n");
   return 0;
}

a)-by using file of type Makefile

a)-通过使用 Makefile 类型的文件

b)-the executable will be named Hello

b)-可执行文件将被命名为 Hello

"Please help to do an exercise. I know how to do it in CodeBlocks, but I don't know what Makefile is and how to write it in Linux. I compiled it using command "gcc filename.c" and subsequently "./a.out" but I still don't understand what the Makefile is. Is it a sort of shell script, an instruction? How would a Makefile for this task exactly look? Thanks in advance :) "

“请帮忙做一个练习。我知道如何在 CodeBlocks 中进行,但我不知道 Makefile 是什么以及如何在 Linux 中编写它。我使用命令“gcc filename.c”编译它,随后使用“./ a.out”,但我仍然不明白 Makefile 是什么。它是一种 shell 脚本吗,一个指令?这个任务的 Makefile 看起来如何?提前致谢:)”

回答by Jayesh Bhoi

This is your simple make file for hello program.

这是 hello 程序的简单 make 文件。

CC      = gcc
CFLAGS  = -g
RM      = rm -f


default: all

all: Hello

Hello: Hello.c
    $(CC) $(CFLAGS) -o Hello Hello.c

clean veryclean:
    $(RM) Hello

Suppose you have two makefilesin one directory named makefile.m1and makefile.m2and if you want build both make file then please use following commands

假设你有两个makefiles在一个目录中名为makefile.m1makefile.m2和,如果你想建立两个make文件,那么请使用以下命令

make -f makefile.m1
make -f makefile.m2

or use single Makefilethat contains:

或使用Makefile包含以下内容的单曲:

m1:
  make -f makefile.m1

m2:
  make -f makefile.m2

and use make m1or make m2

并使用make m1make m2

Now lets clear your doubt about name of make file must not require Makefile

现在让我们消除您对 make 文件名称的疑问一定不需要 Makefile

You can name makefile whatever you want. suppose i would like to give name myfirstmakefile.mk. To use it later you need to tell make what makefile you want. Use -f option for this:

你可以随意命名makefile。假设我想给出名字myfirstmakefile.mk。要稍后使用它,您需要告诉 make 您想要什么 makefile。为此使用 -f 选项:

make -f myfirstmakefile.mk

And again extantion .mkis also not manadatory you can use whatever you want but never forgot to use -foption.

同样,扩展也不.mk是强制性的,您可以使用任何您想要的东西,但永远不会忘记使用-f选项。

so may this help make sense to you.

所以这对你有帮助。

回答by Jens

A makefileis a recipe for the makeutility how to create some file (called a target) from some other files (called dependencies) using a set of commands run by the shell. A makefile typically looks like this:

一个makefile文件是为配方make效用如何创建一些文件(称为目标使用一组由shell中运行命令的一些其他文件(称为依赖))。生成文件通常如下所示:

target: dependency [...]
        command1
        command2

Try running man makefor details.

尝试运行man make以了解详细信息。

Now for your task, really there is no need for a Makefile, since makehas built-in rules that know how to compile a simple program. All you need to do is place your C source in a file named after the executable name (Hello) and with a .cextension, i.e. Hello.c.

现在对于你的任务,真的不需要 Makefile,因为make有知道如何编译一个简单程序的内置规则。您需要做的就是将您的 C 源代码放在一个以可执行文件名称 ( Hello)命名并带有.c扩展名的文件中,即Hello.c.

Then a simple

然后一个简单的

$ make Hello
cc     Hello.c   -o Hello

does everything. If you want to use gcc instead of cc, you can run

无所不能。如果你想使用 gcc 而不是 cc,你可以运行

$ rm Hello
$ make CC=gcc Hello
gcc     Hello.c   -o Hello

If you tell your instructor/teacher/prof that an empty makefile is all you need since you know the built-in rules do the right thing, you'll get some extra credit and maybe your instructor has learnt something new :-) If you are asked for a reference, you could quote the relevant parts of the make manual, or, do it like a pro, quote from the POSIX Standard for the make utility, section Default Rules.

如果你告诉你的导师/老师/教授你只需要一个空的 makefile,因为你知道内置规则做正确的事情,你会得到一些额外的学分,也许你的导师学到了一些新的东西:-) 如果你如果要求参考,您可以引用 make 手册的相关部分,或者像专业人士一样引用make 实用程序POSIX 标准部分Default Rules

回答by vishesh

before going for makefile you have to know what's it and why we need it

在使用 makefile 之前,您必须知道它是什么以及我们为什么需要它

What is Makefile?

什么是生成文件?

Makefile is a script written in a certain prescribed syntax which helps to build the target output (normally, one or more executables) from source files by compilation and linking. In simple words, makefile will compile your source code in simple & fast way. Why we need Makefile?

Makefile 是一种以某种规定的语法编写的脚本,它有助于通过编译和链接从源文件构建目标输出(通常是一个或多个可执行文件)。简而言之,makefile 将以简单快速的方式编译您的源代码。为什么我们需要Makefile?

=> Large projects can contain multiple source files which are dependent in one another or arranged in hierarchical manner for example, in order to compile file A, you have to first compile B; in order to compile B, you have to first compile C; and so on.

=> 大型项目可以包含多个相互依赖或分层排列的源文件,例如,为了编译文件A,您必须先编译B;为了编译B,你必须先编译C;等等。

=> Make is a solution to these problems. It can be used to compile whole project in well arranged manner and generate your target according to your make rule(which we will discuss later) by entering single command that is make.

=> Make 是解决这些问题的方法。它可用于以良好的方式编译整个项目,并通过输入单个命令 make 根据您的 make 规则(我们将在稍后讨论)生成您的目标。

=> An important feature is that when a project is recompiled after a few changes, it will recompile only those files which are changed, and any other files that are dependent on it. This saves a lot of time.

=> 一个重要的特性是,当一个项目在一些更改后重新编译时,它只会重新编译那些被更改的文件,以及任何其他依赖于它的文件。这样可以节省很多时间。

=> For a large project, when a few changes are made to the source, manually recompiling the entire project each time is tedious, error-prone and time-consuming.

=> 对于大型项目,当对源代码进行少量更改时,每次手动重新编译整个项目是乏味、容易出错且耗时的。

Here is nice link for it :How to write first makefile

这是它的不错链接:如何编写第一个 makefile

回答by Aaron Digulla

A makefile is a recipe for computers with instructions how to perform certain tasks and with dependencies between those tasks.

生成文件是计算机的配方,其中包含如何执行某些任务以及这些任务之间的依赖关系的说明。

In the simple form, it looks like so:

在简单的形式中,它看起来像这样:

a.out: filename.c
        gcc filename.c

Read: "To build a.outfrom filename.c, run the command gcc filename.c. If a.outis newer than filename.c, then don't do anything"

阅读:“打造a.outfilename.c运行命令gcc filename.c。如果,a.out是不是新的filename.c,然后什么也不做”

Note: The first character in the gccline mustbe a tab.

注:在第一个字符gcc必须是一个标签。