Get the App
SLTechnology News&Howtos  ›  Servers  › 

Makefile writing rules

Shulou Source: shulou.com Published: 2022-06-03 08:46:42 10月03日 Update

1 basic rules of makefile

TARGET... : DEPENDENCIES...

   COMMAND

  ...

Files produced by TARGET programs, such as executable files and object files; targets can also be actions to be performed, such as "clean".

DEPENDENCIES is the input file used to generate a target, and a target usually depends on multiple files.

A command (COMMAND) is an action performed by make, and one can have multiple commands, each on a line. Note: the starting character of each command line must be a TAB character!

If there are one or more file updates in DEPENDENCIES, COMMAND will execute, which is the core content of Makefile.

1.1 makefile uses pseudo-target .PHONY

1.2 makefile uses variables

1.2.1 makefile automation variables

2 make automatic derivation

GNU's make is very powerful, it can automatically derive the commands behind files and file dependencies, so there is no need to write similar commands after every [.o] file, because our make will automatically recognize and derive the commands.

As long as make sees a [.o] file, it automatically adds the [.c] file to the dependency. If make finds a whatever.o, then whatever.c will be a dependent file for whatever.o. And gcc-c whatever.c will also be deduced.

Common functions in 3 makefile Universal Edition 3.1 makefile

-- wildcard function

Files that match patterns in    's current directory for example: src=$ (wildcard .c)

-- notdir function

   removal path for example: $(notdir $src)

-- patsubst function

   pattern matching replacement for example: $(patsubst%.c,%.o,$src)

   is equivalent to $(src:.c=.o)

-- shell function

   executes the shell command for example: $(shell ls-d /)

3.2 Multi-level directory makefile

   there is one thing to note here, if you compile C++ files with Gmail +, the libraries used must be compiled and generated by Gmail +, otherwise you will not be able to link.

With regard to    + and gcc, although Gmail + is used to compile .cpp and gcc is used to compile .c, it can't be said that there is no overlap between the two.

   in fact, both can compile C and C++ code, but:,

   1) gcc treats .c files as C programs and Gmail + as C++ programs; for .cpp files, both think of C++ programs

   2) so, during the compilation phase, gcc is called by Gmail +, which is equivalent to C++ code, but because the gcc command does not automatically link with the libraries compiled by Gmail +, it is usually done with Gmail +.

  therefore, for convenience's sake, compile our library files with glossy +.

At the end of   , there is an article http://m.blog.csdn.net/zhujianwei31415/article/details/50725569 that I saw.

The compatibility between gcc and Gmail + compiling link library files is easy to understand.

Tags: Files compilations commands objects functions caching programs links multiple code actions variables characters library files patterns directories equivalence rules powerful necessary Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MySQL Linux OPPO Reno Shulou Tech Info