曲径通幽论坛

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

Makefile 实践

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34395
跳转到指定楼层
楼主
发表于 2009-8-3 18:37:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
实例一

头文件 head.h
#define MAXSIZE 128

int test_func1 (int);
int test_func2 (int, int);

fun1.c 文件
#include "head.h"

int test_func1 (int num)
{
    int fun1;
     fun1 = num + MAXSIZE;
    return fun1;
}

fun2.c 文件
#include "head.h"

int test_func2(int a, int b)
{
    int new;
    new = a + b + MAXSIZE;

    return new;
}

Makefile 文件
hello.exe:fun1.o fun2.o hello.o
    gcc fun1.o fun2.o hello.o -o hello.exe

hello.o:hello.c fun1.o fun2.o
    gcc -Wall -g -c hello.c -o hello.o

fun1.o: fun1.c head.h
    gcc -Wall -g -c fun1.c -o fun1.o

fun2.o: fun2.c head.h
    gcc -Wall -g -c fun2.c -o fun2.o

make 运行结果
beyes@linux-beyes:~/C/Make> make
gcc -Wall -g -c fun1.c -o fun1.o
gcc -Wall -g -c fun2.c -o fun2.o
gcc -Wall -g -c hello.c -o hello.o
gcc fun1.o fun2.o hello.o -o hello.exe

注意
如果在头文件 head.h 中没有对 fun1.c 和 fun2.c 两个文件中的函数进行声明,那么就会输出警告信息:
hello.c:10: warning: implicit declaration of function ‘test_func1’
hello.c:12: warning: implicit declaration of function ‘test_func2’

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34395
沙发
 楼主| 发表于 2009-8-4 17:20:14 | 只看该作者
实例二
带有子目录的 Makefile ,使用 make 的隐式规则。实例程序结构如图:


printhello11.c 代码
#include <stdio.h>

void printhello11 ()
{
    printf("hello_11\\n");
}

printhello12.c 代码
#include <stdio.h>

void printhello12 ()
{
    printf("hello_12\\n");
}

printhello21.c 代码
#include <stdio.h>

void printhello21 ()
{
    printf("hello_21\\n");
}

printhello22.c 代码
#include <stdio.h>

void printhello22 ()
{
    printf("hello_22\\n");
}

main.c 代码
#include <stdio.h>

int main (void)
{
    printhllo11();
    printhllo12();
    printhllo21();
    printhllo22();

    return (0);
}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-5-3 23:23 , Processed in 0.078147 second(s), 21 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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