Linux Makefile Basics

Source: Internet
Author: User
Article Title: Basics of compiling Makefile on Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Purpose:

I have mastered the usage of make and can program on Linux.

Environment:

Linux system, or a Linux server, connected through a terminal. One sentence: There is a Linux compiling environment.

Preparation:

Prepare three files: file1.c, file2.c, and file2.h.
File1.c:
# Include
# Include "file2.h"
Int main ()
{
Printf ("print file1 $ \ n ");
File2Print ();
Return 0;
}

File2.h:

# Ifndef FILE2_H _
# Define FILE2_H _

# Ifdef _ cplusplus

Extern "C "{

# Endif

Void File2Print ();

# Ifdef _ cplusplus

}

# Endif

# Endif


File2.c:
# Include "file2.h"
Void File2Print ()
{
Printf ("Print file2 *********************** \ n ");
}

Basics:

Here is an example:
There is such a Makefile. (The file and Makefile are in the same directory)
=== Makefile start ===
Helloworld: file1.o file2.o
Gcc file1.o file2.o-o helloworld

File1.o: file1.c file2.h
Gcc-c file1.c-o file1.o

File2.o: file2.c file2.h

Gcc-c file2.c-o file2.o


Clean:

Rm-rf *. o helloworld

=== Makefile ended ===

A makefile mainly contains a series of rules as follows:
A: B
(Tab)
(Tab)

Each command line must have a tab.

The makefile above aims to compile an executable helloworld file. Let's explain in one sentence:

Helloworld: file1.o file2.o: helloworld depends on file1.o file2.o.

Gcc File1.o File2.o-o helloworld: Compile the helloworld executable file. -O indicates the target file name you specified.

File1.o: file1.c: file1.o depends on the file1.c file.

Gcc-c file1.c-o file1.o: Compile the file1.o file. -C indicates that gcc only compiles the file for it into the target file. It is named by the source file name, but its suffix is changed from ". c "or". cc "to". o ". -O file1.o can be omitted in this sentence. By default, the compiler generates the file1.o file, which is the role of-c.

File2.o: file2.c file2.h
Gcc-c file2.c-o file2.o

These two sentences are the same as the previous two sentences.

Clean:

Rm-rf *. o helloworld

When you type the make clean command, the *. o and helloworld files are deleted.

To compile the cpp file, you only need to change gcc to g ++.

Write the Makefile file, and directly type the make command in the command line to execute the content in the Makefile.

In this step, I think you can compile a Helloworld program.

[1] [2] Next page

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.