简体   繁体   English

[MAKEFILE]:如何将 cpp 源文件从不同的源文件夹复制到一个目标文件夹并使用这些 cpp 文件使用 MAKE 构建

[英][MAKEFILE]: How to copy cpp source files from different source folders into one destination folder and use those cpp files to build using MAKE

My Current makefile looks like the following, and it build the targets correctly.我当前的 makefile 如下所示,它正确构建了目标。 But it fails to build the targets in one instance, that is when the length of the path to source files becomes too long.但它无法在一个实例中构建目标,即源文件的路径长度变得太长。 Therefore I want to collect all my source files in different folders into one single source directory and then do the make.因此,我想将不同文件夹中的所有源文件收集到一个源目录中,然后进行制作。

Any leads on how I can modify the following makefile to create a flat source directory and use the files in that directory to build using make关于如何修改以下 makefile 以创建平面源目录并使用该目录中的文件使用 make 构建的任何线索

PROJ_DIR := ../
SRC_DIR += $(PROJ_DIR)project
SRC_FILES += $(wildcard $(SRC_DIR)/*/source/*.cpp)
OBJ_DIR := $(PROJ_DIR)TASK
OBJ_FILES += $(patsubst $(SRC_DIR)/%.cpp,$(OBJ_DIR)/%.o,$(SRC_FILES))

CXXFLAGS := --c++14

CC_INCLUDE_PATH += -I$(PROJ_DIR)project/include

.PHONY: all clean dirs

all: $(OBJ_DIR) $(OBJ_FILES) 
    "$(COMPILERBIN)"/cc.exe -rvn crackLib.a $(OBJ_FILES)

clean:
    @rm -rf crackLib.a $(OBJ_DIR) $(FLAT_INC_DIR)  

# Build target for directory creation. Intermediate build files will be placed here.
$(OBJ_DIR):
    mkdir -p $@

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp 
    "$(COMPILERBIN)"/cc.exe $(CXXFLAGS) -c -o $@ $< $(CC_INCLUDE_PATH) 

$(OBJ_DIR)/%.o: $(TSM_SRC_DIR)/%.cpp
    "$(COMPILERBIN)"/cc.exe $(CXXFLAGS) -c -o $@ $< $(CC_INCLUDE_PATH) 

$(OBJ_DIR)/%.o: $(SL_SRC_DIR)/%.cpp
    "$(COMPILERBIN)"/cc.exe $(CXXFLAGS) -c -o $@ $< $(CC_INCLUDE_PATH) 

-include $(OBJ_FILES:.o=.d)

Suppose we have the source file $(SRC_DIR)/longpath/source/foo.cpp , and we want to build the object file $(OBJ_DIR)/foo.o .假设我们有源文件$(SRC_DIR)/longpath/source/foo.cpp ,并且我们想要构建 object 文件$(OBJ_DIR)/foo.o We might wish for this rule:我们可能希望这条规则:

$(OBJ_DIR)/foo.o: $(SRC_DIR)/longpath/source/foo.cpp
    ... $< -o $@

But we don't know the path when we write the makefile, so we must ask Make to figure it out at build time.但是我们在写makefile的时候不知道路径,所以在构建的时候一定要让Make弄清楚。

There is more than one way to do this.有不止一种方法可以做到这一点。 The simplest is to use the vpath directive .最简单的是使用vpath指令

If we ken the path ahead of time, we could write this:如果我们提前 ken 路径,我们可以这样写:

vpath %.cpp $(SRC_DIR)/longpath/source

$(OBJ_DIR)/foo.o: foo.cpp
    ... $< -o $@

Make would use vpath to search for the source file, and use its full path as the prerequisite (and therefore $< ). Make 将使用vpath搜索源文件,并使用其完整路径作为先决条件(因此$< )。

The vpath directive can take multiple directories, and although we don't know the path beforehand, we know how to compute them: vpath指令可以采用多个目录,虽然我们事先不知道路径,但我们知道如何计算它们:

vpath %.cpp $(dir $(SRC_FILES))

$(OBJ_DIR)/foo.o: foo.cpp
    ... $< -o $@

And once we confirm that this works, we can switch to a pattern rule:一旦我们确认这可行,我们就可以切换到模式规则:

$(OBJ_DIR)/%.o: %.cpp
    ... $< -o $@

and change OBJ_FILES accordingly:并相应地更改OBJ_FILES

OBJ_FILES += $(patsubst %.cpp,$(OBJ_DIR)/%.o,$(notdir $(SRC_FILES)))

all: $(OBJ_FILES)
    ... $^

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

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