Compiling Makefile in linux

Source: Internet
Author: User
Compile Makefile files in linux-general Linux technology-Linux technology and application information. For details, refer to the following section. When you start programming in Linux, the annoying problem is how to write Makefile files.
Not as familiar as Windows, there are so many good software (maybe it's just a bit ignorant of Linux ). Although
An integrated compilation environment like Kylix and Anjuta, but Kylix is too slow to compile the console program.
It's no less powerful than an anti-DDoS gun, And Anjuta is too unstable, and the font is so ugly. No
Now, let's get down to the truth and see how to write Makefile.



1. Simple GCC Syntax:

If you only have one file (or only a few files), you can leave the Makefile file empty (of course there are
Makefile is more convenient), just compile it directly with gcc. Here we will only introduce a few frequently used
The first parameter is "-o". The parameter after it indicates the target file to be output, and the other parameter is "-c ",
Indicates only compiling (Compile), not connecting (Make), if there is no "-c" parameter, it indicates connection
, The following commands:

Gcc? C test. c, indicates that only the test. c file is compiled. If the test. c file is successfully compiled, the test. o file is output.

Gcc? C test. c? O test. o, which is exactly the same as the previous command

Gcc? O test. o, connect test. o to an executable binary file test.

Gcc? O test. c. compile test. c and connect it to an executable binary file test.

Gcc test. c? O test, same as the previous command

Gcc? C test1.c: Only test1.c is compiled. If test1.o is successfully compiled, the target file test1.o is output.

Gcc? C test2.c: Only test2.c is compiled. If test2.c succeeds, the target file test2.o is output.

Gcc? O test test1.o test2.o connects test1.o and test2.o to an executable binary file test


Gcc? C test test1.c test2.c. Compile test1.o and test2.o and connect them to executable binary data.
File test

Note: If you want to compile the cpp file, use g ++. Otherwise, an error similar to the following may occur:

Cc3r3i2U. o (. eh_frame + 0x12): undefined reference to '_ gxx_personality_v0'
......

Another parameter is the "-l" parameter, which is closely linked to the connected library, such as multiline
If you use the pthread_create function, you should add "-lpthread
","-L "indicates a connection, and" pthread "indicates the database to be connected. Note that they must be connected and written together here.
For example, if you use the cursor library curses, you should add "-lcurses" to the end, such
Statement:

Gcc? O test test1.o test2.o? Lpthread? Lcurses

Of course, I think there are hundreds of gcc parameters, but we usually use them on x86 machines.
This is not a GCC tutorial, so stop it.



2. Makefile basic syntax

I don't have a detailed design book for Makefile here, just by reading other people's Makefile files and some online Parameters
Test materials and make some simple introductions (I understand it myself. If not, please point out that
I would be very grateful)

2.1 goals:

Make all and make install are common when you are reading Makefile files.
, Make clean and other commands, there is also only one Makefile file, then all, install, clean Parameters
How can I control the running of Makefile?
Let me know what I mean )? Here, if the above command can be correctly run,
So there must be several lines in the Makefile file, and their start must be

All: ×××××××

*

Install: ××××××

*

Clean: ×××××××××

*

Of course not all, because we can replace all, install, and clean with other variables,
For the sake of simplicity, let's make a conclusion. Don't be surprised.

All, install, and clean mentioned above are what we call the goal. Make all command.
Make: we will execute the target specified by all. To facilitate understanding of the Make process, let's take a look.
Makefile files unrelated to gcc:

# Indicates the comments in the Makefile file. The specific content of the Makefile is as follows:

All:

@ Echo you have typed command "make all"

Clean:

@ Echo you have typed command "make clean"

Install:

@ Ehco you have typed command "make $ @"

# Makefile end

Note that here, all:, clean:, install: The Rows must be at the top, and all @ echo should be preceded by the tab key to jump.
Indentation. The running result is as follows:

[Root @ xxx test] # cat Makefile

# Indicates the comments in the Makefile file. The specific content of the Makefile is as follows:

All:

@ Echo you have typed command "make all"

Clean:

@ Echo you have typed command "make clean"

Install:

@ Ehco you have typed command "make $ @"

[Root @ xxx test] # make all

You have typed command "make all"

[Root @ xxx test] # make clean

You have typed command "make clean"

[Root @ xxx test] # make install

You have typed command "make install"

[Root @ xxx test] #

I don't know if this is the case. We have a $ @ symbol in the Makefile file, where $ indicates the variable name and its
$ @ Is a pre-defined variable in Makefile, indicating the target command, such
The above file belongs to the install target, then $ @ indicates install. Similarly, if you clean the target
Replace "make clean" with "make $ @" in the following quotation marks, so the output of the command make clean is
It turns out to be the same. You can try it.

2.2 dependency

We now raise the following question: how can I use a make command to replace all make, make
What about the install and make clean commands? Of course, we can write a Makefile file as we did:

[Root @ xxx test] # cat Makefile

# Indicates the comments in the Makefile file. The specific content of the Makefile is as follows:

All:

@ Echo you have typed command "make all"

Clean:

@ Echo you have typed command "make clean"

Install:

@ Ehco you have typed command "make $ @"

Doall:

@ Echo you have typed command "make $ @ l"

@ Echo you have typed command "make all"

@ Echo you have typed command "make clean"

@ Ehco you have typed command "make install"

[Root @ xxx test] # make doall

You have typed command "make doall"

You have typed command "make all"

You have typed command "make clean"

You have typed command "make install"

[Root @ xxx test] #

Here, doall: The target has four-tone statements, which are connected together and start with the tab key. Of course, this
The task can be completed, but it is too stupid. Let's write it like this:

[Root @ xxx test] # cat Makefile

# Indicates the comments in the Makefile file. The specific content of the Makefile is as follows:

All:

@ Echo you have typed command "make all"

Clean:

@ Echo you have typed command "make clean"

Install:

@ Ehco you have typed command "make $ @"

Doall: all clean install

@ Echo you have typed command "make $ @ l"

[Root @ xxx test] # make doall

You have typed command "make all"

You have typed command "make clean"

You have typed command "make install"

You have typed command "make doall"

[Root @ xxx test] #

I believe you have seen the doall: Running method. It runs the all target first, and then runs the clean target.
The next step is install, and the last step is your own target, and each $ @ still maintains its own target name.

Here, we call all, clean, and install as the target of doall.
Lai. That is, if you want to execute doall, first execute them (all, clean, install), and finally execute me
.

Note that dependency must be the goal in Makefile. Otherwise, you have to run it and the ending is doomed:

[Root @ xxx test] # cat Makefile

All:

@ Echo you have typed command "make all"

Xxx: all WAHAHA:

[Root @ xxx test] make xxx


You have typed command "make all"

Make: *** No rule to make target 'wahahaha', needed by 'xxx', Stop.

[Easy] Can we use "mutual dependency" to make it?

[Root @ xxx test] # cat Makefile

Tar1: tar2

Tar2: tar1

@ Echo this line cann't be shown on you screen!

[Root @ xxx test] make tar1

Make: Circular tar2 <-tar1 dependency dropped.

Oh, you can't lie.



3. Practice:

With the above instructions, we can start to write some mentally retarded Makefile files. For example
File:

Tmp/
+ ---- Include/
| + ---- F1.h
| + ---- F2.h
+ ---- F1.c
+ ---- F2.c
+ --- Main. c

In f1.c, # include "include/f1.h", # include "include/f2.h" in f2.c, and main
# Include "include/f1.h", # include "include/f2.h" in. c. The main function is in main. c.
To compile the files whose target is testmf together, we can write them in the following way (of course
Is mentally retarded ):

[Root @ xxx test] # cat Makefile

Main: main. o f1.o f2.o

Gcc? O testmf main. o f1.o f2.o

F1.o: f1.c

Gcc? C? O file1.o file1.c

F2.o: f2.c

Gcc? C? O file2.o file2.c

Main. o

Gcc? C? O main. o main. c

Clean:

Rm? Rf f1.o f2.o main. o testmf

[Root @ xxx test] make

Gcc? C? O main. o main. c

Gcc? C? O file1.o file1.c

Gcc? C? O file2.o file2.c

Gcc? O testmf main. o f1.o f2.o

[Root @ xxx test] ls

Gcc? C? O main. o main. c

Gcc? C? O file1.o file1.c

Gcc? C? O file2.o file2.c

Gcc? O testmf main. o f1.o f2.o

[Root @ xxx test] ls

F1.c f1.o f2.c f2.o main. c main. o include/testmf

If your program is okay, You can execute./testmf.

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.