Makefile is so simple. Why are the tutorials so complicated?

Source: Internet
Author: User
My younger brother just learned C/C ++ programming in Linux. My colleague told me to use makefile for compilation and asked me how to write makefile. A: makefile is very difficult, you can use a highly handwritten makefile from abroad for compilation.
In doubt, open the so-called master makefile. It's really long and I don't understand it.
If you are not convinced, go to the Internet to find some makefile tutorials, which are full of rankings that cannot be understood by file dependencies or targets.
So the paradox is rooted in our hearts.

Two weeks later, today, an accidental test showed me that ---- makefile is really simple. Why is the tutorial so complicated?
Of course, what I mentioned in this article is "Hello World!" for beginners! "Makefile level, does not deny the complexity and power of the advanced functions of makefile. The makefile That I can understand now is just a batch processing program. (Like shell files in Linux)
The key process of makefile is to call the Compilation Program to execute the compilation. In Windows, Cl is used, and in Linux, G ++ is used. In Windows, the makeprogram is nmake.exe and in Linux, It is make.

Let's first write a simple C program:

# Include <stdio. h>

Int main ()
{
Printf ("Hello world! /N ");
Getchar ();
Return 1;
}
 

Save this program as test. cpp.
Compile in Windows (first run C:/programe files/vs2003/vc7/bin/vcvars32.bat): CL test. cpp
Execute the compilation in Linux: G ++ test. cpp

Easy!
Then write the MAKEFILE file:

Anyname:
CL test. cpp
# In Linux, the last line is written as G ++ test. cpp.
# Note the CL line: the front indent is a tab key. It must be a tab.
# The anyname In the first line can be any name, but a colon must be added.

Use nmake compiling in Windows (execute C:/programe files/vs2003/vc7/bin/vcvars32.bat first): nmake.exe
Use make in Linux to compile: Make

It's easy, right? In fact, most makefiles define variables and use the values of these variables as Cl or G ++ parameters.
I hope any tutorial will show a hello World first and then start preaching. For cainiao, confidence in the first success is the best encouragement.

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.