Linux programming-makefile file

Source: Internet
Author: User
Linux programming-makefile file-general Linux technology-Linux programming and kernel information. The following is a detailed description. Today I want to talk about this makefile file. makefile? It may be that I am ignorant. I have never heard of this stuff on the windows platform. In fact, there are also some, but we are not in touch with it.

Makefile. What is it? What is the purpose? How to write? This is what I want to talk about.

We all know that developing a program in the traditional C and C ++ languages requires the following steps: Edit, compile, and connect. The connection runs successfully. On the windows platform, these steps are automatically completed with the help of development tools, so we are not very impressed. On the linux platform, we need to follow this process by ourselves (this sentence is not very correct, and there are already some development tools on linux ). Well, it seems that the makefile file is closely related to this process.

For example, print "hello world!" on a linux terminal !".

My machine is redhat9.0, And the compiler uses g ++ 3.0.

Edit a main. cpp file with the following content:

# Include

Int main (int argc, char ** argv ){

Printf ("hello, world! \ N ");

Return 0;
}

After editing, you should compile the code. Enter the directory where the code file is located from the terminal and execute the following command:

# G ++-Wall-o2-c main. cpp

# Is a command prompt. For the usage of g ++ commands and the significance of each parameter, please refer to other books. I will not describe this too much here.

After this command is executed, the directory contains an additional main. o file. The appearance of this file illustrates some problems. For our developers, there is no syntax error in our source code! If there is a syntax error, the compiler will print the error information to the terminal window. If not, modify the code, save the modification, and then run the preceding command.

After the compilation is passed, this is not a small step. Then the connection is completed. Execute the following command:

# G ++ main. o-o main.exe

This command connects main.oto mian.exe. In a project, a running file is connected by many. o files and other library files provided by the operating system. It is not as simple as here. At this time, a main.exe file is displayed in the directory. Check the attributes of the file and you can see that it has the running attributes. By the way, this is the final executable file we want. If the value is correct, the main.exe file name here is arbitrarily specified. The system will not assign the running attribute to it because we add an exe suffix, Nor will this running attribute be canceled without the exe suffix.

Okay. Run the file:

#./Main.exe

Press enter to view the "hello world!" printed on the terminal !" .

Now I have some functions in another file, in main. use these functions in the cpp file. For example, the other code files are funtion. h and function. cpp file, in main. the cpp file must contain funtion. h file (# include "function. h "), and then

Run the following command:

# G ++-Wall-o2-c function. cpp
# G ++-Wall-o2-c main. cpp

When both the function. o file and the main. o file exist, connect

# G ++ main. o function. o-o main.exe

Now, the new main.exe file is connected.

If funtion. h and function. if the content in the cpp file is changed, the funtion will be re-compiled. o file, dependent on funtion. h and function. main. the cpp file also needs to be compiled, and the connection needs to be done more. It is annoying. If there are many code files, the dependency (Inclusion) relationship is complicated, and a file is modified, which one. o files need to be re-compiled, compiled, and re-connected. This is tedious and error-prone, and the operation is unrealistic.

What should I do? Look! The makefile file appears.

Makefile is a shell file (called in linux, equivalent to a batch file in windows. bat), which records the dependency between code files. The makefile file determines which file needs to be re-compiled.

Let's first look at a specific makefile file:
QUOTE:
Related Article

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.