The 8th chapter of Linux is makefile.
P287
Makefile's role is--automated compilation, once written, only need a make command (parse makefile, perform the actions described in makefile), the entire project can be completed automatically compiled,
No matter how many source code files the project has. Makefile defines a series of rules that specify which files need to be compiled first, which files need to be compiled,
which files need to be recompiled, or even more complex, because makefile is like a shell script, which can also execute commands from the operating system .
Most typically, in Microsoft Visual Studio, when you need someone else to write a DLL as part of your project, if the DLL uses multi-line libraries, and your project uses a single-threaded libraries,
The construction of this project will certainly fail. If you don't know this, you will never solve the problem.
Even if you don't have the skills to write makefile, you need to know how to use the Make tool. Because there is a large part of the software you need, or interested in, is released in source code mode.
Even if the software packaging options such as RPM and Deb are already in the Linux world, if you want to use the latest version of the software, you are basically not able to use the Make tool to create it from the source code.
Even with software developed in the language of Python and Perl, the vast majority of cases are using makefile as the installation script.
Of course, further, you have a great idea and are ready to share it with the world, so you need to master make and even makefile writing skills on Linux.
Because most people do, I think you are not strong enough to be able to change that reality. But if you must say no, then I have no way, then you'd better stay away from Linux, because Linux is far away from you.
For most Java programmers, however, Ant and Maven are probably the tools they are more likely to use or prefer.
This does not mean that makefile will appear to be a bit Ed when it comes to Java, because Makefile also provides good support for Java.
But why does Ant and Maven go to the leading position in this business, or are they relatively makefile shorter learning cycles, and Maven can easily solve some of the jar dependencies.
But here's one thing that always makes me wonder, is that when I talk to a lot of Java gurus about XML, they say it's useless, as if it's the root of all the problems, but Ant and Maven are the full XML format and they love it.
Perhaps just as conflict is the essence of drama, life is a play. but Ant and Maven are really powerful tools. It just seems to me that I prefer makefile because it is more flexible and descriptive, and can do more complicated tasks in a very simple way.
So even if you are a Java programmer, I hope you can master makefile, it may be able to broaden your horizons. But most of the time, ant or Maven is enough,
Just one more thing to say is that even though the IDE can provide good support for them, it's best to stay away from the IDE as a beginner. Because the IDE is a master Fender, but is a beginner's parasol (long not to bask in the sun will get sick)!
GNU make and its basic usage
In fact, Makefile is not the name of the project management tool, but it is a conventional term.
It is the default name of the engineering description file for a class of engineering management tools and is just the equivalent of Ant's Build.xml and maven pom.xml files.
What's more, the headache for beginners is that this file name is not unique. It can be makefile, which can be makefile (note that the file name of Linux is case-sensitive), or even gnumakefile.
Perhaps you have noticed, I just said Makefile is a kind of engineering management tools Engineering description file, not to say one, is not Zhang two monk? In fact, I also encountered the same confusion when learning makefile.
Later, I learned thatmakefile is the engineering description file of Make Engineering management tool, and make is a traditional engineering management tool of Unix-like world , it is part of their culture, it can be called makefile culture.
Then, since it is a culture, we have to allow flowers and schools of contention.
The main branches of traditional UNIX, such as Solaris, Hp-unix, AIX, etc. adopt a set of makefile system, BSD series such as: FreeBSD, OpenBSD, NetBSD adopt a set of makefile system;
Linux uses another set of makefile systems. And even if they belong to the same system, in some ways it will look very different.
In Linux, the GNU make system, part of the GNU program, is one of the most versatile and compatible makefile cultures in all systems.
The Linux kernel is compiled with two strong dependencies, one of which is GNU make and the other is GCC.
In fact, everyone will wonder, since Linux has such a good versatility and portability, why there are two strong dependence?
In fact, this is more fully explained that GNU make and GCC are enough NB, they have a very strong commonality and portability. They have been ported almost to all known and unknown platforms, so even if they have a strong reliance on Linux, it will not affect its versatility and portability at all.
GNU make is now the de facto "standard", as if it were a standard Mandarin, and all other make spoke in dialects. The content introduced in this chapter is also applicable to other make, after all, the difference between Putonghua and dialect is only in pronunciation, the specific morpheme and grammatical habits are consistent.
In Linux it is the make command to do the work (not necessarily in other systems).
Make is a tool under the Linux system and can be executed in any directory (/usr/bin/make), but with different effects.
Make will search for the makefile file in the execution path. As has been said before,Makefile is not the only choice, it can also be Makefile and gnumakefile.
If there are three of them in one path, make chooses one. But don't panic, this is a rule, if there is no rule is not disorderly sex?
Make is selected in the order of Gnumakefile, Makefile, and makefile.
Just select one and stop ignoring the others. At this point, make is still very specificity.
You can, of course, make it less exclusive. The method is to specify a working file for make with the "-F" option.
This time any file name can be, as long as the content conforms to Makefile's writing specification. There are two variants of the "-f" option, namely "--file=file" and "--makefile=file", which are functionally identical, depending on your preferences.
This is a very common command option, especially in cross-platform projects where different makefile files are available for different platforms. The
Another common command option is "-D" for debugging makefile. Very useful when writing a makfile file manually. However, don't expect to be able to step through the same way you would debug a C or Java program.
It only works by printing out the execution of makefile one by one, allowing you to see where an unexpected error has occurred. The Morph option is "-debug[=flags", which is more informative.
for those who just want to pass makefile as a reading code entry, make provides a "-t" option that will help you output all the target files needed to generate an executable file.
So you've ruled out irrelevant things, making this serious problem a lot easier. This process does not carry out the actual project creation process, so this does not affect the original nature of the code, just to help you do a good job of grooming, and the process is simple and fast. The
Warp option is-touch, and the functionality is identical.
There are many options available for the Make command, which are not listed here, and you can use the "-H" or "-help" option to view additional commands and descriptions if you are interested.
If you feel that the content you get from this command is a bit too thin, you can use the Man command to see its online Help manual, which is really useful!
The use of make is far more than the command option, but I'm staying in the next section. Because I always believe that if you want to really master a tool, you have to actually use it.
-F option: Specify Makefile
-T option: outputs all target files needed to generate an executable file
-d option: Debug makefile
8.2 Basic Concepts
First let's look at the basic concepts of makefile.
8.2.1 First example of makefile
There is a retire programmer who is bored and ready to practise calligraphy to kill time and hope to make a contribution. So invested heavily to buy the top four treasures. A day, after dinner suddenly arose. Some capable, paving paper, incense, quite a king, Yan of the almost. To be calm and busy, the face solemnly write the next line of words: Hello world.
Well, this is the sad program ape, as long as you learn something new, you always think of "Hello World". And some people actually claim to be using dozens of-door programming language, so we have to ask such a master. As a result, he only tells you how to write "Hello World" in every language. I really do not want to look for scold, so I show you the first example, and "HelloWorld" has nothing to do. See Code 1 for details.
Code Listing 1:
all:tinyedittinyedit:main.o line.o buffer.o tools.o chunk.o document.o cursor.occMAIN.O LINE.O TOOLS.O BUFFER.O-o tinyeditmyless:myless.o line.o buffer.o tools.o chunk.occmyless.o line.o tools.o buffer.o-omyless-lcursesmyless.o:myless.c line.h buf fer.h tools.h tedef.hcc-C-o myless.o myless.cdocument.o:document.c line.h buf fer.h cursor.h tools.h tedef.hcc-C-o document.o document.ccursor.o:cursor.c line.h tools.h tedef.hcc-C-o cursor.o cursor. cline.o:line.c line.h buf fer.h tools.h tedef.hcc-C-o line.o line,ctools.o:tools.c tools.h tedef.hcc-C-o tool. o Tools. CCHUNK.O:CHUNK.C line.h buffer.h tedef.hcc-C-o chunk.o Chunk. Cbuf fer.o:buf fer.c buf fer.h tools.hcc-C-o buffer.o buf fer.cmain.o:main.c line.h buf fer.h tedef.hcc-C-o Maln. o Maln. C
This is the makefile file that I have written in this editor, although it seems to be a lot of content, it is actually very well understood, and basically contains this part of what I want to tell.
This makefile can generate two executable files: Tinyedit and myless.
Tinyedit is a simple line-up editor: Myless is a simple text reader.
Based on the knowledge that we have learned earlier, when I execute the make command, Tmyedit is generated. So what do I do to build myless? To execute the make myless command.
Ah, smart you will be based on this association, I would like to execute make Love command, is not the generation of love? The answer disappoints you, and make will give an error. Because there is no love in this makefile, it cannot be generated.
This also gives us a sense that no love makes love is actually a mistake, is not love.
The parameter behind make is not a command option, but is used to indicate the target to be processed and eventually generated by the.
Here I guess you'll have a question. Is that when you generate Tinyedit, you don't specify a target? Well, that's a really good question, so let's go ahead and find out.
F
The 8th chapter of Linux is makefile.