一、描述
(1) 多个同事间合作开发一个软件项目,每个人负责一部分模块开发,商定好彼此的对外接口,编程实现后,最后需要将所有人的开发代码编译链接到一起,生成一个最终目标文件;
(2)由于多人开发,若全部采用源文件编译,编译时文件数量众多,编写Makefile复杂难维护;
(3)由于多人开发,存在同名文件冲突的问题;
二、解决方案
(1)为了解决编译时,源文件众多,Makefile文件复杂的问题,每个开发人员负责自己源代码编译,独立生成一个静态库文件(libxxx.a);
(2)为了解决多人开发的源文件同名问题,每个开发人员负责自己的源文件编译,独立生成一个静态库文件(libxxx.a);
(3)一个软件项目产品交付是共享库(libxxx.so)时,将各个开发人员编译生成的静态库链接生成动态库即可;
三、注意事项
(1)由于交付的共享库,则各个开发人员编译静态库时,添加位置无关的编译选项(-fPIC);
# compile options
CFLAGS = -std=c++11
CFLAGS += -fPIC
(2)将静态库文件当作普通的编译生成目标文件(xxx.o),直接链接静态库文件生成动态库文集,由于链接器识别问题,静态库中有部分文件有可能不会编译到动态库中,造成编译生成的动态库不可用;
(3)为了解决链接器识别问题,链接静态库文件时,添加查找静态库文件包含目标文件选项(–whole-archive),链接找到静态库包含的所有目标文件,将其链接生成动态库文件;通过这种方式生成的动态库文件完整得到保证;
# 选项说明
`--whole-archive`
For each archive mentioned on the command line after the `--whole-archive` option, include every object file in the archive in the link, rather than searching the archive for the required object files. This is normally used to turn an archive file into a shared library, forcing every object to be included in the resulting shared library. This option may be used more than once.
`--no-whole-archive`
Turn off the effect of the `--whole-archive` option for subsequent archive files.
# 参考资料
[Using LD, the GNU linker](https://ftp.gnu.org/old-gnu/Manuals/ld-2.9.1/html_mono/ld.html)
# 示例
$(GXX) $(CFLAGS) $(PFLAGS) -Wl,--whole-archive $(STATIC_LIBS) -Wl,--no-whole-archive -o $(SHARE_LIBRARY)
(4)链接生成动态库,需要添加共享选项(-shared);
四、示例
(1) 开发一个小型工具库,其中包含日志库、进程管理库、程序运行时间统计库;
# 目录结构
# src目录下存放源代码,test目录下存放测试代码
.
├── bin
├── build.sh
├── libs
├── src
│ ├── log
│ │ ├── liblog.a
│ │ ├── Makefile
│ │ ├── messages.cpp
│ │ ├── messages.h
│ │ └── messages.o
│ ├── Makefile
│ ├── process
│ │ ├── libprocess.a
│ │ ├── Makefile
│ │ ├── process_manage.cpp
│ │ ├── process_manage.h
│ │ └── process_manage.o
│ └── run_time
│ ├── librun_time.a
│ ├── Makefile
│ ├── run_time.cpp
│ ├── run_time.h
│ └── run_time.o
└── test
├── environment.sh
├── main.cpp
├── main.o
├── Makefile
└── test_exec
(2) log库Makefile文件(src/log/Makefile)
# header file directroy
INC_DIR = .
INCS = $(patsubst %, -I%, $(INC_DIR))
# source file directory
SRC_DIR = .
SRCS = $(foreach dir, $(SRC_DIR), $(wildcard $(dir)/*.cpp))
# object files
OBJS = $(patsubst %.cpp, %.o, $(SRCS))
# compile tool
GXX = g++
# compile options
CFLAGS = -std=c++11
CFLAGS += -fPIC
WFLAGS = -Wall -Wshadow -Wunused-value -Wextra -Waddress
WFLAGS += -Wno-implicit-fallthrough -Werror=return-type
# compile target
TARGET = liblog.a
# --------------------------------------------------------
# compile rules
# default compile target
default: all
$(OBJS) : %.o : %.cpp
$(GXX) $(CFLAGS) $(WFLAGS) $(INCS) -c $< -o $@
all: $(TARGET)
$(TARGET) : $(OBJS)
ar rcs $(TARGET) $(OBJS)
ranlib $(TARGET)
mkdir -p ../../bin/self_utility/include/log
cp *.h ../../bin/self_utility/include/log
# delete compile objects
.PHONY: clean
clean:
rm -rf $(TARGET) $(OBJS)
rm -rf ../../bin/self_utility/include/log
(3) 进程管理库Makefile文件(src/process/Makefile)
# header file directroy
INC_DIR = . ../log
INCS = $(patsubst %, -I%, $(INC_DIR))
# source file directory
SRC_DIR = .
SRCS = $(foreach dir, $(SRC_DIR), $(wildcard $(dir)/*.cpp))
# object files
OBJS = $(patsubst %.cpp, %.o, $(SRCS))
# compile tool
GXX = g++
# compile options
CFLAGS = -std=c++11
CFLAGS += -fPIC
WFLAGS = -Wall -Wshadow -Wunused-value -Wextra -Waddress
WFLAGS += -Wno-implicit-fallthrough -Werror=return-type
# compile target
TARGET = libprocess.a
# --------------------------------------------------------
# compile rules
# default compile target
default: all
$(OBJS) : %.o : %.cpp
$(GXX) $(CFLAGS) $(WFLAGS) $(INCS) -c $< -o $@
all: $(TARGET)
$(TARGET) : $(OBJS)
ar rcs $(TARGET) $(OBJS)
ranlib $(TARGET)
mkdir -p ../../bin/self_utility/include/process
cp *.h ../../bin/self_utility/include/process
# delete compile objects
.PHONY: clean
clean:
rm -rf $(TARGET) $(OBJS)
rm -rf ../../bin/self_utility/include/process
(3) 程序运行时间统计库Makefile(src/run_time/Makefile)
# header file directroy
INC_DIR = .
INCS = $(patsubst %, -I%, $(INC_DIR))
# source file directory
SRC_DIR = .
SRCS = $(foreach dir, $(SRC_DIR), $(wildcard $(dir)/*.cpp))
# object files
OBJS = $(patsubst %.cpp, %.o, $(SRCS))
# compile tool
GXX = g++
# compile options
CFLAGS = -std=c++11
CFLAGS += -fPIC
WFLAGS = -Wall -Wshadow -Wunused-value -Wextra -Waddress
WFLAGS += -Wno-implicit-fallthrough -Werror=return-type
# compile target
TARGET = librun_time.a
# --------------------------------------------------------
# compile rules
# default compile target
default: all
$(OBJS) : %.o : %.cpp
$(GXX) $(CFLAGS) $(WFLAGS) $(INCS) -c $< -o $@
all: $(TARGET)
$(TARGET) : $(OBJS)
ar rcs $(TARGET) $(OBJS)
ranlib $(TARGET)
mkdir -p ../../bin/self_utility/include/run_time
cp *.h ../../bin/self_utility/include/run_time
# delete compile objects
.PHONY: clean
clean:
rm -rf $(TARGET) $(OBJS)
rm -rf ../../bin/self_utility/include/run_time
(4) 链接静态库生成动态库Makefile(src/Makefile)
# compile file directory
SUB_DIR = log process run_time
# object files
OBJS = $(foreach dir, $(SUB_DIR), $(wildcard $(dir)/*.a))
# compile tool
GXX = g++
# compile options
CFLAGS = -std=c++11
CFLAGS += -fPIC -shared
WFLAGS = -Wall -Wshadow -Wunused-value -Wextra -Waddress
WFLAGS += -Wno-implicit-fallthrough -Werror=return-type
# compile target
TARGET = ../bin/self_utility/libself_utility.so
# --------------------------------------------------------
# compile rules
# default compile target
default: all
lib:
@for dir in $(SUB_DIR); do \
$(MAKE) -C $$dir -j32; \
done
all: $(TARGET)
$(TARGET) : lib
mkdir -p ../bin/self_utility
$(GXX) $(CFLAGS) $(WFLAGS) -Wl,--whole-archive \
$(OBJS) -Wl,--no-whole-archive -o $(TARGET)
# delete compile objects
.PHONY: clean
clean:
rm -rf ../bin/self_utility
@for dir in $(SUB_DIR); do \
$(MAKE) -C $$dir clean; \
done