改进makefile以查看子目录

时间:2013-04-20 00:05:01

标签: c++ makefile

前段时间我需要创建一个makefile来处理我正在制作的许多小测试项目。整个范围是简单地编译其目录中的所有.cpp文件,并整理目标文件和依赖项。但是,其中一个项目最终变得足够长,我需要创建自定义子文件夹以保持所有代码的有序性。

问题是我对makefile没有那么多了解,我制作的那个是由我在互联网上发现的碎片和碎片组成的。我想知道如何修改此代码以便能够包含来自不同文件夹的文件。我现在拥有的是:

SHELL=/bin/bash
CC=g++
EXECUTABLE=ruletheskies

CFLAGS=-std=c++0x -Wall -pedantic #-I/usr/include/boost_1_51_0/
LDFLAGS=-L./
LIBRARIES=-ljson_linux-gcc-4.6_libmt -lbprinter
WINLIBRARIES=-ljson_mingw_libmt -lbprinter_mingw

WINDEFINES=-DWINVER=0x0400 -D__WIN95__ -D__GNUWIN32__ -DSTRICT -DHAVE_W32API_H -D__WXMSW__ -D__WINDOWS__
WINLDFLAGS=-I./ # $(shell find /usr/include/ -maxdepth 1 -type d | sed 's/^/-I/g') #-L/usr/lib/ -I/usr/include/
WINBASELIBRARIES=-lodbc32 -lwsock32 -lwinspool -lwinmm -lshell32 -lcomctl32 -lctl3d32 -lodbc32 -ladvapi32 -lodbc32 -lwsock32 -lopengl32 -lglu32 -lole32 -loleaut32 -luuid
WINSTATIC=-static-libstdc++ -static-libgcc
WINOPTIONS=--verbose

SOURCES := $(wildcard *.cpp)
HEADERS := $(wildcard *.h)

OBJECTSFOLDER=objects
OBJECTS=$(addprefix $(OBJECTSFOLDER)/, $(SOURCES:.cpp=.o))

DEPFOLDER=dependencies
df=$(DEPFOLDER)/$(*F)
odf=$(OBJECTSFOLDER)/$(*F)

EXEFOLDER=bin

.PHONY : all
# Brute force compiling statement for simple compilation and no .o residual files
all: $(EXEFOLDER)
    $(CC) $(CFLAGS) $(LDFLAGS) *.cpp $(LIBRARIES) -o $(EXEFOLDER)/$(EXECUTABLE)

.PHONY: debug
# Compiles using object files in OBJECTSFOLDER, only modified stuff
debug: folders $(SOURCES) $(EXEFOLDER)/$(EXECUTABLE)

.PHONY: windows
# cross-compilation: mingw has to be selected, adds libraries for windows, brute force
windows: $(EXEFOLDER)
    $(CC) $(CFLAGS) $(WINDEFINES) $(WINSTATIC) $(LDFLAGS) $(WINLDFLAGS) *.cpp $(LIBRARIES) $(WINLIBRARIES) $(WINBASELIBRARIES) -o $(EXEFOLDER)/$(EXECUTABLE).exe $(WINOPTIONS)

# Final linking of the executable
$(EXEFOLDER)/$(EXECUTABLE): $(OBJECTS) 
    $(CC) $(LDFLAGS) $(OBJECTS) $(LIBRARIES) -o $@

# Creation of object files
# ( Actual compilation is only last instruction, the rest is creating dependency header rules )
$(OBJECTSFOLDER)/%.o: %.cpp
    @$(CC) -MMD -c $(CFLAGS) -o $@ $<
    @cp $(odf).d $(df).P; \
        sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
            -e '/^$$/ d' -e 's/$$/ :/' < $(odf).d >> $(df).P; \
    rm -f $(odf).d
    $(CC) -c $(CFLAGS) $< -o $@

-include $(DEPFOLDER)/*.P

# Creation of folders
.PHONY : folders
folders: $(OBJECTSFOLDER) $(DEPFOLDER) $(EXEFOLDER)
$(OBJECTSFOLDER):
    mkdir $@
$(DEPFOLDER):
    mkdir $@
$(EXEFOLDER):
    mkdir $@

# Cleanup of object files
# ( .PHONY is to avoid calling the rule in case a file named clean exists )
.PHONY : clean
clean:
    rm -rf $(OBJECTSFOLDER)
    rm -rf $(DEPFOLDER)

我想我必须修改SOURCES := $(wildcard *.cpp)行,但是如何包含我必须查看的子目录,无论它们是什么?我打算将相应的标题和源文件放在一起,如果这简化了事情。

0 个答案:

没有答案