SOURCE
:
=
$(
wildcard *.c)
$(
wildcard *.cpp)
OBJS
:
=
$(
patsubst %.c,%.o,
$(
patsubst %.cpp,%.o,
$(
SOURCE
)))
TARGET
:
= host_send
CC
:
= gcc
CXX
:
= g++
LIBS
:
= -lusb-
1.0
-luvc -lopencv_highgui -lreadline -lopencv_imgcodecs -lopencv_imgproc -lc -lopencv_calib3d -lopencv_core
LDFLAGS
:
= -
L
/usr/libx86_64-linux-gnu/ -
L
/usr/lib/x86_64-linux-gnu -
L
/usr/local/lib -
L
.
DEFINES
:
= -fpic -
Wl
,-rpath=.
INCLUDE
:
= -
I
. -
I
./
include
-
I
/usr/
include
/libusb-
1.0
-
I
/usr/local/
include
CFLAGS
:
= -g -
Wall
-
O3
$(
DEFINES
)
$(
INCLUDE
)
CXXFLAGS
:
=
$(
CFLAGS
)
.
PHONY
:
everything objs clean veryclean rebuild
everything
:
$(
TARGET
)
all
:
$(
TARGET
)
objs
:
$(
OBJS
)
rebuild:
veryclean everything
clean
:
rm -rf *.o
rm
$(
TARGET
)
veryclean
:
clean
rm -rf
$(
TARGET
)
$(
TARGET
)
:
$(
OBJS
)
$(
CXX
)
$(
CXXFLAGS
) -o
$@
$(
OBJS
)
$(
LDFLAGS
)
$(
LIBS
)
第二个模版:
.c和
.cpp文件在src目录,文件结构更清晰。
BINFILE = test
ifdef VERBOSE
E = @true
Q = @
E = @echo
endif
CFILES := $(shell find src -mindepth 1 -maxdepth 4 -name "*.c")
CXXFILES := $(shell find src -mindepth 1 -maxdepth 4 -name "*.cpp")
INFILES := $(CFILES) $(CXXFILES)
OBJFILES := $(CXXFILES:src/%.cpp=%) $(CFILES:src/%.c=%)
DEPFILES := $(CXXFILES:src/%.cpp=%) $(CFILES:src/%.c=%)
OFILES := $(OBJFILES:%=obj/%.o)
COMMONFLAGS := -Wall -Wextra -pedantic -O3
DEFINES := -fpic -Wl,-rpath=.
INCLUDE := -I. -I./include -I/usr/include/libusb-1.0 -I/usr/local/include
LDFLAGS := -L/usr/libx86_64-linux-gnu/ -L/usr/lib/x86_64-linux-gnu -L/usr/local/lib -L.
LIBS := -lusb-1.0 -lopencv_core -lopencv_highgui -lreadline -lopencv_calib3d -lopencv_imgcodecs -lc -lopencv_imgproc
ifdef DEBUG
COMMONFLAGS := $(COMMONFLAGS) -g
endif
CFLAGS := $(COMMONFLAGS) --std=c99 $(DEFINES) $(INCLUDE)
CXXFLAGS := $(COMMONFLAGS) --std=c++0x $(DEFINES) $(INCLUDE)
DEPDIR := deps
all: $(BINFILE)
ifeq ($(MAKECMDGOALS),)
-include Makefile.dep
endif
ifneq ($(filter-out clean, $(MAKECMDGOALS)),)
-include Makefile.dep
endif
CC = gcc
CXX = g++
-include Makefile.local
.PHONY: clean all depend
.SUFFIXES:
obj/%.o: src/%.c
$(E)C-compiling $<
$(Q)if [ ! -d `dirname $@` ]; then mkdir -p `dirname $@`; fi
$(Q)$(CC) -o $@ -c $< $(CFLAGS)
obj/%.o: src/%.cpp
$(E)C++-compiling $<
$(Q)if [ ! -d `dirname $@` ]; then mkdir -p `dirname $@`; fi
$(Q)$(CXX) -o $@ -c $< $(CXXFLAGS)
Makefile.dep: $(CFILES) $(CXXFILES)
$(E)Depend
$(Q)for i in $(^); do $(CXX) $(CXXFLAGS) -MM "$${i}" -MT obj/`basename $${i%.*}`.o; done > $@
$(BINFILE): $(OFILES)
$(E)Linking $@
$(Q)$(CXX) -o $@ $(OFILES) $(LDFLAGS) $(LIBS)
clean:
$(E)Removing files
$(Q)rm -f $(BINFILE) obj/* Makefile.dep
用了很久的Linux,感觉还是对Makefile不够熟悉,经常为一个复杂一点的项目的Makefile折腾半天。现在对Makefile的基本写法做一下总结,方便以后查阅。
这里用我近期写的一个Makefile做讲解,是一个C与C++混编的项目。并附上完整的Makefile内容。
目录结构是:
+--include/
+--include/librtmp
+--liblog/
+--libr...
目前的业务需求是:
c写的几个文件,和一个C++文件要整合为一个动态库,被C++调用。而这个动态库的生成过程中,会链接几个基础的开发库,比如libz,libpcre,libm等。
一、C和C++混合编译动态库的Makefile
编写这样的Makefile来生成一个正确的so库很重要,下面是一个正确的Makefile的写法:
CC = g...
1.自动遍历源文件
自动遍历源文件文件部分参考 https://blog.csdn.net/yuliying/article/details/49635485,
AllDIRS := $(shell ls -R | grep '^\./.*:$$' | awk '{gsub(":","");print}') .
CSRCS = $(foreach n,$(All
makefile 中c/c++混编编译遇到的问题
前提:三个文件server.c client.c wrapper.c,c风格编写的,server.c包含了c++11的内容。mac os下面编译。
其中server.c包含了c++11的内容,其他部分使用c风格的方式写的,为了以后扩展写了makefile编译,其中遇到了很多问题,花了很长时间来解决,记录如下:
编译命令是gcc/g++,有clang的warning报错
clang: warning: treating ‘c’ input as ‘c++’
# ----------------------------------------------------------------------------
# Makefile for building tapp
SOURCES:=$(wildcard *.c)
CPPSOURCE:=$(wildcard *.cpp)
OBJECT:=$(SOURCES