我正在尝试使用 Makefile 构建库的调试和发布版本,并将这些库复制到相关的构建目录,例如
.PHONY: all clean distclean
all: $(program_NAME_DEBUG)
$(CP) $(program_NAME_DEBUG) $(BUILD_DIR)/debug/$(program_NAME_DEBUG)
$(RM) $(program_NAME_DEBUG)
$(RM) $(program_OBJS)
$(program_NAME_RELEASE)
$(CP) $(program_NAME_RELEASE) $(BUILD_DIR)/release/$(program_NAME_RELEASE)
$(RM) $(program_NAME_RELEASE)
$(RM) $(program_OBJS)
$(program_NAME_DEBUG): $(program_OBJS)
$(LINK_DEBUG.c) -shared -Wl,-soname,$(program_NAME_DEBUG) $(program_OBJS) -o $(program_NAME_DEBUG)
$(program_NAME_RELEASE): $(program_OBJS)
$(LINK_RELEASE.c) -shared -Wl,-soname,$(program_NAME_RELEASE) $(program_OBJS) -o $(program_NAME_RELEASE)
第一个目标 (program_NAME_DEBUG) 编译正常,但第二个目标 (program_NAME_RELEASE) 产生以下错误:
libGlam_rel.so
make: libGlam_rel.so: Command not found
make: *** [all] Error 127
libGlam_rel.so
是的价值program_NAME_RELEASE
它似乎不像第一个目标那样识别第二个目标?
编辑
终于让这个工作了。
一个问题是在多个目录中有 src 文件,使用 VPATH 对此进行排序,例如
# specify dirs other then current dir to search for src files
VPATH = ../../pulse_IO/src ../../../g2/src
在平台条件的东西中添加了额外的库目标,例如
# Platform specific conditional compilation
UNAME := $(shell uname)
TARGET := Glam
ifeq ($(UNAME), Linux)
# LINUX version
program_NAME := lib$(TARGET).so
program_DEBUG_NAME := lib$(TARGET)_dbg.so
program_RELEASE_NAME := lib$(TARGET)_rel.so
BUILD_DIR = ../build/linux
endif
ifeq ($(UNAME), MINGW32_NT-6.1)
# WINDOWS version
program_NAME := lib$(TARGET).dll
program_DEBUG_NAME := lib$(TARGET)_dbg.dll
program_RELEASE_NAME := lib$(TARGET)_rel.dll
BUILD_DIR = ../build/windows
endif
添加了新的调试和发布目标文件:
DEBUG_OBJS := $(addprefix $(BUILD_DIR)/debug/,${program_OBJS})
RELEASE_OBJS := $(addprefix $(BUILD_DIR)/release/,${program_OBJS})
设置我的调试并发布 CFLAGS:
DEBUG_CFLAGS := -fPIC -g -Wall -DDEBUG=1
RELEASE_CFLAGS := -fPIC -O2 -Wall -DDEBUG=0
整理了所有调试和发布编译器选项:
DEBUG_LINK.c := $(CC) $(DEBUG_CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
RELEASE_LINK.c := $(CC) $(RELEASE_CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
将我的新规则添加到“全部”目标中:
.PHONY: all clean
all: $(program_DEBUG_NAME) $(program_RELEASE_NAME)
规则集如下所示(包括替代隐式目标文件生成):
$(program_DEBUG_NAME): $(DEBUG_OBJS)
$(DEBUG_LINK.c) -shared -Wl,-soname,$@ $^ -o $(BUILD_DIR)/debug/$@
$(program_RELEASE_NAME): $(RELEASE_OBJS)
$(RELEASE_LINK.c) -shared -Wl,-soname,$@ $^ -o $(BUILD_DIR)/release/$@
# rule to build object files (replaces implicit rule)
$(BUILD_DIR)/debug/%.o: %.c
$(DEBUG_LINK.c) $< -c -o $@
$(BUILD_DIR)/release/%.o: %.c
$(RELEASE_LINK.c) $< -c -o $@
最后,我修改了 clean 以处理所有新文件:
clean:
@- $(RM) $(BUILD_DIR)/debug/$(program_DEBUG_NAME)
@- $(RM) $(DEBUG_OBJS)
@- $(RM) $(BUILD_DIR)/release/$(program_RELEASE_NAME)
@- $(RM) $(RELEASE_OBJS)
这允许我使用单个 Makefile 在 linux 和 windows 平台上生成我的库的调试和发布版本,例如$ make -k