今天对之前发布的关于#%#$#%@%@%$#%$#%#%#$%@_e206a54e97690c++e50cc872dd70ee896 c/c++项目中生成elf、动态/静态库文件的通用makefile的文章进行了优化,增强了在当前目录下生成单个可执行文件的功能。
Linux C/C++工程中可生成 ELF、动/静态库文件的通用 Makefile(二)1 功能说明之前的功能不再重复说明,此次主要增强了在包含多个cpp文件的项目中生成一个可执行文件的能力(之前只能从单个cpp或c文件生成对应的可执行文件)。
如上图所示,在我的myRedisSentinel目录中包含了多个cpp文件,只需在SINGLE_BIN中配置想要输出的可执行文件名称,然后执行make -j4命令即可生成。
Linux C/C++工程中可生成 ELF、动/静态库文件的通用 Makefile(二)2 附录:Makefile 文件源码已将下面的 Makefile 源文件托管到 GitHub 仓库中:
1、GitHub: https://www.php.cn/link/6d4a2c2688cbf0b68a09db10bf21c4f0 ;
立即学习“C++免费学习笔记(深入)”;
代码语言:javascript代码运行次数:0运行复制```javascript #################################################################### FILENAME : Makefile# DESCRIPT : A general makefile to generate an ELF or a# dynamic or a static library for C/C++ project.# AUTHOR : vfhky 2015.08.07# URI : https://www.php.cn/link/2b243b78ebe97b9313128868aff49aeb: all clean helpall: # Some important on-off settings. You can not be too careful about them.DEBUG := y# Flag of generate a dynamic lib or a static lib: y means yes. If the target is a excutable file, it should be blank!GEN_LIBS := y# Flag of generate a dynamic lib: y means yes. It should be blank unless you want to generate a dynamic lib!GEN_DYN_LIB := y# generate a single target bin file.Please let it be blank unless the target is a excutable file.SINGLE_BIN :=# generate mutil target bin files.Please let it be blank unless the target is a excutable file.MUTIL_BIN :=# Name of the static lib. It should be blank unless the target is a static lib, then the GEN_LIBS is y and GEN_DYN_LIB is blank.# STATIC_LIBS := libsrcpbl.a# Name of the dynamic lib. It should be blank unless the target is a dynamic lib, then the GEN_LIBS is y and GEN_DYN_LIB is y.DYNAMIC_LIBS := libsrcpbl.so# Environment settings. The value of PROJECT_DIR shoule be set in the nix system as the the absolute dir path of your project.# PROJECT_DIR := /home/test_usr#CURDIR := $(PROJECT_DIR)/src/pblCURDIR := $(shell pwd)PRG_BIN_DIR := $(PROJECT_DIR)/binPRG_LIB_DIR := $(PROJECT_DIR)/libPRG_INC_DIR := $(PROJECT_DIR)/include# Cross compile tools defined. You needn't modify these vars below generally.AS := asLD := ldCC := gccCXX := g++CPP := $(CC) -EAR := ar rcsNM := nmSTRIP := stripRANLIB := ranlibSTD_OPT := -D_GNU_SOURCECC += -std=c99 $(STD_OPT)CXX += $(STD_OPT)# nix system tools defined. You needn't modify these vars below generally.BLACK = "\e[33;0m"RED = "\e[31;1m"GREEN = "\e[32;1m"YELLOW = "\e[33;3m"BLUE = "\e[34;1m"PURPLE = "\e[35;1m"CYAN = "\e[36;1m"WHITE = "\e[37;1m"CP := cpSED := sedFIND := findMKDIR := mkdir -pXARGS := xargsMV := mvRM := rm -rf# Get .c, .cpp source files by searching from current directory.CUR_SRC_DIR = $(shell ls -AxR $(CURDIR)|grep ":"|tr -d ':')CUR_SRC := $(foreach subdir,$(CUR_SRC_DIR),$(wildcard $(subdir)/.c $(subdir)/.cpp))#CUR_SRC := $(shell find . -name ".c" -o -name ".cpp"|sed -e 's,./,,')CUR_C := $(filter %.c, $(CUR_SRC))CUR_CPP := $(filter %.cpp, $(CUR_SRC))# Get the include files, object files, dependent files by searching from PRG_INC_DIR.CUR_INC_DIR = $(shell ls -AxR $(PRG_INC_DIR)|grep ":"|tr -d ':')CUR_INC := $(foreach subdir,$(CUR_INC_DIR),$(subdir)/.h)SRC_H := $(filter %.h, $(CUR_INC))#CUR_OBJ := $(addprefix $(PRG_BIN_DIR)/,$(strip $(CUR_CPP:.cpp=.o) $(CUR_C:.c=.o)))#CUR_OBJ := $(addprefix $(PRG_BIN_DIR)/,$(notdir $(CUR_CPP:.cpp=.o) $(CUR_C:.c=.o)))CUR_OBJ := $(strip $(CUR_CPP:.cpp=.o) $(CUR_C:.c=.o))#CUR_DEP := $(addprefix $(PRG_BIN_DIR)/,$(notdir $(CUR_CPP:.cpp=.d) $(CUR_C:.c=.d)))CUR_DEP := $(strip $(CUR_CPP:.cpp=.d) $(CUR_C:.c=.d))# Create directory in the header files, bin and library directory.$(foreach dirname,$(sort $(PRG_INC_DIR) $(PRG_BIN_DIR) $(PRG_LIB_DIR)),\ $(shell $(MKDIR) $(dirname)))# Complie and link variables. LD_LIBS means the dynamic or static library needed for the object file.CFLAGS := $(if $(DEBUG),-g -Wall, -O2 -Wall)CFLAGS += $(if $(GEN_DYN_LIB), $(addprefix -fPIC -I ,$(sort $(dir $(SRC_H)))), $(addprefix -I ,$(sort $(dir $(SRC_H)))))CXXFLAGS = $(CFLAGS)LDFLAGS := LD_LIB_DIR := #-L $(PRG_LIB_DIR)LD_LIBS := #-lsrcpbl -lmysqlclientXLD_FLG := -Xlinker "-(" $(LDFLAGS) -Xlinker "-)"# Add vpath.vpath %.h $(sort $(dir $(SRC_H)))vpath %.c $(sort $(dir $(CUR_C)))vpath %.cpp $(sort $(dir $(CUR_CPP)))# Generate depend files.ifneq "$(MAKECMDGOALS)" "clean"sinclude $(CUR_DEP)endif# Gen_depend(depend-file,source-file,object-file,cc). This command-package is used to generate a depend file with a postfix of .d.define gen_depend @set -e; \ $(RM) $1; \ $4 $(CFLAGS) -MM $2 | \ $(SED) 's,($(notdir $3)): ,$3: ,' > $1.tmp; \ $(SED) -e 's/#.//' \ -e 's/^[^:]: //' \ -e 's/ *\$$//' \ -e '/^$$/ d' \ -e 's/$$/ :/' > $1.tmp; \ $(MV) $1.tmp $1;endef# Rules to generate objects file(.o) from .c or .cpp files.$(CURDIR)/%.o: $(CURDIR)/%.c @$(call gen_depend,$(patsubst %.o,%.d,$@),$
<code></code>
以上就是Linux C/C++工程中可生成 ELF、动/静态库文件的通用 Makefile(二)的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号