多个Makefile文件编译,Makefile多目标编译和多层次编译
生活随笔
收集整理的这篇文章主要介绍了
多个Makefile文件编译,Makefile多目标编译和多层次编译
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
多个Makefile文件编译,Makefile多目标编译和多层次编译
- README
- Makefile多目标编译
- Makefile多层次编译
- a文件夹
- b文件夹
- include文件夹
- obj文件夹
- 多个Makefile文件编译
- 相关截图
- make文件夹
- a_b文件夹
- project2文件夹
README
文件夹以及文件说明。
根文件夹为make,实现多个Makefile文件编译。
Makefile多目标编译
在文件夹a_b中实现。
main_a.c: 第一个目标主文件
test_a.c:第一个目标printf功能函数文件
#include <test_a.h>void print_a() {printf( "This is test_a.c!\n" );}test_a.h:第一个目标功能函数声明头文件
#ifndef _TEST_A_H #define _TEST_A_H#include <stdio.h> #include <stdlib.h>void print_a();#endifmain_b.c:第二个目标主文件
#include <stdio.h> #include <stdlib.h> #include <test_b.h>int main() {print_b();return 0;}test_b.c:第二个目标printf功能函数文件
#include <test_b.h>void print_b() {printf( "This is test_b.c!\n" );}test_b.h:第二个目标功能函数声明头文件
#ifndef _TEST_B_H #define _TEST_B_H#include <stdio.h> #include <stdlib.h>void print_b();#endifMakefile:二级文件夹a_b多目标编译的Makefile文件
CC = gcc CFLAGS = -lm -Wall -g#get the posotion of current directory ROOT = $(PWD) #get the posotion of current library INCLUDE = -I$(ROOT) APP = $(ROOT)/obj#all the out files of first target OBJS1 = main_a.o \test_a.o#all the out files of second target OBJS2 = main_b.o \test_b.o #first and second target TARGETS1 = $(APP)/main_a TARGETS2 = $(APP)/main_b #total tagets TARGETS = $(TARGETS1) TARGETS += $(TARGETS2) #include the path of library CFLAGS += $(INCLUDE)OBJS = $(OBJS1) OBJS += $(OBJS2)all:$(TARGETS)$(TARGETS1):$(OBJS1)$(CC) -o $(TARGETS1) $(OBJS1) $(CFLAGS)$(TARGETS2):$(OBJS2)$(CC) -o $(TARGETS2) $(OBJS2) $(CFLAGS)clean:-$(RM) $(OBJS) -$(RM) $(TARGETS)Makefile多层次编译
Makefile文件
ROOT = $(PWD)A = $(ROOT)/a B = $(ROOT)/b INCLUDE = $(ROOT)/include APP = $(ROOT)/objCFLAGS = -I$(A) CFLAGS += -I$(B) CFLAGS += -I$(ROOT) CFLAGS += -I$(INCLUDE)OBJS = main.o $(A)/a.o $(B)/b.o c.o TARGET = $(APP)/appall:$(TARGET)$(TARGET):$(OBJS)$(CC) -o $(TARGET) $(OBJS) $(CFLAGS)clean:-$(RM) $(OBJS)-$(RM) $(TARGET)c.c:c()功能函数实现
#include <stdio.h> #include <stdlib.h> #include <c.h> #include <code.h>void c(){printf( "%d: This is c function!\n" , C );}c.h:c()功能函数声明头文件
#include<stdio.h> void c();main.c:project2工程实现主函数,调用a(),b(),c()函数接口
#include <stdio.h> #include "a.h" #include "b.h" #include "c.h"void main(){a();}a文件夹
三级文件夹: a功能函数实现
a.c:a()功能函数实现
a.h:a()功能函数声明头文件
void a();b文件夹
三级文件夹: b功能函数实现
b.c:b()功能函数实现
b.h:b()功能函数声明头文件
#include <stdio.h>void b();include文件夹
code.h:相关常量宏定义头文件
#include <stdio.h> #include <stdlib.h>#define A 1 #define B 2 #define C 3obj文件夹
编译前为空文件夹,编译时为project2工程目标文件生成的指定位置
多个Makefile文件编译
Makefile:位于根文件夹make中
ROOT = $(PWD) SUBDIR = $(ROOT)/a_b SUBDIR += $(ROOT)/project2define make_subdir @for i in $(SUBDIR); do \( cd $$i && make $1 ) \ done; endefALL: $(call make_subdir)clean:$(call make_subdir , clean)相关截图
make文件夹
a_b文件夹
project2文件夹
/project2/a 文件夹
/project2/b 文件夹
/project2/include 文件夹
/project2/obj 文件夹
总结
以上是生活随笔为你收集整理的多个Makefile文件编译,Makefile多目标编译和多层次编译的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: PCI-E 1X金手指封装
- 下一篇: CleanMyMac4.12.2最新免费