Write make file

 

make Makefile to perform

cmake cross-platform project management tool with more abstract syntax to organize the project. Although still a target, something like dependence, but more abstract and friendly, like you represent the available math math library, without the need to specify in the end is math.dll or libmath.so, in windows it will support generation visual studio project, under linux it will generate a Makefile, it can even generate eclipse project file. In other words, starting from the same abstract rules, it is customized for each compiler project file.

When the specific use, under Linux, small projects can be written manually Makefile, large engineering automake to help you generate a Makefile, in order to cross-platform, use cmake.

cmake and make only one key commands, make for processing Makefile, cmake for translation CMakeLists.txt

Write makefile

 

 

Use variables improved version:

 

 

GNU make is very powerful, it can automatically deduce the file and the following command file dependencies, so we did not need to go after every [.o] files are written on a similar order, because we make will automatically recognize, and derive their own command.

 

Just make see a [.o] file, it will automatically put [.c] file in the dependency plus, if you make a whatever.o find, then whatever.c, would be dependent files whatever.o . And cc -c whatever.c will be derived, so we no longer have to write the makefile so complicated. Our new makefile and baked.

 

 

 

 

Continue to improve:

 

 

 

 

Header files included have to fill dependency, otherwise only update the header file, make will not be updated.

makefile can follow the time stamps to determine whether the files need to be recompiled.

 

How to make works

 

In the default mode, that is, we just enter the make command. Well,

 

    1, make will find name "Makefile" or "makefile" file in the current directory.

    2, if found, it will find the file in the first target file (target), in the example above, he would find the "edit" the document, and this document as the ultimate destination file.

    3, if the edit file does not exist, or edit files depends .o file modification time later than the new edit this file, then he will execute the command as hereinafter defined to generate edit this file.

    4, if the edit depends .o files exist, make will find in the current file is dependent target .o file, and then if you find that a .o files are generated according to the rules. (This process is a bit like a stack)

    5, of course, your C files and H files exist anyway, so make generates a .o file, then use the ultimate task of life make .o files, and edit the file is executed.

 

This is the whole make-dependent, make layer after layer will look for a dependency file until finally compile the first target file. In the process of finding, if an error occurs, such as last dependent file is not found, then it will make exit and error, and for the command defined errors, or compile unsuccessful, make simply ignore. just make file dependency, that is, if after I found a dependence, after the colon file or not, then I'm sorry, I do not work it.

 

Through the above analysis, we know that like clean, not directly or indirectly associated with the first target file, the command defined behind it will not be automatically executed, but we can show to make execution. That command - "make clean", in order to clear all the object files to recompile.

We can use a function of C / C ++ compiler. Most of the C / C ++ compilers support a "-M" option, which automatically finds the header file contained in the source file, and generates a dependency.   

 gcc -M main.c output is:

 

    main.o: main.c defs.h /usr/include/stdio.h /usr/include/features.h /

         /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h /

         /usr/lib/gcc-lib/i486-suse-linux/2.95.3/include/stddef.h /

         /usr/include/bits/types.h /usr/include/bits/pthreadtypes.h /

         /usr/include/bits/sched.h /usr/include/libio.h /

         /usr/include/_G_config.h /usr/include/wchar.h /

         /usr/include/bits/wchar.h /usr/include/gconv.h /

         /usr/lib/gcc-lib/i486-suse-linux/2.95.3/include/stdarg.h /

         /usr/include/bits/stdio_lim.h

 

 

    gcc -MM main.c output is:

 

    main.o: main.c defs.h

$ @ Represents the target file

$ ^ Represents all dependent files

$ <Represents a dependency file

$? Representation is newer than the target list of dependencies

 

 

Guess you like

Origin www.cnblogs.com/sunmeng1994/p/11120501.html