带子目录的makefile文件

1。建立文件夹 dir
  dir中含 三个文件夹 include source pro 和makefile文件。
  include 中包含 hai.h头文件。
  source 中包含 hello.c文件。
  pro 中包含 haia.c haib.c文件。
  
  makefile文件内容有两种写法:
  makefile 第一种写法:
  //*********************
  CC=gcc #选择编译器类型为gcc
  VPATH= include : pr : source #列明文件存放目录
  
  hello: hello.o haia.o haib.o
   $(CC) -o hello hello.o haia.o haib.o
  hello.o:hello.c hai.h
   $(CC) -c hello.c
  haia.o: haia.c hai.h
   $(CC) -c haia.c
  haib.o: haib.c hai.h
   $(CC) -c haib.c
  .PHONY: clean
  clean:
   rm hello $(obj)
  //*************************
  
  
  
  makefile第二种写法(简化版):
  //********************************
  CC=gcc #选择编译种类
  
  VPATH= include : pr : source #在include pr source 三目录下查找文件
  
  obj= hello.o haia.o haib.o #
  
  hello:$(obj) #
   $(CC) -o hello $(obj)
  
  $(obj): hai.h
  
  .PHONY: clean
  clean:
   rm hello $(obj)
  //**********************************
  
  
  hai.h文件内容为:
  //************************
  #include "stdio.h"
  extern void haiprinta();
  extern void haiprintb();
  //****************
  
  hello.c内容为:
  //****************
  #include "../include/hai.h"
  
  int main(void)
  {
  haiprinta();
  haiprintb();
  return 0;
  }
  //**************
  
  haia.c内容为:
  //************
  #include "../include/hai.h"
  
  void haiprinta()
  {
  printf("hai aaaaaaaaaa\n");
  }
  //**************
  
  
  haib.c内容为:
  //**************
  #include "../include/hai.h"
  
  void haiprintb()
  {
  printf("haib bbbbbbbbbbbbbb\n");
  }
  //*******************
  
  2。make 调用makefile生成 可执行文件 hello
  
  3. 运行 hello显示打印结果:
  [HAI@localhost wulong]$ ./hello
  hai aaaaaaaaaa
  haib bbbbbbbbbbbbbb
  [HAI@localhost wulong]$
  
  到此结束,恭喜!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值