Make and makefile

Source: Internet
Author: User

 

Makefile and makefile are equivalent.

$ MV makefile
$ Make
Gcc-C main. c
Gcc-C Liu. c
Gcc-C generatedatafile. c
Gcc-G main. O Liu. O generatedatafile. O-o Liu
-L/usr/local/MySQL/lib/MySQL-lmysqlclient-LZ
Rm-f *. o

Makefile format

<Target 1>:
Depend

<Command 1>

<Command 2>

MAKEFILE file format -------- use the section (rule) as the basic structure

$ Cat makefile
Libs =-L/usr/local/MySQL/lib/MySQL-lmysqlclient-LZ
ALL: Liu
Clean
Two sections of all depend

Liu: Main. O Liu. o
Generatedatafile. o
Liu depend

Gcc-G $? -O $ @ $ (libs)
Main. O: Main. c

Gcc-C
$?
You must have a tab (that is, the tab key)

Liu. O: Liu. c

Gcc-C Liu. c
Generatedatafile. O: generatedatafile. c

Gcc-C $?
Clean:

$ (RM) *. o

 
The command line must use a tab and cannot contain a string of spaces.


Tab is the flag of the command line.

[MACG @ localhost mysqltmp] $ more makefile
Liu: Main. O Liu. O generatedatafile. o

Gcc-G main. O Liu. O generatedatafile. O-o Liu-L/usr/lib/MySQL-l
Mysqlclient-LZ
Main. O: Main. c comm. h

Gcc-C main. c
Liu. O: Liu. c/usr/include/MySQL. h

Gcc-C Liu. c
Generatedatafile. O: generatedatafile. c

Gcc-C generatedatafile. c

Only make a single section

$ Make main. o
Gcc-C main. c
Main. O: Main. c

Gcc-C $?

Make install and make
Clean is also a single section

# Cat makefile
...
Install:
...
Clean:
...

Menu-based Section

Reference, written directly at depend

$ VI makefile
Libs =-L/usr/local/MySQL/lib/MySQL-lmysqlclient-LZ

Liu: Main. o
Liu. O generatedatafile. O clean


Gcc-G main. O Liu. O generatedatafile. O-o Liu
$ (Libs)

Liu depend: Main. O Liu. o
Generatedatafile. O clean

Main. O: Main. c

Gcc-C main. c
Liu. O: Liu. c

Gcc-C Liu. c
Generatedatafile. O: generatedatafile. c

Gcc-C generatedatafile. c
Clean:

$ (RM) *. o

$ Make
Gcc-C main. c
Gcc-C Liu. c
Gcc-C generatedatafile. c
Rm-f *. o

Gcc-G main. O Liu. O generatedatafile. O-o Liu
-L/usr/local/MySQL/lib/MySQL-lmysqlclient-LZ
GCC: Main. O: no such file or
Directory

GCC: Liu. O: no such file or
Directory

Because the overall execution sequence:
O depend
Conclusion 1,
O depend
Conclusion 2,
O depend
Conclusion 3,
O
Depend Summary 4 (clean ),

O gcc-G main. O Liu. o
Generatedatafile. O-o Liu $ (libs)

So before GCC link, the *. O is deleted first, so an error occurs.
Correct practice
$ VI makefile
Libs =-L/usr/local/MySQL/lib/MySQL-lmysqlclient-LZ

ALL: Liu clean

Set a general menu bar

O
The first step is to compile and link is to compile

O
The last step is deleted.

Liu: Main. O Liu. O generatedatafile. o

Gcc-G main. O Liu. O generatedatafile. O-o Liu $ (libs)
Main. O: Main. c

Gcc-C main. c
Liu. O: Liu. c

Gcc-C Liu. c
Generatedatafile. O: generatedatafile. c

Gcc-C generatedatafile. c
Clean:

$ (RM) *. o

$ Make
Gcc-C main. c
Gcc-C Liu. c
Gcc-C generatedatafile. c
Gcc-G main. O Liu. O generatedatafile. O-o Liu
-L/usr/local/MySQL/lib/MySQL-lmysqlclient-LZ
Rm-f *. o


Makefile comments are similar to shell, and all texts starting with "#" are comments.

The command in the Section (rule) is a common Unix Command, which can be a GCC command or any UNIX
Command


Install: Liu

Chmod 750 Liu


CP Liu/usr/bin
 
Liu: Liu. o

GCC Liu. O-o Liu
Run the depend Liu summary first to generate the executable file Liu
Change the generated Executable File Permission

Copy the generated executable file to the appropriate installation directory.

Another example:

Clean:

Rm-F
*. O
Delete the *. o file generated in the middle

 
Usually add a delete statement to the last link to delete the *. o file generated in the middle.

$ VI makefile
Ldflags =-L/usr/local/MySQL/lib/MySQL-lmysqlclient-LZ

Liu: Main. O Liu. O generatedatafile. o

Gcc-G main. O Liu. O generatedatafile. O-o Liu $ (ldflags)

Rm-f *. o

$ Make
Gcc-C main. c
Gcc-C Liu. c
Gcc-C generatedatafile. c
Gcc-G main. O Liu. O generatedatafile. O-o Liu
-L/usr/local/MySQL/lib/MySQL-lmysqlclient-LZ
Rm-f *. o

$ Ls
Comm. h
Generatedatafile. c
Liu. c
Makefile
Testm
Dataf
Liu
Main. c
Makefile. Bak
Testm. c



Use the internal macro $ (RM) of make to delete the file.

VI makefile
Liu: Main. O Liu. O generatedatafile. o

Gcc-G main. O Liu. O generatedatafile. O-o Liu $ (ldflags)

$ (RM) *. o
$ Make
Gcc-C main. c
Gcc-C Liu. c
Gcc-C generatedatafile. c
Gcc-G main. O Liu. O generatedatafile. O-o Liu
-L/usr/local/MySQL/lib/MySQL-lmysqlclient-LZ
Rm-f *. o


Replacing macro variables in the MAKEFILE file and referencing macro variables must include $ ()

$ VI makefile
Ldflags
=-L/usr/local/MySQL/lib/MySQL
-Lmysqlclient-LZ

Liu: Main. O Liu. O generatedatafile. o

Gcc-G main. O Liu. O generatedatafile. O-o Liu $ ldflags

$ Make
Gcc-C Liu. c
Gcc-G main. O Liu. O generatedatafile. O-o Liu dflags

GCC: dflags: no such file or
Directory

* ** Error code 1
 

$ VI makefile
Liu: Main. O Liu. O generatedatafile. o

Gcc-G main. O Liu. O generatedatafile. O-o Liu $ (
Ldflags)

$ Make
Gcc-G main. O Liu. O generatedatafile. O-o Liu-L/usr/local/MySQL/lib/MySQL-lmysqlclient
-LZ

 

Why does make add $ () to referenced variables ()?

Because it is not a variable, it is a macro.

All file names in makefile are path names and can contain directory paths, such as src/Main. C.

Main. O: Main. c comm. h

Gcc-C main. c
$ (INC)
The source file is in the current directory.

Main. O: src/Main. c Include/COMM. h

Gcc-C src/Main. c
$ (INC)
The source file is in a subdirectory.

$ @ And $? Usage:

Target
: Depend

$ @

: $?

Liu
:
Main. O Liu. o
Generatedatafile. o


Gcc-G $?
-O $ @
$ (Libs)

$?
Can replace multiple (all) depend

$ VI makefile
Libs =-L/usr/local/MySQL/lib/MySQL-lmysqlclient-LZ
ALL: Liu clean

Liu: Main. o
Liu. O generatedatafile. o


Gcc-G $?
-O $ @
$ (Libs)

Main. O: Main. c


Gcc-C $?

Liu. O: Liu. c


Gcc-C Liu. c

Generatedatafile. O: generatedatafile. c

Gcc-C $?
Clean:

$ (RM) *. o

No. h file is required during compilation.

$ VI makefile
Main. O: Main. c comm. h

Gcc-C main. c


The essence of the so-called "Compilation without. H" file-What Is depend used?

Description of depend: if the target check finds any new depend, recompile it.
.

<Target 1>: depend

<Command 1>

<Command 2>

The following depend comm. H is necessary because comm. H is a custom header file.
, May be modified

Main. O: Main. c
Comm. h

Gcc-C main. c

The following mysql. H is unnecessary because it is a standard header file and cannot be modified or depend.
Other standard header files, such as stdio. H, do not need to appear in depend.

Liu. O: Liu. c/usr/
/Include/MySQL. h

Gcc-C Liu. c

If depend is a section, redirect it to the Section and continue to check the timestamp of depend.
 

Liu: Main. o

Main. O: Main. c

If the custom header file also includes the custom header file (serial set), it should also be used in depend

Depend
Xxx. h file defects, cannot continue to use $? Now

Main. O: Main. c comm. h

Gcc-C $?
$ Make
Gcc-C src/Main. c Include/COMM. H-iinclude

Apparently $? Compile comm. h.
Now
The correct method: directly use the file name, no $?

Main. O: src/Main. c Include/COMM. h

Gcc-C src/Main. C $ (INC)

You can only use the file name directly, not $? Now


Make with Structured Programming

  • Directory tree

Mysqltmp --- SRC --- main. c Liu. c generatedatafile. c

--- Include -- comm. h

--- Lib -- libcomm. So or libcomm.

  • In the top-level directory
    Create makefile

Call the. c file compilation in the SRC subdirectory (depend and command in the rule can both contain paths ).

VI makefile
Libs =-L/usr/local/MySQL/lib/MySQL-L/export/home/MACG/mysqltmp/lib
-Lmysqlclient-LZ-lcomm
INC =-iinclude

ALL: Liu clean

Liu: Main. O Liu. O generatedatafile. o

Gcc-G $? -O $ @ $ (libs)
Main. O: src/
Main. c


Gcc-C $? $ (INC)
Custom header files cannot be repeatedly included


We recommend that you include multiple files in Main. C)

Or use the File Include (assuming only one file is used)

Liu. O: src/
Liu. c

Gcc-C $?
Generatedatafile. O: src/
Generatedatafile. c

Gcc-C $?
Clean:

$ (RM) *. o

  • Make
$ Make
Gcc-C src/Liu. c
Gcc-C src/generatedatafile. c
Gcc-G main. O Liu. O generatedatafile. O-o Liu
-L/usr/local/MySQL/lib/MySQL-lmysqlclient-LZ
Rm-f *. o
[MACG @ machome]:/export/home/MACG/mysqltmp> $ ls
-F
Makefile
Dataf
LIB/
Src/

Liu *
Include/

 
[MACG @ machome]:/export/home/MACG/mysqltmp> $ ls
SRC
Generatedatafile. c
Liu. c
Main. c
[MACG @ machome]:/export/home/MACG/mysqltmp> $ ls
Include
Comm. h
[MACG @ machome]:/export/home/MACG/mysqltmp> $ ls
Lib
Comm. c
Comm. o
Libcomm. So

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.