Makefile编译所有cpp文件

相关视频讲解:Makefile 20分钟入门 (opens new window)

相关资料:seisman/how-to-write-makefile (opens new window) GNU make (opens new window)

## version 1
# hello: hello.cpp
#         g++ -c hello.cpp getday.cpp
#         g++ -o main hello.o getday.o
# .PHONY: clean
# clean:
#         rm -f main *.o

## version 2
C := g++
CFLAGS := -g
TARGET := test
SRCS := $(wildcard *.cpp)
OBJS := $(patsubst %cpp,%o,$(SRCS))
all:$(TARGET)
%.o:%.cpp
        $(CC) $(CFLAGS) -c $<
        $(TARGET):$(OBJS)
                $(CC) $(CFLAGS) -o $@ $^
clean:
        rm -rf $(TARGET) *.o
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

注意Tab不可以用空格代替(vim 设置中 expandtab 也需要关闭)



 









syntax on
set number
set expandtab //删除 或 set noexpandtab
set autoindent
set smartindent
set tabstop=4
set laststatus=2
set mouse=a
set scrolloff=4
inoremap { {}<ESC>i
inoremap {<CR> {<CR>}<ESC>O
1
2
3
4
5
6
7
8
9
10
11

Buy me a cup of coffee ☕.