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.