在单独目录树中带有对象的Makefile

时间:2019-02-20 23:56:52

标签: makefile gnu-make

我一直在尝试设计一个makefile示例,该示例可以将目标文件构建为与关联源不同的目录结构。包含源和目标的目录树如下:

/repo-root
|-- /build
    |-- /example_lib
        |-- makefile
        |-- /Release*
            |-- libexample_lib.a*
            |-- /obj*
                |-- example_lib.o*
                |-- example_lib.d*
|-- /source
    |-- /example_lib
        |-- /inc
            |-- example_lib.h
        |-- /src
            |-- example_lib.cpp

星号文件夹/文件是makefile应该生成的文件夹。

我还看到了其他问题和答案(Makefile : Build in a separate directory tree,(Makefile with directory for object files)等,但是如果我正确理解了它们的源到对象规则,它们似乎会在输出目录与源目录匹配。

我正在使用的当前makefile如下,使用GNU Make 3.82运行:

SHELL = /bin/sh

.SUFFIXES:
.SUFFIXES: .cpp .o

# @todo Variables passed to make.
BUILD_CONFIG=Release

# Makefile specific variables
REPO_ROOT=../..
LIBRARY_NAME=example_lib

#-------------------------------------------------------------------
# Derived variables
#-------------------------------------------------------------------

LIBRARY_FILENAME=lib$(LIBRARY_NAME).a
LIBRARY_FILEPATH=$(BUILD_CONFIG)/$(LIBRARY_FILENAME)

# Source directories
CPP_SRC_DIRS=$(REPO_ROOT)/source/example_lib/src

# Source files
vpath %.cpp $(CPP_SRC_DIRS)
CPP_SRCS=$(foreach cpp_src_dir, $(CPP_SRC_DIRS), $(wildcard $(cpp_src_dir)/*.cpp))

# Object/dependencies directory
OBJ_DEPS_DIR=./$(BUILD_CONFIG)/obj

# Object files
OBJS=$(CPP_SRCS:%.cpp=$(OBJ_DEPS_DIR)/%.o)

# Dependency files (built with objects)
DEPS=$(CPP_SRCS:%.cpp=$(OBJ_DEPS_DIR)/%.d)

#-------------------------------------------------------------------
# C++ compiler settings
#-------------------------------------------------------------------

CPP_COMMAND=g++
CPP_OPTIONS_INC_PATHS=-I"$(REPO_ROOT)/source/example_lib/inc"
CPP_OPTIONS_OPTIM=-O3
CPP_OPTIONS_WARN=-Wall
CPP_OPTIONS_MISC=-c -fmessage-length=0
CPP_OPTIONS=$(CPP_OPTIONS_INC_PATHS) $(CPP_OPTIONS_OPTIM) $(CPP_OPTIONS_WARN) $(CPP_OPTIONS_MISC)

#-------------------------------------------------------------------
# Archiver settings
#-------------------------------------------------------------------

AR_COMMAND=ar
AR_OPTIONS=-r

#-------------------------------------------------------------------
# Targets
#-------------------------------------------------------------------

# Object/dependency directory target
$(OBJS): | $(OBJ_DEPS_DIR)
$(OBJ_DEPS_DIR):
    mkdir -p $(OBJ_DEPS_DIR)

# Object targets
$(OBJ_DEPS_DIR)/%.o: %.cpp
    @echo 'Building file: $<'
    @echo 'Invoking: GCC C++ Compiler'
    $(CPP_COMMAND) $(CPP_OPTIONS) -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -o "$@" "$<"
    @echo 'Finished building: $<'
    @echo ' '

# 'all' target
all: $(LIBRARY_FILEPATH)

# Output library target
$(LIBRARY_FILEPATH): $(OBJS)
    @echo 'Building target: $@'
    @echo 'Invoking: GCC Archiver'
    $(AR_COMMAND) $(AR_OPTIONS)  "$@" $(OBJS)
    @echo 'Finished building target: $@'
    @echo ' '

# 'clean' target
clean:
    @echo 'Cleaning targets'
    rm -rf $(OBJS) $(DEPS) $(LIBRARY_FILENAME)
    @echo ' '

# 'PHONY' target
.PHONY: all clean

有意义的“ make”输出是:

make all 
Building file: ../../source/example_lib/src/example_lib.cpp
Invoking: GCC C++ Compiler
g++ -I"../../source/example_lib/inc" -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"Release/obj/../../source/example_lib/src/example_lib.d" -MT"Release/obj/../../source/example_lib/src/example_lib.o" -o "Release/obj/../../source/example_lib/src/example_lib.o" "../../source/example_lib/src/example_lib.cpp"
../../source/example_lib/src/example_lib.cpp:6:1: fatal error: opening dependency file Release/obj/../../source/example_lib/src/example_lib.d: No such file or directory
 }
 ^
compilation terminated.
make: *** [Release/obj/../../source/example_lib/src/example_lib.o] Error 1

源到对象规则是将%.cpp文件的完整路径替换为%.o目标,这将导致对象路径为:

Release/obj/../../source/example_lib/src/example_lib.o

我不了解的是如何获取源文件和目标文件的隐式规则以使其在不同树中时匹配。我使用vpath来帮助进行源解析,但是对象(目标)部分不匹配。我还尝试将源到对象规则更改为:

$(OBJ_DEPS_DIR)/$(notdir %.o): %.cpp

但是这导致了相同的路径(通配符匹配不支持$(notdir ...)之类的命令?)。我还意识到,如果两个源目录恰好有一个具有相同名称的文件,那么从潜在的不同源目录中删除所有目标文件可能会导致名称冲突-这对于我正在使用的代码来说不是问题。

感谢您的帮助,在此先感谢您!

1 个答案:

答案 0 :(得分:0)

技巧是在构造目标文件的名称之前,删除到源文件的不需要的路径:

CPP_SRCS=$(foreach cpp_src_dir, $(CPP_SRC_DIRS), $(wildcard $(cpp_src_dir)/*.cpp))
# CPP_SRCS now contains "../../source/example_lib/src/example_lib.cpp"
SRCS := $(notdir $(CPP_SRCS))
# SRCS now contains "example_lib.cpp"
BAD_OBJS := $(CPP_SRCS:%.cpp=$(OBJ_DEPS_DIR)/%.o)
# BAD_OBJS now contains "./Release/obj/../../source/example_lib/src/example_lib.o"
OBJS := $(patsubst %.cpp,$(OBJ_DEPS_DIR)/%.o,$(SRCS))
# OBJS now contains "./Release/obj/example_lib.o"

一旦您做了很多工作,就可以对规则进行一些小的调整。