Linux programming preliminary study makefile learning notes

Source: Internet
Author: User
Preliminary introduction to linux programming-makefile-general Linux technology-Linux programming and kernel information. The following is a detailed description. Objects = main. o kbd. o command. o display. o \
Insert. o search. o files. o utils. o
Edit: $ (objects)
Cc-o edit $ (objects)


. PHONY: clean
Clean:
Rm edit $ (objects)


Makefile contains
1. explicit rules
2. Concealed rules
3. variable definition
4. File instructions
5. Notes #

Include
Current Directory: a. mk, B. mk, c. mk, foo. make, e. mk, f. mk
Bar = e. mk f. mk

Include foo. make *. mk $ (bar)
It is equivalent:
Include foo. make a. mk B. mk c. mk e. mk f. mk

Make will search for it in the current directory first. If it is not found in the current directory, make will search for it in the following directories:
1. If the "-I" or "-- include-dir" parameter exists during make execution, make will search for it in the directory specified by this parameter.
2. If the Directory ;/Include (usually:/usr/local/bin or/usr/include), make will also find it.

-Include

1. Read all makefiles.
2. Read Other makefiles to be included.
3. initialize the variables in the file.
4. Deduce hidden rules and analyze all rules.
5. create dependency links for all target files.
6. Determine the targets to be regenerated Based on the dependency.
7. Run the generated command.

Targets: prerequisites; command
Or
Targets: prerequisites
Command

Objects: = $ (wildcard *. o)

VPATH
My directory structure is as follows:
Ctest
|
| -- Hello. c
| -- Makefile
| -- Printf2.h
| -- Sub
| -- Printf2.c

// ***** Printf2.h ****
Void printf2 (void );

// ***** Printf2.c ***
# Include
Void printf2 (void)
{
Printf ("I am printed by printf2 \ n ");
}

// ***** Hello. c ****
# Include
# Include "printf2.h"
Int main (void)
{
Printf2 ();
Return 1;
}

// Makefile
VPATH =./sub

Hello: hello. o printf2.o
Gcc-o hello. o printf2.o

Hello. o: hello. c printf2.h

The following Makefile structure will be better:
PROG = hello
OBJS = hello. o printf2.o
RM = rm-f

VPATH =./sub
All: $ (PROG)

Hello: $ (OBJS)
$ (CC) $ (CFLAGS) $ (CPPFLAGS) $ ^ $ (LDFLAGS)-o $ @

Hello. o: hello. c printf2.h

Clean:
$ (RM) $ (PROG) $ (OBJS)

Or
VPATH =./sub
Hello: hello. o printf2.o
Gcc-o hello. o printf2.o
Printf2.o: printf2.c
Gcc-c $ <
Hello. o: hello. c printf2.h
Gcc-c hello. c


Use: To separate VPATH = src: ../headers

Vpath
1. vpath Vpath %. h ../headers
For the compliant mode . ;.

2. vpath
Clear conformity Mode .

3. vpath
Clear all configured file search directories.

Vpath %. c foo
Vpath % blish
Vpath %. c bar
It indicates the file ending with ". c". First in the "foo" directory, then "blish", and finally the "bar" directory.

Vpath %. c foo: bar
Vpath % blish
The preceding statement indicates the file ending with ". c". First in the "foo" directory, then the "bar" directory, and finally the "blish" directory.


Static Mode
: : ;
;


Objects = foo. o bar. o

All: $ (objects)

$ (Objects): %. o: %. c
$ (CC)-c $ (CFLAGS) $ <-o $ @
Equivalent:
Foo. o: foo. c
$ (CC)-c $ (CFLAGS) foo. c-o foo. o

Bar. o: bar. c
$ (CC)-c $ (CFLAGS) bar. c-o bar. o

**************************************** *****
Files = foo. elc bar. o lose. o

$ (Filter %. o, $ (files): %. o: %. c
$ (CC)-c $ (CFLAGS) $ <-o $ @

$ (Filter %. elc, $ (files): %. elc: %. el
Emacs-f batch-byte-compile $ <

Automatic dependency generation
Cc-M main. c

The output is:
Main. o: main. c defs. h

If you use the gnu c/C ++ compiler, you must use the "-MM" parameter. Otherwise, the "-M" parameter will include header files of some standard libraries.

We can write [. c] file and [. d] File dependency, and make auto update or self-contained [. d] file and include it in our main Makefile. In this way, we can automatically generate dependencies for each file.
%. D: %. c
@ Set-e; rm-f $ @;\
$ (CC)-M $ (CPPFLAGS) $ <>;@ @. $ ;\
Sed's, \ ($ * \) \. o [:] *, \ 1.o $ @:, G' <$ @. $> @;\
Rm-f $ @. $

If the make parameter "-n", "-- just-print", "-- dry-run", or "-- recon" are included during the make operation, the command is only displayed, but no command is executed.
The "-t" or "-- touch" parameter means to update the Time of the target file without changing the target file.
The make parameter "-s" or "-- slient" indicates that the command is fully forbidden.
The make parameter is "-k" or "-- keep-going", which means that if a command in a rule fails, the rule is executed, but continue to execute other rules.
"-W" or "-- print-directory" will output some information during the make process, so that you can see the current working directory.
When you use the "-C" parameter to specify the lower Makefile of make, "-w" will be automatically opened. If the parameter contains "-s" ("-- slient") or "-- no-print-directory", "-w" is always invalid.
The "-f" or "-- file" parameter specifies Makefile make? F hchen. mk if the "-f" parameter is used more than once in the make command line, all specified makefiles will be connected and passed to make for execution.
The behavior of the "-q" or "-- question" parameter is to find the target. That is to say, if the target exists, nothing will be output, and of course no compilation will be executed, if the target does not exist, an error message is printed.



Subsystem:
Cd subdir & $ (MAKE)
It is equivalent:

Subsystem:
$ (MAKE)-C subdir


If you want to pass the variable to the lower-level Makefile, you can use the following declaration:
Export ;

If you do not want some variables to be passed to lower-level Makefile, you can declare them as follows:
Unexport ;

Define command package
Define run-yacc
Yacc $ (firstword $ ^)
Mv y. tab. c $ @
Endef

Foo. c: foo. y
$ (Run-yacc)

The variable on the right of "=" is not necessarily a defined value. You can also use the value defined later.
Foo = $ (bar)
Bar = $ (ugh)
Ugh = Huh?
All:
Echo $ (foo)

: = The preceding variables cannot use the following variables. They can only use the previously defined variables. If so:

Y: = $ (x) bar

X: = foo

Then, the value of y is "bar" instead of "foo bar ".

Ifeq (0, $ {MAKELEVEL })
Cur-dir: = $ (shell pwd)
Whoami: =$ (shell whoami)
Host-type: = $ (shell arch)
MAKE: =$ {MAKE} host-type =$ {host-type} whoami =$ {whoami}
Endif

Advanced use

Foo: = a. o B. o c. o
Bar: = $ (foo:. o =. c)

The value of $ (bar) is a. c B. c. c.
//*****************
Objects = main. o foo. o bar. o utils. o
Objects + = another. o

//****************
Override if a variable is set through the make command line parameter, the value of this variable in Makefile will be ignored.
Override =

Target variable
Prog: CFLAGS =-g

Prog: prog. o foo. o bar. o

$ (CC) $ (CFLAGS) prog. o foo. o bar. o

Prog. o: prog. c

$ (CC) $ (CFLAGS) prog. c

Foo. o: foo. c

$ (CC) $ (CFLAGS) foo. c

Bar. o: bar. c

$ (CC) $ (CFLAGS) bar. c
No matter what the global $ (CFLAGS) value is, in the prog target and all the rules it raises (prog. o foo. o bar. o rules), $ (CFLAGS) values are "-g"


Mode Variable
%. O: CFLAGS =-O



Libs_for_gcc =-lgnu
Normal_libs =

Foo: $ (objects)
Ifeq ($ (CC), gcc)
$ (CC)-o foo $ (objects) $ (libs_for_gcc)
Else
$ (CC)-o foo $ (objects) $ (normal_libs)
Endif

Ifdef ;

If the variable The; value is not null, so the expression is true.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.