[英]Make and Re-Compiling Headers with Sub-Directories
There are a few different questions already related to what I'm trying to do, such as this , this , and this . 与我要执行的操作已经存在一些不同的问题,例如this , this和this 。 However, I've looked at what is there and what they link to and I'm still not able to get it to work.
但是,我查看了其中的内容以及它们链接到的内容,但仍然无法使它正常工作。
I have the following make file (I had some help with this here ). 我有以下make文件(我在这里有了一些帮助)。 It builds .os in a build directory from .cpps in a source directory.
它从源目录中的.cpps在生成目录中生成.os。 I want to adjust this so that if the headers are updated, then it properly re-compiles those so I don't have to
make clean
the whole thing (most particularly the ones from the INC_DIR1
folder). 我想对此进行调整,以便如果更新标头,则可以正确地重新编译标头,因此我不必
make clean
整件事(尤其是INC_DIR1
文件夹中的标头)。
CC = g++
CFLAGS = -g -Wall
INC_DIR1 = include
INC_DIR2 = C:/CPPFiles/CPP_Extra_Libraries/armadillo-4.200.0/include
INC_DIR = $(INC_DIR1) $(INC_DIR2)
INCLUDES = $(foreach d, $(INC_DIR), -I$d)
BUILD_DIR = build
SRC_DIR = test
SRC = $(wildcard */*.cpp)
VPATH = $(SRC_DIR)
OBJS = $(addprefix $(BUILD_DIR)/, $(notdir $(SRC:.cpp=.o)))
MAIN = armadillo_extra_functions_test
.PHONY: depend clean
all: $(BUILD_DIR) $(MAIN)
@echo compilation complete
$(BUILD_DIR):
mkdir -p $@
$(BUILD_DIR)/%.o: %.cpp
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
$(MAIN): $(OBJS)
$(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS)
clean:
$(RM) *.o *~ $(MAIN) $(BUILD_DIR)/*.o
depend: $(SRC)
makedepend $(INCLUDES) $^
The latest thing I tried was to remove the depend
and makedepend
statements and then replace the $(BUILD_DIR/%.o: %.cpp
statement with 我尝试的最新操作是删除
depend
和makedepend
语句,然后将$(BUILD_DIR/%.o: %.cpp
语句替换为
DEPS = $(patsubst %.o, %.d, $(OBJS))
-include $(DEPS)
$(BUILD_DIR)/%.o: %.cpp
$(CC) -c $(CFLAGS) $(INCLUDES) $< -o $@
$(CC) -c $(CFLAGS) -MMD $< > $(DEPS)
Without the last line of these adjustments, the make file runs, though it will not update headers. 没有这些调整的最后一行,make文件将运行,尽管它不会更新标题。 However, when I add in the last line I get an error about the
$(BUILD_DIR)/%.d
files not being there. 但是,当我在最后一行添加内容时,出现有关
$(BUILD_DIR)/%.d
文件不存在的错误。
Basically you need to construct the makefile so that the .cpp files depend on the headers. 基本上,您需要构造makefile,以便.cpp文件取决于标题。 In this way, changes to the headers will trigger recompilation of the .cpp files.
这样,对标题的更改将触发.cpp文件的重新编译。 To make this easier, ideally the header dependencies of each .cpp file would be automatically determined.
为了使此操作更容易,理想情况下,将自动确定每个.cpp文件的标头依赖关系。
I recommend using either the GNU autotools approach (great for software you will publish in source form), jam (very simple configuration), or (my current favorite) CMake . 我建议使用GNU自动工具方法(非常适合您将以源代码形式发布的软件), 果酱 (非常简单的配置)或(我目前最喜欢的) CMake 。 All of these essentially automatically generate the makefile from another file (or set of files).
所有这些基本上都是从另一个文件(或文件集)自动生成makefile。 They automatically handle the dependency detection and management, too.
他们也自动处理依赖项检测和管理。 I would only hand-roll a makefile for self-education or for something with only a handful of source files, or perhaps something weird and complex.
我只会手动滚动一个makefile文件以进行自我教育,或者只包含少量源文件的东西,或者可能是奇怪而又复杂的东西。
You were almost right with the -MMD
flag, but there is no need to complicate things: 使用
-MMD
标志几乎是正确的,但无需使事情复杂化:
INC_DIR1 := include
INC_DIR2 := C:/CPPFiles/CPP_Extra_Libraries/armadillo-4.200.0/include
INC_DIRS := $(INC_DIR1) $(INC_DIR2)
BUILD_DIR := build
SRC_DIR := test
SRCS := $(wildcard $(SRC_DIR)/*.cpp)
OBJS := $(SRCS:$(SRC_DIR)/%.cpp=$(BUILD_DIR)/%.o)
DEPS := $(OBJS:.o=.d)
LDLIBS := # -l flags
LDFLAGS := # -L flags
CPPFLAGS := -MMD -MP $(foreach DIR, $(INC_DIRS), -I $(DIR))
CXXFLAGS := -W -Wall
MAIN := armadillo_extra_functions_test
.PHONY: all clean
all: $(MAIN)
@echo "compilation complete"
$(MAIN): $(OBJS)
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@
clean:
$(RM) -r $(MAIN) $(BUILD_DIR)
-include $(DEPS)
$(BUILD_DIR):
mkdir $@
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp | $(BUILD_DIR)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ -c $<
Some quick notes : 一些快速注意事项:
You're using C++. 您正在使用C ++。 Drop the
$(CC)
and $(CFLAGS)
C variables for their C++ versions: $(CXX)
and $(CXXFLAGS)
variables. 删除其C ++版本的
$(CC)
和$(CFLAGS)
C变量: $(CXX)
和$(CXXFLAGS)
变量。
$(CPPFLAGS)
is meant for preprocessor flags, such as -MMD
, -MP
or -I
. $(CPPFLAGS)
用于预处理器标志,例如-MMD
, -MP
或-I
。
You need to include the rules GCC created in the dependency files. 您需要包括在依赖文件中创建的GCC规则。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.