一个简单的多目录makefile

在linux下编程,习惯于将.h 文件放在include目录中,.c文件放入src目录中,今天看了一下makefile的教程,写了个简单的makefile,
文件目录如下:

show01---icnlude
            |-------show.h
         src 
            |-------main.c show.c
        Makefile
//show.h
  1 #ifndef _SHOW_H
  2 #define _SHOW_H
  3 #include <stdio.h>
  4 
  5 void show_str();
  6 
  7 #endif

//show.c
  1 #include <stdio.h>
  2 #include "show.h"
  3 
  4 void show_str()
  5 {
  6     printf("hello this is function show_str()\r\n");
  7 
  8 }

//main.c
  1 #include <stdio.h>
  2 #include "show.h"
  3 
  4 int main()
  5 {
  6     show_str();
  7     return 0;
  8 }

//Makefile
  1 #makefile test
  2 
  3 #DIR_INC = ./include
  4 #DIR_SRC = ./src
  5 
  6 #include ${DIR_SRC}/Dir.mk
  7 
  8 hello : main.o show.o
  9     gcc main.o show.o -o hello
 10 main.o : ./src/main.c ./include/show.h
 11     gcc -c ./src/main.c -I ./include
 12 show.o : ./src/show.c ./include/show.h
 13     gcc -c ./src/show.c -I ./include
 14 clean:
 15     rm -rf *.o

使用结果:

ninja@ninja:~/makefiletest/show01$ ls
include  Makefile  src
ninja@ninja:~/makefiletest/show01$ make hello
gcc -c ./src/main.c -I ./include
gcc -c ./src/show.c -I ./include
gcc main.o show.o -o hello
ninja@ninja:~/makefiletest/show01$ ls
hello  include  main.o  Makefile  show.o  src
ninja@ninja:~/makefiletest/show01$ ./hello
hello this is function show_str()
ninja@ninja:~/makefiletest/show01$ make clean
rm -rf *.o
ninja@ninja:~/makefiletest/show01$ ls
hello  include  Makefile  src
ninja@ninja:~/makefiletest/show01$ 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值