【问题标题】:Modify the makefile to include subdirectories修改 makefile 以包含子目录
【发布时间】:2016-01-22 17:04:59
【问题描述】:

我正在使用 c++ 中的一个小程序来学习 makefile。

程序有2个源文件(main.cpp和classf.cpp)和1个头文件(classf.h)。所有文件都包含在名为“testmake”的项目目录中。这是windlows上eclipse生成的makefile:

CXXFLAGS =  -O2 -g -Wall -fmessage-length=0

OBJS =  main.o classf.o
LIBS =
TARGET =    createPddl.exe

$(TARGET):  $(OBJS)
    $(CXX) -o $(TARGET) $(OBJS) $(LIBS)

all:    $(TARGET)

clean:
    rm -f $(OBJS) $(TARGET)

我想修改 makefile 以接受新的子目录,例如,当我添加一个名为“testmake/src”的文件夹并将文件 main.cpp 移动到其中时,名为“testmake/csource”的文件夹并移动classf.cpp 里面,并创建一个名为“testmake/cheader”的文件夹,并将classf.h 移动到里面。

【问题讨论】:

  • 要编译的文件我猜这个问题有你的答案:stackoverflow.com/questions/3774568/…,但我建议不要将所有具有 *.h 文件的路径一概添加到包含文件夹列表中
  • 您想要目标文件(main.oclassf.o)吗?
  • 到名为 build 的文件夹,可执行文件到名为 bin 的文件夹,我需要考虑名为 include 的文件夹中的头文件

标签: c++ eclipse windows makefile subdirectory


【解决方案1】:

这个makefile是eclipse自动生成的,不接受任何修改。我在那里手动创建了一个 make 文件,该文件适用于任何具有树结构的 c++ 项目。

其实我一般都用这个 Makefile

CC := g++ # This is the main compiler
SRCDIR := src
BUILDDIR := build
TARGETDIR :=bin/
TARGET := pddlcrate
DATADIR := data 
SRCEXT := cpp
SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
CFLAGS := -g # -Wall
#LIB := -pthread -lmongoclient -L lib -lboost_thread-mt -lboost_filesystem-      
mt -lboost_system-mt
INC := -I include

$(TARGET): $(OBJECTS)
    @echo " Linking..."
    @echo " $(CC) $^ -o $(TARGETDIR)$(TARGET) $(LIB)"; $(CC) $^ -o     
$(TARGETDIR)$(TARGET) $(LIB)

$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
    @mkdir -p $(BUILDDIR)
    @echo " $(CC) $(CFLAGS) $(INC) -c -o $@ $<"; $(CC) $(CFLAGS) $(INC) -c -  o $@ $<

clean:
    @echo " Cleaning..."; 
    @echo " $(RM) -r $(BUILDDIR) $(TARGET)"; $(RM) -r $(BUILDDIR) $(TARGET)

对于任何具有这种树结构的 c++ 项目

$ tree .
├── Makefile
├── bin
    >exefile
├── include
   > *.h   files
├── obj
   > *.o files
├── src
    >*.cpp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 2021-12-17
    • 2011-12-23
    相关资源
    最近更新 更多