如何编写Makefile来编译简单的C程序

如何编写Makefile来编译简单的C程序

本文介绍了如何编写Makefile来编译简单的C程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译以下程序

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

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

a)-by using file of type Makefile

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

b)-the executable will be named Hello

请帮助您进行练习.我知道如何在CodeBlocks中进行操作,但是我不知道什么是Makefile以及如何在Linux中编写它.我使用命令"gcc filename.c"对其进行了编译,随后"./a.out",但我还是不明白Makefile是什么.它是一种shell脚本,一条指令吗?该任务的Makefile看起来会怎样?在此先感谢:)"

"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 :) "

推荐答案

这是用于hello程序的简单make文件.

This is your simple make file for hello program.

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


default: all

all: Hello

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

clean veryclean:
    $(RM) Hello

假设在一个名为makefile.m1makefile.m2的目录中有两个makefiles,并且如果要构建两个make文件,请使用以下命令

Suppose you have two makefiles in one directory named makefile.m1 and makefile.m2 and if you want build both make file then please use following commands

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

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

or use single Makefile that contains:

m1:
  make -f makefile.m1

m2:
  make -f makefile.m2

并使用make m1make m2

现在,请清除您对Make file名称一定不需要的疑问,该Makefile

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

您可以根据需要命名makefile.假设我想命名为myfirstmakefile.mk.要在以后使用它,您需要告诉make您想要哪个makefile.为此,请使用-f选项:

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:

make -f myfirstmakefile.mk

再说一次引爆.mk也不是强制性的,您可以使用任何您想使用的东西,但永远不要忘记使用-f选项.

And again extantion .mk is also not manadatory you can use whatever you want but never forgot to use -f option.

这对您有帮助吗?

这篇关于如何编写Makefile来编译简单的C程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 13:05