曲径通幽论坛

标题: Makefile 实践 [打印本页]

作者: beyes    时间: 2009-8-3 18:37
标题: Makefile 实践
实例一

头文件 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’

作者: beyes    时间: 2009-8-4 17:20
实例二
带有子目录的 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);
}





欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) Powered by Discuz! X3.2