Makefile的几个简单示例

时间:2022-09-01 20:59:51

word下载:点击打开链接

 

一、只有一个.c文件时的Makefile书写

 

Makefile的几个简单示例

 

main.c

 

#include <stdio.h>

int main(void)
{
printf("Makefile\n");
return 0;
}


 

Makefile

CFLAGS=-Wall
CC=gcc

main:main.o
$(CC) $(CFLAGS) main.o -o main
main.o:main.c
$(CC) $(CFLAGS) -c main.c -o main.o
clean:
$(RM) main.o
$(RM) main
.PHONY:clean

 

Makefile的几个简单示例

 

Makefile的第二种写法:

CFLAGS=-Wall
CC=gcc

main:main.o
clean:
$(RM) main.o
$(RM) main
.PHONY:clean

 

 

Makefile的几个简单示例

 

 

二、有两个.c文件时,Makefile的书写

Makefile的几个简单示例

 

main.c

#include <stdio.h>

extern void fun1(void);
extern void fun2(void);

int main(void)
{
printf("main\n");
fun1();
fun2();
return 0;
}

fun 1.c

#include <stdio.h>

void fun1(void)
{
printf("fun1\n");
}

fun2.c
#include <stdio.h>

void fun2(void)
{
printf("fun2\n");
}


 

Makefile

CFLAGS=-Wall
CC=gcc

main:main.o fun1.o cs2/fun2.o
clean:
$(RM) *.o
$(RM) cs2/*.o
$(RM) main
.PHONY:clean

Makefile的几个简单示例

 



 

Makefile的另一种写法:

CFLAGS=-Wall
CC=gcc

main:main.o fun1.o cs2/fun2.o
$(CC) $(CFLAGS) $^ -o $@
main.o:main.c
$(CC) $(CFLAGS) -c $^ -o $@
fun1.o:fun1.c
$(CC) $(CFLAGS) -c $^ -o $@
cs2/fun2.o:cs2/fun2.c
$(CC) $(CFLAGS) -c $^ -o $@
clean:
$(RM) *.o
$(RM) cs2/*.o
$(RM) main
.PHONY:clean


Makefile的几个简单示例 

三、嵌套Makefile的书写

Makefile的几个简单示例

其中makefiletest目录下的Makefile为总控Makefile,通过make命令读取总控Makefile,然后总控Makefile在依次读取子目录下的Makefile

总控Makefile

CC=gcc
CFLAGS=-Wall -g

SUBDIR=d1 \
d2 \
d3 \
d4 \
main \
obj

OBJS=d1.o d2.o d3.o d4.o main.o
BIN=myapp

OBJDIR=obj
BINDIR=bin

export CFLAGS CC SUBDIR OBJS OBJDIR BIN BINDIR

all:checkdir $(SUBDIR)
checkdir:
mkdir -p $(BINDIR)

//注意:$(SUBDIR)后面应该有依赖文件,否则make -C $@没法执行,或者将目录名全部声明为伪目标
$(SUBDIR):huixian	make -C $@  ##是$@,而不能是$(SUBDIR)huixian:	@echo $(SUBDIR)	@echo begin!clean:	@$(RM) $(OBJDIR)/*.o	@rm -rf $(BINDIR).PHONY:clean注意:Makefile的注释必须以##开头,并且需顶头写。

 

main/main.c

#include "../d4/d4.h" 
extern void fun2(void);
extern void fun3(void);
extern void fun1(void);
int main(void)
{
fun1();
fun2();
fun3();
fun4();

return 0;
}


 

main/Makefile

../$(OBJDIR)/main.o:main.c
$(CC) -c $(CFLAGS) $< -o $@


 

include/myinclude.h

#ifndef __MYINCLUDE_H__
#define __MYINCLUDE_H__

#include <stdio.h>

#endif


 

d1/d1.c

#include "../include/myinclude.h"

void fun1(void)
{
printf("fun1\n");
return ;
}

d1/Makefile

../$(OBJDIR)/d1.o:d1.c
$(CC) $(CFLAGS) -c $< -o $@



 

d2/d2.c

#include "../include/myinclude.h"

void fun2(void)
{
printf("%s\n","this is fun2");
}

d2/Makefile

../$(OBJDIR)/d2.o:d2.c
$(CC) $(CFLAGS) -c $^ -o $@


d3/d3.c

#include "../include/myinclude.h"

void fun3(void)
{
printf("%s\n","this is fun3");
}

d3/Makefile

../$(OBJDIR)/d3.o:d3.c
$(CC) $(CFLAGS) -c $^ -o $@


d4/d4.h

#ifndef __D4_H__
#define __D4_H__

#define N 4

extern void fun4(void);

#endif

d4/d4.c

#include "../include/myinclude.h"
#include "d4.h"

void fun4(void)
{
printf("this is fun4-->%d\n",N);
}

d4/Makefile

../$(OBJDIR)/d4.o:d4.c
$(CC) $(CFLAGS) -c $< -o $@


make

Makefile的几个简单示例


进入bin/运行myapp


Makefile的几个简单示例


make以后目录的变化

Makefile的几个简单示例



 

make clean 以后目录的变化

Makefile的几个简单示例