Some usage of linux makefile

Makefile ar usage


  Create a static library. a file. It is often used when developing programs in C/C++, but I rarely use the ar command in the command line alone. It is usually written in makefiles and sometimes used in shell scripts. For related knowledge of library files, static libraries, dynamic libraries and how to create and use under Linux, please refer to the related materials [3] "Analysis of Linux Static and Dynamic Libraries" later in this article.
  Common parameters
  format: ar rcs libxxx.a xx1.o xx2.o
  parameter r: insert the module in the library (replacement). When the inserted module name already exists in the library, replace the module with the same name. If one of several modules does not exist in the library, ar displays an error message and does not replace other modules with the same name. By default, new members are added at the end of the library, and other options can be used to change the position of the addition. [1]
  Parameter c: create a library. Regardless of whether the library exists, it will be created.
  Parameter s: Create the target file index, which can speed up the time when creating a larger library. (Supplement: If you don't need to create an index, you can change it to uppercase S parameters; if the .a file lacks an index, you can use the ranlib command to add it)
  Format: ar t libxxx.a
  displays which target files in the library file, only the name.
  Format: ar tv libxxx.a
  displays which object files are in the library file, and displays detailed information such as file name, time, and size.
  Format: nm -s libxxx.a
  displays the index table in the library file.
  Format: ranlib libxxx.a
  creates an index table for library files.
  Example of use    Example
  1: Using
  Bash code in a shell script
  OS=`uname
  -r` ar rcs libhycu.a.$OS *.o
  Example 2: Using
  Makefile code in makefile
  $(BIN1): $(BIN1_OBJS)
  ar rcs $@ $^
  Example 3: Creating and using static library
  first Step: Edit the source file, test.h test.c main.c. The main.c file contains the main function as the program entry; test.c contains the functions that need to be used in the main function.
  vi test.h test.c main.c
  Step 2: Compile test.c into an object file.
  gcc -c test.c
  If test.c is correct, you will get the target file test.o.
  The third step: by. o file to create a static library.
  ar rcs libtest.a test.o
  Step 4: Use static libraries in the program.
  gcc -o main main.c -L. -ltest
  Because it is statically compiled, the generated executable file can be independent of it. a file runs.
  Step 5: Implementation.
  ./main
  Example 4: Create and use a dynamic library    The
  first step: edit the source file, test.h test.c main.c. The main.c file contains the main function as the program entry; test.c contains the functions that need to be used in the main function.
  vi test.h test.c main.c
  Step 2: Compile test.c into an object file.
  The
  first two steps of gcc -c test.c are consistent with creating a static library.
  The third step: by. o file to create a dynamic library file.
  gcc -shared -fPIC -o libtest.so test.o
  Step 4: Use dynamic libraries in the program.
  gcc -o main main.c -L. -ltest
  When the static library and the dynamic library have the same name, the gcc command will give priority to the dynamic library.
  Step 5: Implementation.
  LD_LIBRARY_PATH=. ./main
  Example 5 View the files in the static library
  [root@node56 lib]# ar -t libhycu.a
  base64.co
  binbuf.co
  cache.co
  chunk.co
  codec_a.co
  …
  xort.co
  [root@node56 lib]#
  [root@node56 lib]# ar -tv libhycu.a
  rw-r--r-- 0/0 7220 Jul 29 19:18 2011 base64.co
  rw-r--r-- 0/0   2752 Jul 29 19:18 2011 binbuf.c.o
  rw-r--r-- 0/0  19768 Jul 29 19:18 2011 cache.c.o
  …
  rw-r--r-- 0/0   4580 Jul 29 19:18 2011 xort.c.o
  [root@node56 lib]#
  [root@node56 lib]# nm -s libhycu.a | less
  Archive index:
  Base64Enc in base64.c.o
  GetBase64Value in base64.c.o
  Base64Dec in base64.c.o
  encode64 in base64.c.o
  decode64 in base64.c.o
  check64 in base64.c.o
  test64 in base64.c.o
  …
  chunk_alloc in chunk.c.o
  [root@node56 lib]#

 

Makefile other usage

1. wildcard: expand wildcard
2. notdir: remove path

3. patsubst: Replace wildcard

4. The syntax of conditional expressions

=========================
Example:
create a test directory, create a subdirectory named sub under the test directory
$ mkdir test
$ cd test
$ mkdir sub

Under test, create ac and bc, under sub directory, create sa.c and sb.c to

create a simple Makefile
src=$(wildcard *.c ./sub/*.c)
dir=$( notdir $(src))
obj=$(patsubst %.c,%.o,$(dir))


all:
    @echo $(src)
    @echo $(dir)
    @echo $(obj)
    @echo "end"

execution Result analysis: the
first line of output:

a.c b.c ./sub/sa.c ./sub/sb.c

Wildcard expands all files with the suffix c under the specified directories ./ and ./sub/ .

The second line of output:
ac bc sa.c sb.c
notdir removes the path of the expanded file. The

third line of output:
ao bo sa.o sb.o

in $(patsubst %.c,%.o,$(dir In) ), patsubst replaces all variables in $(dir) with .c suffixes with .o,

or you can use
obj=$(dir:%.c=%.o) to
have the same effect.

========================

4. The syntax of conditional expressions

 There are four keywords for ifeq  .

The first one is "ifeq"

ifeq (<arg1>, <arg2>)

ifeq '<arg1>' '<arg2>'

ifeq "<arg1>" "<arg2>"

ifeq "<arg1>" '<arg2>'

ifeq '<arg1>' "<arg2>"

Compare whether the values ​​of the parameters "arg1" and "arg2" are the same. Of course, we can also use the make function in the parameters. Such as:

ifeq ($(strip $(foo)),)

<text-if-empty>

endif

In this example, the "strip" function is used. If the return value of this function is Empty, then <text-if-empty> will take effect.

 

The second condition keyword is "ifneq"

ifneq (<arg1>, <arg2>)

ifneq '<arg1>' '<arg2>'

ifneq "<arg1>" "<arg2>"

ifneq "<arg1>" '<arg2>'

ifneq '<arg1>' "<arg2>"

It compares whether the values ​​of the parameters "arg1" and "arg2" are the same, and if they are different, it is true. Similar to "ifeq".

 

The third condition keyword is "ifdef"

ifdef <variable-name>

If the value of the variable <variable-name> is not empty, the non-empty expression is true. Otherwise, the expression is false. Of course, <variable-name> can also be the return value of a function. Note that ifdef only tests whether a variable has a value , it does not expand the variable to the current position . Let's look at two examples:

Example 1:

bar =

foo = $(bar)

ifdef foo

frobozz = yes

else

frobozz = no

endif

 

all:
    @echo frobozz=$(frobozz)

After executing make, it will print out frobozz=yes

 

Example two:

foo =

ifdef foo

frobozz = yes

else

frobozz = no

endif

all:
    @echo frobozz=$(frobozz)

In the first example, the value of "$(frobozz)" is "yes", and the second is "no".

The fourth condition keyword is "ifndef"

ifndef <variable-name>

I won't say much about this, it means the opposite of "ifdef".

=====================================

 

Commonly used functions

Commonly used string processing functions are: subst, patsubst, strip, findstring, filter, filter-out, sort, word, wordlist, words, firstword.

File name manipulation function: The parameter string of each function will be treated as one or a series of file names. Commonly used functions are: dir, notdir, suffix, basename, addsuffix, addprefix, join.

Foreach function: This function is used for looping. The foreach function in Makefile is almost imitating the for statement in the Unix standard Shell (/bin/sh) or the foreach statement in C-Shell (/bin/csh) And built.

if function: The if function is very similar to the conditional statement ifeq supported by GNU make. The if function can contain the "else" part or not. That is, the parameters of the if function can be two or three.

call function: is the only function that can be used to create new parameterizations.

Origin function: It does not manipulate the value of the variable, it just tells you where the variable comes from.

Shell function: Its parameter should be the command of the operating system Shell, which has the same function as the backquote "`". That is to say, the shell function returns the output after executing the operating system command as a function. Therefore, you can use operating system commands and string processing commands awk, sed and other commands to generate a variable.

Functions that control make: error, warning.

Run of make: Generally speaking, the easiest way is to directly enter the make command on the command line. The make command will find the Makefile in the current directory to execute it. Everything is automatic.

The exit code of make: 0, which means successful execution; 1, if there is any error when make is running, it returns 1; 2. If you use the "-q" option of make, and make makes some targets do not need to be updated, then return 2.

Specify Makefile: The rule of GNU make to find the default Makefile is to find three files in the current directory-"GNUmakefile", "makefile" and "Makefile". It searches for these three files in order, and once it finds it, it starts to read and execute the file.

We can also assign a Makefile with a special name to the make command. To achieve this function, you must use make's "-f" or "--file" parameter ("--makefile" parameter is also OK).

Specify the target: Generally speaking, the final target of make is the first target in the makefile, and other targets are usually brought out by this target. This is the default behavior of make. To instruct make to complete the target you specified, you need to directly follow the name of the target after the make command .

Any target in the makefile can be specified as the ultimate target, but except for targets that start with "-" or include "=", targets with these characters will be parsed into command-line parameters or variables .

Implicit rule: It is a convention, make will run according to this "convention", even if there is no such rule written in our Makefile.

Variables used in implicit rules : The variables used in implicit rules can be divided into two types, one is command-related, such as "CC", and the other is parameter-related, such as "CFLAGS".

Command related variables:

(1) AR: Function library packaging program, the default command is "ar";

(2) AS: assembly language compiler, the default command is "as";

(3) CC: C language compiler, the default command is "cc";

(4) CXX: C++ language compiler, the default command is "g++";

(5) CO: extend the file program from the RCS file, the default command is "co";

(6), CPP: C program preprocessor (output is standard output device), the default command is "$(CC)-E";

(7) FC: Fortran and Ratfor compilers and preprocessors, the default command is "f77";

(8), GET: the program to expand the file from the SCCS file, the default command is "get";

(9), LEX: Lex method analyzer program (for C or Ratfor), the default command is "lex";

(10) PC: Pascal language compiler, the default command is "pc";

(11), YACC: Yacc grammar analyzer (for C programs), the default command is "yacc";

(12), YACCR: Yacc grammar analyzer (for the Ratfor program), the default command is "yacc-r";

(13) MAKEINFO: Convert Texinfo source file (.texi) to Info file program, the default command is "makeinfo";

(14) TEX: A program to create TeX DVI files from TeX source files, the default command is "tex";

(15) TEXI2DVI: A program to create TeX DVI files from Texinfo source files, the default command is "texi2dvi";

(16) WEAVE: A program to convert Web to TeX, the default command is "weave";

(17) CWEAVE: A program to convert C Web to TeX, the default command is "cweave";

(18) TANGLE: A program to convert Web to Pascal language, the default command is "tangle";

(19) CTANGLE: Convert C Web to C, the default command is "ctangle";

(20) RM: delete file command, the default command is "rm-f".

Parameter-related variables: If the default value is not specified, then the default value is empty.

(1), ARFLAGS : the parameter of the AR command of the function library packager, the default is "rv";

(2) ASFLAGS: assembly language compiler parameters (when ".s" or ".S" files are called obviously);

(3), CFLAGS: C language compiler parameters;

(4), CXXFLAGS: C++ language compiler parameters;

(5) COFLAGS: RCS command parameters;

(6) CPPFLAGS: C preprocessor parameters (also used by C and Fortran compilers);

(7) FFLAGS: Fortran language compiler parameters;

(8) GFLAGS: SCCS "get" program parameters;

(9), LDFLAGS: linker parameters, such as "ld";

(10), LFLAGS: Lex grammar analyzer parameters;

(11), PFLAGS: Pascal language compiler parameters;

(12) RFLAGS: Fortran compiler parameters of the Ratfor program;

(13), YFLAGS: Yacc grammar analyzer parameters.

Defining pattern rules: You can use pattern rules to define an implicit rule. A pattern rule is like a general rule, but in the rule, the definition of the target needs to have the "%" character . "%" means one or more arbitrary characters. You can also use "%" in the dependent target, but the value of "%" in the dependent target depends on its target. Note: The expansion of "%" occurs after the expansion of variables and functions. The expansion of variables and functions occurs when make loads the Makefile, while the "%" in the pattern rule occurs at runtime .

Automatic variable : This variable automatically takes out a series of files defined in the pattern one by one until all the files that conform to the pattern are taken out. Such automated variables should only appear in the commands of the rules .

The following is a description of the automation variables:

(1), $@: indicates the target file set in the rule. In the pattern rule, if there are multiple targets, then "$@" is the set that matches the pattern definition in the target;

(2). $%: Only when the target is a library file, it means the target member name in the rule. If the target is not a library file, then its value is empty;

(3), $<: Depends on the first target name in the target. If the dependent target is defined in a pattern (ie "%"), then "$<" will be a series of file sets that conform to the pattern. Note that they are taken out one by one;

(4), $?: The set of all dependent targets that are newer than the target, separated by spaces;

(5), $^: The collection of all dependent targets, separated by spaces. If there are multiple duplicates in the dependent targets, then this variable will go out to the duplicate dependent targets, and only one copy will be kept;

(6) $+: This variable is very similar to "$^", and it is also a collection of all dependent targets, except that it does not remove duplicate dependent targets;

(7), $*: This variable represents "%" and the part before it in the target mode. If the target is "dir/a.foo.b" and the target pattern is "a.%.b", then the value of "$*" is "dir /a.foo". If there is no pattern definition in the target, then "$*" cannot be deduced. However, if the suffix of the target file is recognized by make, then "$*" is the part except the suffix. For example, if the target is "foo.c", because ".c" is the suffix name recognized by make, the value of "$*" is "foo". This feature is GNU make, and it may not be compatible with other versions of make, so you should avoid using "$*" as much as possible.

Use make to update the function library file: the function library file is the packaging file of the Object file (the intermediate file for program compilation). Under Unix, the packaging is usually done by the command "ar".

The above content is organized from the CSDN blog:

https://blog.csdn.net/fengbingchun/article/details/45170853

http://blog.csdn.net/haoel/article/category/9198

csdn blogger fengbingchun 's GitHub : https://github.com/fengbingchun/Linux_Code_Test

Guess you like

Origin blog.csdn.net/hyl999/article/details/108642313