makefile简介及其简单编写示例

来源:互联网 发布:windows商店 下载 编辑:程序博客网 时间:2024/04/30 03:53

makefile文件里面主要有三种内容:


  1.变量声明:

  变量声明就是一种基本的严格字符替换的操作。

  比如在前面声明了:objects=program.o foo.o utils.o

  那么在后面出现的所有$(objects)或者${objects}都会被自动替换成上面的那个字符序列,而且是严格替换,即不带空格的。

  

  2.映射法则

  

  3.命令:

  映射法则和命令通常都是联合起来组成这样的结构形式:

  target... : prerequisites..

  command

  

  可以简单地理解为通过prerequisites,也就是先决的依赖文件,采取后面描述的相应的命令(这里的命令都是linux里的shell命令)command之后(一般是)生成了文件target。命令的前面都要按以下tab建留一段空白来表示它是命令。

  有的target后面并没有先决条件,也就是后面的命令是无条件执行的。


makefile 文件c语言程序:

#include <linux/init.h>

#include <linux/module.h>

MODULE_LICENSE("Dual BSD/GPL");


static int hello_init(void)

{

printk(KERN_ALERT "Hello, world\n");

return 0;

}

static void hello_exit(void)

{


printk(KERN_ALERT "Goodbye, cruel world\n");

}


module_init(hello_init);

module_exit(hello_exit);


makefile

# If KERNELRELEASE is defined, we've been invoked from the

# kernel build system and can use its language.

ifneq ($(KERNELRELEASE),)


obj-m := hello.o 

# Otherwise we were called directly from the command

# line; invoke the kernel build system.

else


KERNELDIR ?= /lib/modules/$(shell uname -r)/build

PWD := $(shell pwd) 

default:

$(MAKE) -C $(KERNELDIR) M=$(PWD) modules


endif






makefile里面所写的内容其实就是你要编译的命令,那么,什么是编译命令呢?

假写你已经写好一个程序代码,并将之存在一个.c文件中,如:hello.c,在终端上你可以这样做!在终端上输入gcc -o hello hello.c

然后回车,看一看有没有什么反映,如果没有打出很多英文的话,恭喜你!你完美地完成了第一步!然后,在终端中输入./hello看看是不是有什么输出了?

现在来解释一下编译命令:上面的命令的意思就是,使用gcc编译器编译hello.c源代码,生成的文件名称叫做hello.最后,要看程序运行结果,就要运行生成的程序也就是“./hello”了,“./”的意思就是在当前的目录下运行。

makefile中内容的就是上面的编译命令,如:在makefile文件中写入

Hello:hello.c

  gcc -o Hello hello.c

保存文件之后直接在终端中输入make,就完成编译了!makefile存在的意义只是让编译更加方便,也就说,可以把所以的编译都写在一个makefile文件中,然后在终端中输入make就可以完成makefile文件里的命令!



怎么写这个程序的Makefile文件


#include <stdio.h>

#include "sin_value.h"

#include "cos_value.h"

#include "haha.h"

#define pi 3.14159

char name[15];

float angle;

int main(void)

{

 printf ("\n\nPlease input your name: ");

 scanf  ("%s", name );

 printf ("\nPlease enter the degree angle (ex> 90): " );

 scanf  ("%f", &angle );

 haha( name );

 sin_value( angle );

 cos_value( angle );

 return 0;

}


这是main.c文件,其中的sin_value.hcos_value.hhaha.h分别如下:

#include <stdio.h>

#include <math.h>

#define pi 3.14159

void sin_value(float angle)

{

 float value;

 value = sin ( angle / 180. * pi );

 printf ("\nThe Sin is: %5.2f\n",value);

}                   //这是sin_value.h


#include <stdio.h>

#include <math.h>

#define pi 3.14159

void cos_value(float angle)

{

 float value;

 value = cos ( angle / 180. * pi );

 printf ("The Cos is: %5.2f\n",value);

}                                //这是cos_value.h


#include <stdio.h>

int haha(char name[15])

{

 printf ("\n\nHi, Dear %s, nice to meet you.", name);

 return 0;

}                     //这是haha.h


程序没有问题,不用makefile的方法我已经运行通过了,麻烦直接给出可以copyMakefile,不要给教程什么,拜谢!!!另外是不是这个Makefile文件要放在程序那个文件夹里啊?



提问者采纳


Makefile文件名字,放在这个你这个程序的目录中:

prog=app

OBJS=sin_value.o cos_value.o haha.o main.c

LDFLAGS:=  #这个是编译参数例:-lpthread线程库

CFLAGS:= -g -Wall #可调式,以及编译警告通知

all:$(prog)


sin_value.o:sin_value.c

        $(CXX) $(CFLAGS) -c -o $@ $^ 

cos_value.ocos_value.c

        $(CXX) $(CFLAGS) -c -o $@ $^ 

haha.o: haha.c

       $(CXX) $(CFLAGS) -c -o $@ $^ 

main.o:main.c

       $(CXX) $(CFLAGS) -c -o $@ $^ 


install:

        cp $(prog) /bin

        

clean:

        rm -rf *.o  *~

        rm -rf $(prog)

        rm -rf /bin/$(prog)


$(prog):$(OBJS)

        $(CXX) $(LDFLAGS) -o $@ $^



[精华]跟我一起写 Makefile

http://www.chinaunix.net/old_jh/23/408225.html


Linux/Unix环境下的makemakefile详解

http://blog.csdn.net/lcj_cjfykx/article/details/9003279


教会你如何编写makefile文件

http://blog.csdn.net/wallwind/article/details/6791505

1 0
原创粉丝点击