曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 4988|回复: 0
打印 上一主题 下一主题

makefile 相关

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34387
跳转到指定楼层
楼主
发表于 2009-8-4 22:53:38 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
      模式规则
在编写 Makefile 规则时,除了隐式规则经常使用外,还会使用模式规则。模式规则不同于隐式规则,隐式规则仅能够用 make 默许的变量来进行操作,而模式规则则可以引入用户自定义的变量,更智能的处理多个文件。

GNU make 支持两种类型的模式规则。

( 1 )后缀规则 ( Suffix Rule )
后缀规则是定义模式规则的老风格方法。后缀规则定义了将一系列具有某个后缀的文件 ( 例如 .c 文件 ) 转换为具有另外一种后缀的文件 ( 例如 .o 文件 ) 的方法。每个后缀规则以两个成对出现的后缀名定义。比如将 .c 文件转换为 .o 文件的后缀规则可定义为:
.c.o:
        $(CC) -c -o $@ $<

实例
在一个目录下,有以下几个 .c 文件:
beyes@linux-beyes:~/C/Make/Suffix_Rule> ls
Makefile test1.c   test2.c  test3.c  test.c

各个文件的内容分别为:
test.c :
#include <stdio.h>

int main (void)
{
    printf ("test:hello makefile\n");
    test1 ();
    test2 ();
    test3 ();
    return (0);
}

test1.c :
#include <stdio.h>

int test1 (void)
{
    printf ("test1:hello makefile\n");

    return (0);
}

test2.c :
#include <stdio.h>

int test1 (void)
{
    printf ("test2:hello makefile\n");

    return (0);
}

test3.c :
#include <stdio.h>

int test1 (void)
{
    printf ("test3:hello makefile\n");

    return (0);
}

Makefile 文件如下
CC=gcc
OBJS=test.o test1.o test2.o test3.o

test.exe:$(OBJS)
    $(CC) -o $@ $^

.c.o:
    $(CC) -c -o $@ $<

执行 make 命令
beyes@linux-beyes:~/C/Make/Suffix_Rule> make
gcc -c -o test.o test.c
gcc -c -o test1.o test1.c
gcc -c -o test2.o test2.c
gcc -c -o test3.o test3.c
gcc -o test.exe test.o test1.o test2.o test3.o

执行生成的可执行文件 test.exe :
beyes@linux-beyes:~/C/Make/Suffix_Rule> ./test.exe
test:hello makefile
test1:hello makefile
test2:hello makefile
test3:hello makefile

Makefile 文件说明

OBJS=test.o test1.o test2.o test3.o           #声明 OBJS 变量

test.exe:$(OBJS)                                            #生成可执行 test.exe 文件依赖于这么多个 .o 文件
    $(CC) -o $@ $^                                          #$@ 表示生成目标(test.exe)的完整名称;$^ 表示所有依赖的文件( *.o )


.c.o:                                                                  #后缀规则,一种老风格的规则,要让目录底下所有的 .c 都转换为 .o 文件
    $(CC) -c -o $@ $<                                    #依次查找到 .c 文件并逐个转换为 .o 文件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2024-5-19 14:02 , Processed in 0.068027 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表