commonly-makefile

一个编译二进制的Makefile,本Makefile修改自Link

增加或删除文件夹时,只需修改SOURCE_DIRSINCLUDE_DIRS

  • SOURCE_DIRS:表示所有点C目录
  • INCLUDE_DIRS:表示所有点H目录

案例(可直接make):Link

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#
# This Makefile was modified from:
# https://github.com/latelee/Makefile_templet/blob/master/mult_dir_project_new/Makefile
#
# usage: $ make
# $ make CROSS_COMPILE=arm-arago-linux-gnueabi-
#
###############################################################################

# cross compile
CROSS_COMPILE =

CC = $(CROSS_COMPILE)gcc

# name of executable binary
TARGET = main

# directories of .c files, modified here if add/del directory
SOURCE_DIRS = src mod1 mod2

# directories of .h files, modified here if add/del directory
INCLUDE_DIRS = include mod1 mod2

# all .h directories
INCS = $(foreach n, $(INCLUDE_DIRS), -I./$(n))

# all .c files
SRCS = $(foreach n, $(SOURCE_DIRS), $(wildcard $(n)/*.c))

# all .o files
OBJS = $(patsubst %.c,%.o,$(SRCS))

CFLAGS = -Wall -O2
CFLAGS += $(INCS)

LDFLAGS =

###############################################################################

ALL: $(TARGET)

$(TARGET): $(OBJS)
$(CC) $(CFLAGS) $^ -o $(TARGET) $(LDFLAGS)

%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@

.PHONY: clean
clean:
-rm -rf $(TARGET) $(OBJS)