Makefile学习笔记

ls -l 查看文件详细信息

1.gcc -E test.c -o test.i
//预编译
gedit test.i //查看:高级C

2.gcc -Wall -S test.i -o test.s
//编译显示所有警告
gedit test.s //查看:介于C与汇编之间的

3. as test.s -o test.o
nano test.o//强行打开是乱码

一步到位:gcc -c test.c
gcc test.o
./a.out

five.c,five.h//新建
gcc -o test test.c five.c //编译为test

gedit Makefile
内容:
test: test.c five.c
gcc -o test.c five.c
clean:rm test


make clean
ls
make
ls
./test

修改其中一个文件,修改Makefile如下,改善编译
test: test.o five.o #源文件
TAB gcc -o test.o five.o #命令
test.o: test.c #源文件
TAB gcc -c test.c #命令
five.o: five.c #源文件
TAB gcc -c five.c #命令
clean:rm test #删除test
TAB rm *.o #删除所有.o

 

make clean


再次修改其中一个文件,修改Makefile如下,改善编译
方便以后修改调整程序
CC=gcc #像宏定义
CFLAGS=-c -Wall #像宏定义
#使用替换 $(。。。)
all:test
test: test.o five.o #源文件
TAB $(CC) -o test.o five.o #命令
test.o: test.c #源文件
TAB $(CC) $(CFLAGS) test.c #命令
five.o: five.c #源文件
TAB $(CC) $(CFLAGS) five.o #命令
clean:rm test #删除test
TAB rm *.o #删除所有.o

 

 

一个简单的makefile示例及其注释
  相信在unix下编程的没有不知道makefile的,刚开始学习unix平台
下的东西,了解了下makefile的制作,觉得有点东西可以记录下。
  下面是一个极其简单的例子:
现在我要编译一个Hello world,需要如下三个文件:
  1. print.h
      #include<stdio.h>
      void printhello();

  2. print.c
      #include"print.h"
      void printhello(){
        printf("Hello, world\n");
      }

   3. main.c
      #include "print.h"
      int main(void){
        printhello();
        return 0;
      }

  好了,很简单的程序了。如果我们想要编译成功需要哪些步骤呢?
我认为在这里需要理解的就两步:
  # 为每一个 *.c文件生成 *o文件。
  # 连接每一个*o文件,生成可执行文件。
下面的makefile 就是根据这样的原则来写的。

 

一:makefile 雏形:


#makefile的撰写是基于规则的,当然这个规则也是很简单的,就是:
#target : prerequisites
  command  //任意的shell 命令

实例如下:
makefile:
    helloworld : main.o print.o #helloword 就是我们要生成的目标
                 # main.o print.o是生成此目标的先决条件
      gcc -o helloworld main.o print.o#shell命令,最前面的一定是一个tab键

    mian.o : mian.c print.h
      gcc -c main.c
    print.o : print.c print.h
      gcc -c print.c
    
    clean :          
        rm helloworld main.o print.o
  OK,一个简单的makefile制作完毕,现成我们输入 make,自动调用Gcc编译了,
输入 make clean就会删除 hellowworld mian.o print.o


二:小步改进:


  在上面的例子中我们可以发现 main.o print.o 被定义了多处,
我们是不是可以向C语言中定义一个宏一样定义它呢?当然可以:
makefile:
    objects = main.o print.o #应该叫变量的声明更合适

    helloworld : $(objects) //声明了变量以后使用就要$()了
      gcc -o helloworld$(objects)
    mian.o : mian.c print.h
      gcc -c main.c
    print.o : print.c print.h
      gcc -c print.c
    
    clean :          
        rm helloworld $(objects)
修改完毕,这样使用了变量的话在很多文件的工程中就能体现出方便性了。


三:再进一步:


  再看一下,为没一个*.o文件都写一句gcc -c main.c是不是显得多余了,
能不能把它干掉?而且 main.c 和print.c都需要print.h,为每一个都写上是
不是多余了,能不能再改进?
能,当然能了:
makefile:
    objects = main.o print.o

    helloworld : $(objects)
      gcc -o helloworld$(objects)
    
    $(objects) : print.h # 都依赖print.h
    mian.o : mian.c #干掉了gcc -c main.c 让Gun make自动推导了。
    print.o : print.c  
  
    clean :          
        rm helloworld $(objects)

好了,一个简单的makefile就这样完毕了,简单吧。

posted @ 2013-12-09 01:36  雨花梦  阅读(362)  评论(1编辑  收藏  举报