makefile的简单使用

  • 在test(copy)文件夹下创建如下文件,内容如下。

  • 5个文件的内容如下
/*********************main.c**************/

#include "test1.h"  
#include "test2.h"
#include <stdio.h>
int main()
{
        test1_func("hello test1!");
        test2_func("hello test2!");
        return 0;
}


/**********************test1.c***********************/
#include "test1.h"
#include <stdio.h>

void test1_func(char *str)
{
        printf("This is test1 : %s ",str);
}


/*************************test1.h*******************/
#ifndef _TEST1_H
#define _TEST1_H
        void test1_func(char *str);
#endif


/**********************test2.c**********************/
#include "test2.h"
#include <stdio.h>

void test2_func(char *str)
{
        printf("This is test2 : %s",str);
}

/**********************test2.h*********************/
#ifndef _TEST2_H
#define _TEST2_H
        void test2_func(char *str);
#endif
  • makefile文件如下 :

首先两个命令:gcc -c xxx.c xxx.h 的意思是在xxx.c中包含头文件xxx.h,编译器gcc依赖xxx.h编译生成可执行文件.o

                         gcc -o main main.o test1.o test.o   将由test1.c,test2.c,main.c生成的可执行文件链接起来生成目标文件main.lib,然后执行:./main可运行生成文件。

#Makefile
main:main.o test1.o test2.o                #[目标文件]:[依赖关系表]  链接
        gcc -o main main.o test1.o test2.o
main.o:main.c test1.h test2.h           #[需编译文件]:[依赖文件] 编译
        gcc -c main.c
test1.o:test1.c test1.h
        gcc -c test1.c
test2.o:test2.c test2.h
        gcc -c test2.c
.PHONY:clean
clean:
        rm -f *.o main
  • 写好上述文件之后,在main.c的目录下先make, 然后./main即可输出相应结果。
  • 输入make clean即可删除所有中间生成的.o文件。

 

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值