In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
When we use makefile, we will inevitably avoid using third-party library files. So how do you modify makefile in the compilation environment when you need to use third-party library files? In some of the usual experience, third-party libraries provide functions in the library by way of function calls; library files are released with header files that declare library function prototypes; header files are used in the compilation phase and library files are used in the link phase. Let's take a look at the location of the third-party library in the project, as follows
Let's first take a look at the compilation phase support of third-party libraries: 1. Define the variable DIR_LIBS_INC to indicate the location of the header file, DIR_LIBS_INC: = $(DIR_PROJECT) / libs/inc;2, use DIR_LIBS_INC to prompt the location of the make header file, vpath% $(TYPE_INC) $(DIR_LIBS_INC) 3. Use DIR_LIBS_INC to prompt the location of the compiler header file, CFLAGS + =-I $(DIR_LIBS_INC).
Let's take a look at what happens if we use third-party library files directly. We add the contents of the file using the third-party library to the main.c under the main folder, as follows
# include # include "define.h" # include "slib.h" # include "dlib.h" int main () {printf ("Version:% s\ n", VERSION); printf ("main ():: start main...\ n"); common (); module_main (); printf ("Dynamic Lib:% s\ n", dlib_name ()); printf ("2 + 3 =% d\ n", add (2,3)) Printf ("Static Lib:% s\ n", slib_name ()); printf ("4 * 5 =% d\ n", multi (4,5));}
Let's take a look at the compilation results.
Let's first take a look at how the specific makefile is written, based on the makefile of the previous blog.
Pro-rule.mk source code
.PHONY: all compile link clean rebuild $(MODULES) DIR_PROJECT: = $(realpath.) DIR_BUILD_SUB: = $(addprefix $(DIR_BUILD) /, $(MODULES)) MODULE_LIB: = $(addsuffix .a, $(MODULES)) MODULE_LIB: = $(addprefix $(DIR_BUILD) /, $(MODULE_LIB)) APP: = $(addprefix $(DIR_BUILD) / (APP)) define makemodule cd ${1} & &\ $(MAKE) all\ DEBUG:=$ (DEBUG)\ DIR_BUILD:=$ (addprefix $(DIR_PROJECT) /, $(DIR_BUILD))\ DIR_COMMON_INC:=$ (addprefix $(DIR_PROJECT) /, $(DIR_COMMON_INC))\ DIR_LIBS_INC:=$ (addprefix $(DIR_PROJECT) / (DIR_LIBS_INC))\ CMD_CFG:=$ (addprefix $(DIR_PROJECT) /, $(CMD_CFG))\ MOD_CFG:=$ (addprefix $(DIR_PROJECT) /, $(MOD_CFG))\ MOD_RULE:=$ (addprefix $(DIR_PROJECT) /, $(MOD_RULE)) & &\ cd. ; endefall: compile $(APP) @ echo "Success! Target = > $(APP)" compile: $(DIR_BUILD) $(DIR_BUILD_SUB) @ echo "Begin to compile." @ set-e;\ for dir in $(MODULES);\ do\ $(call makemodule, $dir)\ done @ echo "Compile Success!" Link $(APP): $(MODULE_LIB) @ echo "Begin to link..." $(CC)-o $(APP)-Xlinker "- (" $^-Xlinker "-)" $(LFLAGS) @ echo "Link Success!" $(DIR_BUILD) $(DIR_BUILD_SUB): $(MKDIR) $@ clean: @ echo "Begin to clean." $(RM) $(DIR_BUILD) @ echo "Clean Success!" Rebuild: clean all$ (MODULES): $(DIR_BUILD) $(DIR_BUILD) / $(MAKECMDGOALS) @ echo "Begin to compile $@" @ set-e;\ $(call makemodule, $@)
Pro-cfg.mk source code
MODULES: = common\ module\ main MOD_CFG: = mod-cfg.mkMOD_RULE: = mod-rule.mkCMD_CFG: = cmd-cfg.mkDIR_BUILD: = buildDIR_COMMON_INC: = common/incDIR_LIBS_INC: = libs/incAPP: = app.out
Cmd-cfg.mk source code
AR: = arARFLAGS: = crsCC: = gccLFLAGS: = CFLAGS: =-I $(DIR_INC)-I $(DIR_COMMON_INC)-I $(DIR_LIBS_INC) ifeq ($(DEBUG), true) CFLAGS + =-gendifMKDIR: = mkdirRM: = rm-fr
Mod-rule.mk source code
.PHONY: allMODULE: = $(realpath.) MODULE: = $(notdir $(MODULE)) DIR_OUTPUT: = $(addprefix $(DIR_BUILD) /, $(MODULE)) OUTPUT: = $(MODULE). AOUTPUT: = $(addprefix $(DIR_BUILD) /, $(OUTPUT)) SRCS: = $(wildcard $(DIR_SRC) / * $(TYPE_SRC)) OBJS: = $(SRCS:$ (TYPE_SRC) = $(TYPE_OBJ) OBJS: = $(patsubst $(DIR_SRC) /% $(DIR_OUTPUT) /%, $(OBJS) DEPS: = $(SRCS:$ (TYPE_SRC) = $(TYPE_DEP)) DEPS: = $(patsubst $(DIR_SRC) /%, $(DIR_OUTPUT) /% $(DEPS)) vpath% $(TYPE_INC) $(DIR_INC) vpath% $(TYPE_INC) $(DIR_COMMON_INC) vpath% $(TYPE_INC) $(DIR_LIBS_INC) vpath% $(TYPE_SRC) $(DIR_SRC)-include $(DEPS) all: $(OUTPUT) @ echo "Success! Target = > $(OUTPUT) "$(OUTPUT): $(OBJS) $(AR) $(ARFLAGS) $@ $^ $(DIR_OUTPUT) /% $(TYPE_OBJ):% $(TYPE_SRC) $(CC) $(CFLAGS)-o $@-c $(filter% $(TYPE_SRC)) $^) $(DIR_OUTPUT) /% $(TYPE_DEP):% $(TYPE_SRC) @ echo "Creating $@..." @ set-e \ $(CC) $(CFLAGS)-MM-E $(filter% $(TYPE_SRC), $^) | sed's,\ (.*\)\ .o [:] *, $(DIR_OUTPUT) /\ 1 $(TYPE_OBJ) $@:, g'> $@
Let's see if the compilation is successful.
We see that the compilation has been successful. There are several considerations when compiling: 1, define DIR_LIBS_LIB: = libs/lib (this is the path where the third-party library is located); 2, do not directly link the library files in DIR_LIBS_LIB when linking; 3. Copy the library files to the DIR_BUILD folder first; 4. You must consider the new and old relationship between the copied library files and the original library files, as follows
So why don't we just try the link?
We saved it when we saw the direct link. Let's take a look at the link phase support of third-party libraries, defining the variable EXTERNAL_LIB to hold the list of third-party libraries, and the target link needs to rely on the list of third-party libraries. As follows
Let's see how the specific makefile is written.
Pro-rule.mk source code
.PHONY: all compile link clean rebuild $(MODULES) DIR_PROJECT: = $(realpath.) DIR_BUILD_SUB: = $(addprefix $(DIR_BUILD) /, $(MODULES)) MODULE_LIB: = $(addsuffix .a, $(MODULES)) MODULE_LIB: = $(addprefix $(DIR_BUILD) /, $(MODULE_LIB)) EXTERNAL_LIB: = $(wildcard $(DIR_BUILD) / *) EXTERNAL_LIB: = $(patsub $(DIR_LIBS_INC) /%, $(DIR_BUILD) /% (EXTERNAL_LIB)) APP: = $(addprefix $(DIR_BUILD) /, $(APP)) define makemodule cd ${1} & &\ $(MAKE) all\ DEBUG:=$ (DEBUG)\ DIR_BUILD:=$ (addprefix $(DIR_PROJECT) /, $(DIR_BUILD))\ DIR_COMMON_INC:=$ (addprefix $(DIR_PROJECT) / (DIR_COMMON_INC))\ DIR_LIBS_INC:=$ (addprefix $(DIR_PROJECT) /, $(DIR_LIBS_INC))\ CMD_CFG:=$ (addprefix $(DIR_PROJECT) /, $(CMD_CFG))\ MOD_CFG:=$ (addprefix $(DIR_PROJECT) /, $(MOD_CFG))\ MOD_RULE:=$ (addprefix $(DIR_PROJECT) / (MOD_RULE) & &\ cd. ; endefall: compile $(APP) @ echo "Success! Target = > $(APP)" compile: $(DIR_BUILD) $(DIR_BUILD_SUB) @ echo "Begin to compile." @ set-e;\ for dir in $(MODULES);\ do\ $(call makemodule, $dir)\ done @ echo "Compile Success!" Link $(APP): $(MODULE_LIB) $(EXTERNAL_LIB) @ echo "Begin to link..." $(CC)-o $(APP)-Xlinker "- (" $^-Xlinker "-)" $(LFLAGS) @ echo "Link Success!" $(DIR_BUILD) /%: $(DIR_LIBS_LIB) /% $(CP) $^ $@ $(DIR_BUILD_SUB): $(MKDIR) $@ clean: @ echo "Begin to clean..." $(RM) $(DIR_BUILD) @ echo "Clean Success!" Rebuild: clean all$ (MODULES): $(DIR_BUILD) $(DIR_BUILD) / $(MAKECMDGOALS) @ echo "Begin to compile $@" @ set-e;\ $(call makemodule, $@)
Cmd-cfg.mk source code
AR: = arARFLAGS: = crsCC: = gccLFLAGS: = CFLAGS: =-I $(DIR_INC)-I $(DIR_COMMON_INC)-I $(DIR_LIBS_INC) ifeq ($(DEBUG), true) CFLAGS + =-gendifMKDIR: = mkdirRM: = rm-frCP: = cp
Pro-cfg.mk source code
MODULES: = common\ module\ main MOD_CFG: = mod-cfg.mkMOD_RULE: = mod-rule.mkCMD_CFG: = cmd-cfg.mkDIR_BUILD: = buildDIR_COMMON_INC: = common/incDIR_LIBS_INC: = libs/incDIR_LIBS_LIB: = libs/libAPP: = app.out
Let's take a look at the compilation results.
We see that the link has been successful and the executable program has been run correctly. So in the actual work, the third-party library files may be updated at any time, we re-establish dlib.so and slib.a under the libs/lib folder to see if they will be re-linked?
We see that it is re-copied on the first link and not copied on the second link. Through the study of the use of third-party libraries, the summary is as follows: 1, the compilation environment must support the use of third-party libraries (static or dynamic libraries); 2, project development will generally use a special folder to store third-party libraries; 3, the header files attached to third-party libraries are used to declare functions (needed during the compilation phase); 4, in the link phase, copy the library files to the build folder and then link.
Welcome to learn makefile language, you can add me QQ:243343083.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.