Makefile C与C++混编的简单写法

用了很久的Linux,感觉还是对Makefile不够熟悉,经常为一个复杂一点的项目的Makefile折腾半天。现在对Makefile的基本写法做一下总结,方便以后查阅。

这里用我近期写的一个Makefile做讲解,是一个C与C++混编的项目。并附上完整的Makefile内容。

目录结构是:

+--include/
+--include/librtmp
+--liblog/
+--librtmp/
+--release/
+--release/liblog
+--release/librtmp
+--xx.cpp
+--Makefile

讲解:

TARGET : 目标文件
OBJ_DIR_THIS : 中间文件存放目录
COMPILE.cpp和COMPILE.c : 编译
LINK.cpp和LINK.c : 链接
SOURCE_PATHS : 源码.c和.cpp存放目录,多个目录用空格隔开
INCLUDE_PATHS : 文件夹.h存放目录,多个目录用空格隔开
foreach : 用于遍历多个目录
wildcard : 用于遍历指定目录的指定文件
RELOBJFILES_cpp和RELOBJFILES_c : .cpp和.c编译后对应的.o文件

make的时候会先执行COMPILE.cpp和COMPILE.c分别把.cpp和.c编译成.o文件,然后再执行LINK.cpp把.o文件以及依赖的库文件链接成目标文件。

示例:

TARGET = rtmp_server

OBJ_DIR_THIS = release

C_FLAGS = -Wall -g

CPP_FLAGS =     -I. \
        -I./include \
        -I./include/librtmp \
        -D_GNU_SOURCE \
        -D_LARGEFILE64_SOURCE \
        -D_FILE_OFFSET_BITS=64 \
        -D__STDC_CONSTANT_MACROS 

LD_FLAGS = -lpthread  


COMPILE.cpp = g++ $(C_FLAGS) $(CPP_FLAGS) -c
LINK.cpp = g++
COMPILE.c = gcc $(C_FLAGS) $(CPP_FLAGS) -c
LINK.c = gcc

RELCFLAGS = -O2 -fno-strict-aliasing

SOURCE_PATHS = . liblog librtmp
SOURCES_cpp = $(foreach dir,$(SOURCE_PATHS),$(wildcard $(dir)/*.cpp))
SOURCES_c = $(foreach dir,$(SOURCE_PATHS),$(wildcard $(dir)/*.c))

INCLUDE_PATHS = . include librtmp
HEADERS = $(foreach dir,$(INCLUDE_PATHS),$(wildcard $(dir)/*.h))

RELOBJFILES_cpp = $(SOURCES_cpp:%.cpp=$(OBJ_DIR_THIS)/%.o)
RELOBJFILES_c = $(SOURCES_c:%.c=$(OBJ_DIR_THIS)/%.o)

OBJ_DIR_PATHS = $(foreach dir,$(SOURCE_PATHS), $(OBJ_DIR_THIS)/$(dir))

.PHONY: clean mkdir release

all:    mkdir release

mkdir: 
    mkdir -p $(OBJ_DIR_PATHS)

release:    $(TARGET)

$(TARGET):   $(RELOBJFILES_cpp) $(RELOBJFILES_c)
    $(LINK.cpp) -o $@ $^ -lrt  $(LD_FLAGS)
    @echo === make ok, output: $(TARGET) ===

$(RELOBJFILES_cpp): $(OBJ_DIR_THIS)/%.o: %.cpp $(HEADERS)
    $(COMPILE.cpp) $(RELCFLAGS) -o $@ $<

$(RELOBJFILES_c): $(OBJ_DIR_THIS)/%.o: %.c $(HEADERS)
    $(COMPILE.c) $(RELCFLAGS) -o $@ $<

clean:
    -$(RM) -rf $(TARGET) $(OBJ_DIR_THIS) *.d

你可能感兴趣的:(linux)