自动化我的Makefile

时间:2014-02-16 23:15:29

标签: c++ c makefile g++

我正在学习创建一个makefile。我的文件夹结构如下所示..

  

项目

    bin        //this is where the executable "e" is kept
          e
    obj        //this is where the obj files generated are kept
          Node.o
          Node1.o
    src        //this is where .cpp files resides
          Folder
                   Node.cpp
          Folder2
                   Node1.cpp
    include    //this is where .hhp files resides
          Folder
                   Node.hpp
          Folder2
                   Node1.hpp
    makefile

我正在使用Ubuntu 12.04并尝试使用g ++编译它。 makefile与文件夹一起驻留在项目文件夹中:src,include,obj和bin

我的makefile看起来像这样

CC = g++
DEBUG = -g
CFLAGS = -Wall -c $(DEBUG)
LFLAGS = -Wall $(DEBUG)
TARGET = bin/
OBJ = obj/
INCLUDE = include/
SRC = src/

SOURCES=$(wildcard $(SRC)**/*.cpp $(SRC)*.cpp)
HEADERS=$(wildcard include/**/*.hpp include/*.hpp)
OBJECTS=$(patsubst %.cpp,$(OBJ)%.o,$(notdir $(SOURCES)))

All : $(TARGET)e

$(TARGET)e : make_dir $(OBJECTS)
    $(CC) $(LFLAGS) $(OBJECTS) -o $@

$(OBJ)Node.o : $(SRC)Folder/Node.cpp $(HEADERS)      
    $(CC) $(CFLAGS) -I$(INCLUDE) $< -o $@
$(OBJ)Node1.o : $(SRC)Folder2/Node1.cpp $(HEADERS)
    $(CC) $(CFLAGS) -I$(INCLUDE) $< -o $@

make_dir:
    mkdir -p obj bin
clean :
    -rm -rf bin
    -rm -rf obj 
    -rm -f e *.o *~

这段代码运行得很好,但我想做的是替换下面的代码片段

$(OBJ)Node.o : $(SRC)Folder/Node.cpp $(HEADERS)      
    $(CC) $(CFLAGS) -I$(INCLUDE) $< -o $@
$(OBJ)Node1.o : $(SRC)Folder2/Node1.cpp $(HEADERS)
    $(CC) $(CFLAGS) -I$(INCLUDE) $< -o $@

有点像

%.o : %.cpp $(HEADERS)      
    $(CC) $(CFLAGS) -I$(INCLUDE) $< -o $@ 

推广整个喂食和绘图的东西。换句话说,在层次结构受到干扰之前,我不应该担心或编辑自动化makefile。

1 个答案:

答案 0 :(得分:1)

$(OBJ)/%.o : $(SRC)Folder/%.cpp $(HEADERS)      
    $(CC) $(CFLAGS) -I$(INCLUDE) $< -o $@