Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Makefile(04)_ function

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

9. Function definition and call

The concept of functions is supported in Makefile, and the make parser provides a series of functions for Makefile to use. At the same time, you can customize functions.

9.1. Custom function

Support the implementation of custom functions in Makefile, and call execution, through the define keyword to achieve custom functions.

Syntax rules for function definition:

The nature of custom functions:

1. A custom function is actually a multi-line variable and cannot be called directly; it is used through the call keyword (the function of call is to replace the argument to the corresponding position of the function body)

two。 A custom function is a procedure call with no return value

3. Used to define a collection of commands and apply them to rules.

Example:

.PHONY: testdefine func1 @ echo "My name is $(0)" endefdefine func2 @ echo "My name is $(0)" @ echo "Param 1 = > $(1)" @ echo "Param 2 = > $(2)" endefvar: = $(call func1) new: = $(func1) test: @ echo "new = > $(new)" @ echo "var = > $(var)" $(call func1) # @ echo My name is func1 $(call func2, D.T.Software, delphi_tang)

Output result:

9.2. Predefined function

Make functions provide functions that handle filenames, variables, and commands. Functions can be called where needed to handle specified parameters, and the result is replaced where the function is called.

Calls to predefined functions:

Why is the call form of custom function and predefined function different?

In essence, Makefile does not support the real sense of custom functions, custom functions are essentially multi-line variables, predefined call functions pass parameters to multi-line variables when calling, custom functions call function parameters, and are executed in call.

Example:

PHONY: testdefine func1 @ echo "My name is $(0)" endefdefine func2 @ echo "My name is $(0)" endefvar1: = $(call func1) var2: = $(call func2) var3: = $(abspath. /) var4: = $(abspath test.cpp) test: @ echo "var1 = > $(var1)" @ echo "var2 = > $(var2)" @ echo "var3 = > $(var3)" @ echo "var4 = > $(var4)"

Output result:

10. The comprehensive application of variables and functions is 10.1. Actual combat requirements:

Automatically generate target folders for executable programs, generate objs folders for compiled object files (* .o)

Support the compilation selection of the debug version (through precompiled macros), considering the extensibility of the code (custom variables)

10.2. Raw materials for tools:

$(wildcard _ pattern) to get the files or directories in the current working directory that satisfy _ pattern

$(addprefix _ prefix _ name), adding the prefix _ prefix to each name in the name list _ name

10.3. Key skills:

1. Automatically get the list of source files in the current directory (function calls), SRCS: = $(wildcard * .c)

two。 Generate a target file list based on the file list (variable assignment replacement) OBJS: = $(SRCS:.c=.o)

3. Prefix the path to each list of target files (function calls) OBJS: = $(addprefix path/, $(OBJS))

Pattern substitution in rules:

The difference between these two mode replacements is that the latter's schema replacement target comes from a variable var, and the former's target comes from a specified folder.

Dependency of compilation rules:

Final procedure:

CC: = gccMKDIR: = mkdirRM: = rm-frDIR_OBJS: = objsDIR_TARGET: = targetDIRS: = $(DIR_OBJS) $(DIR_TARGET) TARGET: = $(DIR_TARGET) / hello-makefile.out# main.c const.c func.cSRCS: = $(wildcard * .c) # main.o const.o func.oOBJS: = $(SRCS:.c=.o) # objs/main.o objs/const.o objs/func.oOBJS: = $(addprefix $(DIR_OBJS) / $(OBJS)) .PHONY: rebuild clean all$ (TARGET): $(DIRS) $(OBJS) $(CC)-o $@ $(OBJS) @ echo "Target File = > $@" $(DIRS): $(MKDIR) $@ # perform mode replacement for the working directory under the current file $(DIR_OBJS) /% .o:% .c ifeq ($(DEBUG)) True) $(CC)-o $@-g-c $^ else $(CC)-o $@-c $^ endifrebuild: clean allall: $(TARGET) clean: $(RM) $(DIRS)

Source file main.c

Extern void foo (); int main () {foo (); return 0;}

Source file const.c

Const char* g_hello = "hello makefile"

Source file func.c

# include "stdio.h" extern char* foo () {printf ("void foo ():% s\ n", g_hello);}

Output result

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report