makefile的简单制作

makefile 的好处—–“自动化编译”
makeile 需要用make 命令工具
make是解释makefile 中的命令的工具

预编译
  将.c 文件转化成 .i文件
  使用的gcc命令是:gcc –E
编译
  将.c/.h文件转换成.s文件
使用的gcc命令是:gcc –S
汇编
  将.s 文件转化成 .o文件
  使用的gcc 命令是:gcc –c
链接
  将.o文件转化成可执行程序
  使用的gcc 命令是: gcc -o
  
makefile 的编写规则:
target : prerequisites
command

target 可以是目标文件 gcc -c 后的.o文件
也可以是最终的可执行文件
command 是shell命令,需要tab键进行开头
makefile 其实是解决文件依赖关系
target 是依赖 prerequisites 的,也就是说只要
prerequistites里面只要有一个比target新的话,
就执行command

下面使用makefile写的插入排序算法:

union.h文件

#include<stdio.h>
#include<stdlib.h>

main.h文件

#include"swap.h"
#include"show.h"
#include"union.h"

main.c文件

#include"main.h"
#include<unistd.h>
#define length 10
int main()
{


    int str [length];
    int i;
    for(i = 0;i< length;i++)
    {
        str[i] = rand()%100;
    }
    printf("原数组:\n");
    show(str,length);

    sleep(2);
    int j;
    int k;
    for(j = 1;j < length ;j++)
    {
        for(k = j -1;k>= 0 && str[k] > str[k+1];k--)
        {
            swap(&str[k],&str[k+1]);
        }
    }
    printf("\n");
    printf("排序后数组:\n");
    show(str,length);
    return 0;
}

show.h文件

#include"union.h"
void show(int str[],int length);

show.c 文件


#include"show.h"
void  show(int str[],int length)
{
    if(NULL  == str)
    {
        printf("str is error");
    }
    int i;
    for(i =0 ; i< length ;i++)
    {
        printf("  %d",str[i]);
    }
    return  ;
}

swap.h文件


#include<string.h>
#include"union.h"
#ifndef _SWAP_H_
#define _SWAP_H_

void swap(int *a,int *b);

#endif

swap.c文件

#include"swap.h"
void swap(int *a,int *b)
{


    int * temp = (int *)malloc(sizeof(int));
    *temp  = *a;
    *a     = *b;
    *b     = *temp;
    return ;
}

Makefile文件

inset_sort :  main.o  swap.o    show.o 
    gcc   main.o   swap.o  show.o   -o   inset_sort
main.o   :   main.h union.h  main.c
    gcc  -c    main.c
show.o   :  show.c  union.h
    gcc  -c    show.c
swap.o   :   swap.c  swap.h
    gcc   -c     swap.c
clean :
    rm      inset_sort  show.o   swap.o     main.o
    ls

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值