简体   繁体   English

具有源子目录的单个顶级makefile

[英]Single top-level makefile with source subdirectories

Firstly, apologies for my ignorance. 首先,对我的无知表示歉意。 I'm sure the answer to my question exists in one of the many existing makefile threads here. 我确定问题的答案存在于此处许多现有的makefile线程之一中。 However, I have been unable to find one that concisely and clearly answers my specific question without obfuscating the answer with details that aren't relevant to my particular situation. 但是,我找不到一个简洁明了地回答我的特定问题的答案,而又没有用与我的特定情况无关的细节来使答案模糊不清。

My code directory has a single top-level source file containing main. 我的代码目录只有一个包含main的顶级源文件。 The rest of the source files are organised in subdirectories according to logical divisions in the system. 其余的源文件根据系统中的逻辑划分按子目录组织。 The code contains no relative paths in the includes. 该代码在include中不包含相对路径。 This means that everything works perfectly if all the code is in a single directory using the following, simple makefile: 这意味着,如果使用以下简单的makefile文件将所有代码都放在一个目录中,则一切工作都将完美进行:

CC=g++
CFLAGS=-c
LDFLAGS=
SOURCES=Main.cpp Source1.cpp Source2.cpp Source3.cpp Etc.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=executable

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS) 
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
    $(CC) $(CFLAGS) $< -o $@

Until now I have been building my code using the NetBeans IDE. 到目前为止,我一直在使用NetBeans IDE构建代码。 This has helped preserve my make ignorance by generating some vast and overly complicated makefiles on my behalf. 通过代我生成一些庞大且过于复杂的makefile,这有助于保留我的无知。 Now the time has come to distribute my code for public use so I'm looking to produce a makefile will enable me to distribute the code with the directory structure I have. 现在是时候分发我的代码供公众使用了,所以我希望生成一个makefile,使我可以使用已有的目录结构来分发代码。

Can you help? 你能帮我吗?

Regards, Enthusastic Amateur. 热情的业余爱好者。

Take a look at this: 看看这个:

# Source directories separated by space
# Example ./ src1/ src2/
SRCDIR   = ./ src/
# Directory where object files will be placed
OBJDIR   = obj/
# Include directories separated by space
# Example: include1/ include2/
INCDIR   = include/
# Directory where binary file will be placed
BINDIR   = bin/
# Name of the result file
TARGET   = app
# Compiler
CXX      = g++

# Retrive list of the source files
SRC      = $(wildcard $(addsuffix *.cpp,$(SRCDIR)))
# Generate list of the object files
OBJ      = $(addprefix $(OBJDIR), $(patsubst %.cpp, %.o, $(notdir $(SRC))))

VPATH    = $(SRCDIR)

# Compilation flags
CXXFLAGS = -std=c++11 -pthread

$(TARGET) : $(OBJ)
    @echo Linking...
    @mkdir -p $(BINDIR)
    @$(CXX) $(CXXFLAGS) -o $(BINDIR)$@ $(OBJ)

$(OBJDIR)%.o : %.cpp
    @echo Compiling $< in $@...
    @mkdir -p $(OBJDIR)
    @$(CXX) $(CXXFLAGS) $(addprefix -I,$(INCDIR)) -c -o $@ $<

clean :
    @$(RM) -r $(OBJDIR)
    @$(RM) -r $(BINDIR)

Here you can provide multiple source directories. 在这里您可以提供多个源目录。

And "everything works perfectly" as well if the code is in multiple directories, using the same makefile you already have. 如果代码位于多个目录中,并且使用您已经拥有的同一个makefile,那么“一切都可以正常工作”。 No changes needed. 无需更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM