【发布时间】:2014-01-28 17:57:43
【问题描述】:
我正在尝试学习项目的“最佳实践”makefile。
请查看下面我的 Makefile 文件并提出改进建议。
目录布局:
root dir
--- Makefile
deps
--- deps
bin
--- binary
objs
--- all .o files
include
--- all .h files
src
--- all .c .cc files
生成文件:
#
# Generic makefile
#
all: tengine test2
#
# Include files for compiling, and libraries for linking.
#
INC=-I /usr/include -I /usr/local/include -I /usr/include/hiredis
LIB=-lhiredis
#
# Debug or not debug?
#
DEBUG=1
ifdef DEBUG
CFLAGS=-Wall -Winline -pipe -g -DDEBUG #-pedantic -pg
else
CFLAGS=-Wall -Winline -pipe -O3 -march=native -funroll-all-loops \
-finline-functions #-pedantic
endif
#CXXFLAGS=$(CFLAGS)
# Rules for creating dependency files
deps/%.d: src/%.cc
@echo Generating $@
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) $(INC) -MM -MT '$(patsubst src/%,obj/%,%(patsubst %.cc,%.o,$<))' $< > $@
deps/%.d: src/%.c
@echo Generating $@
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) $(INC) -MM -MT '$(patsubst src/%,obj/%,%(patsubst %.c,%.o,$<))' $< > $@
# Rules for compilation
#
# C source with header and no c++ code
obj/%.o: src/%.c src/%.h deps/%.d
@echo Compiling $@
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) $(INC) -o $@ -c $<
# C++ source with header.
obj/%.o: src/%.cc src/%.h deps/%.d
@echo Compiling $@
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) $(INC) -o $@ -c $<
# C source without header and no c++ code
obj/%.o: src/%.c deps/%.d
@echo Compiling $@
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) $(INC) -o $@ -c $<
# C++ source without header.
obj/%.o: src/%.cc deps/%.d
@echo Compiling $@
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) $(INC) -o $@ -c $<
# ##############################################################
#
# TARGET: tengine
#
# ##############################################################
OBJS= obj/main.o obj/tengine.o
tengine: $(OBJS)
$(CXX) -pipe $(CXXFLAGS) -o bin/tengine $(OBJS) $(LIB)
# ##############################################################
#
# TARGET: test2
#
# ##############################################################
OBJS= obj/main.o obj/test2.o
test2: $(OBJS)
$(CXX) -pipe $(CXXFLAGS) -o bin/test2 $(OBJS) $(LIB)
# ##############################################################
#
# Cleanup
#
# ##############################################################
clean:
rm -f *~ bin/* obj/* deps/* src/*~ gmon.out
help:
@echo ""
@echo "make - builds tengine"
@echo "make test2 - builds test2"
@echo "make all - builds tengine test2"
@echo "make clean - deletes prior build"
【问题讨论】:
-
你真的需要那么多章节标题吗?
-
您可能需要将
test2部分中的tengine更改为test2。我会删除 Makefile 中的很多 cmets,因为它们大多是噪音。您还应该在链接行中为g++和-pipe使用宏。如果您的 makefile 要健壮,则应在对象之后列出库。您似乎也没有设置 CXXFLAGS 但您使用它。 -
谢谢。我做了这些改变。我出于 SO 的目的添加了 cmets,通常我的 Makefile 中没有它们。 CXXFLAGS 在原帖中被注释掉了。