Read Source code technology and art 1

Source: Internet
Author: User
It can be said that open source and analyze source Program Personnel, it is very meaningful and valuable.
Based on my experience, read Source code , Has at least three benefits.
First benefit,
Yes Learning To many programming methods and optimistic sources Code To improve your programming level, it is more helpful than writing source code. Of course, it doesn't mean you don't have to write your own code. Instead, you can learn more programming methods and skills from the middle of the good source code written by others.
The second advantage is that it can improve its ability to grasp large-scale source code.
A relatively large program often goes through many versions for a long time. Many people participate in development, correct errors, and add functions. Therefore, the size of source code is usually relatively large, with less than 10-kb and more than dozens of MB. when reading a large amount of source code, you can improve your ability to grasp the big software, quickly understand the context, and be familiar with the details, not only programming skills, but also in the program architecture, improve your design capabilities. (Here is an external remark. <Design Model> This book is believed to have been read by many people, and many people think about it as a classic. Now many books are named "Design Pattern. As mentioned in the book, the design pattern is not a teaching material, not to teach you how to compile programs, but to record some fixed patterns in programming at ordinary times and keep them going. Test And improvements, and some experience stories distributed to the majority of programmers. When I was reading this book, some design methods often made me feel familiar, and others were often used before. Some of these experiences have benefited from the coding process. Another important source is to read the source code written by others .)
The third advantage of reading source code is to get some good ideas. For example, many people like to go to sourceforge.net to find out whether someone has done the same or similar software before starting a software project. If so, read it, you can have a deeper understanding of this software project. I used to find a book about how to read the source code, but I didn't find it. On the contrary, I found a lot of books that analyze the source code, such Linux Kernel, Apache source, and so on. So I thought, why don't I write some experiences to share with you? (Of course it's not about writing a book, and you don't have that ability or time .)
So here I will use an example to illustrate how to read the source code and share some experiences! (Reference the example of a netizen)

the example I am looking for is a tool for collecting logs , Webalizer. (I used this tool before. It seems that I remember that the previous version was written in Perl. I don't know why the author has completely changed it to C. It may be for efficiency, or I remember it .) The reason why I chose this software as an example is that it is written in C, the process is relatively simple, and there are not so many branches of C ++ programs, and the software functions are not complex, the code is not large enough and can be completed in a Article ; another reason is that I modified it some time ago because of my work. I just read it and haven't forgotten it yet. :-) The example I used is webalizer2.01-09, you can also go to its website http://www.mrunix.net/webalizer/ download the latest version. This is written in C. It processes text files (in simple terms, it actually supports three log text formats: CLF, FTP, squid) and outputs results in HTML format. You can download its source code package and read the article while reading the program. Decompress its tar package (I downloaded its source code tar package) and see the following result in the file directory:

$ Ls
Aclocal. M4 dns_resolv.c Lang output. h webalizer.1
Changes dns_resolv.h Lang. h parser. c Webalizer. c
Configure graphs. c linklist. c parser. h Webalizer. h
Configure. In graphs. h linklist. h preserve. c webalizer_lang.h
Copying hashtab. c makefile. In preserve. h Webalizer. LSM
Copyright hashtab. h makefile. STD readme webalizer.png
Country-codes.txt install msfree.png readme. First
DNS. readme install-SH output. c sample. conf

 

First, I read its readme (this is a very important part) and have a general understanding of the software's functions, history, modification logs, and installation methods. Then install and run it in the default mode described in the following figure to check its output. (The installation is relatively simple, because it has a Configure. In no special cases, simple./configure, make, and make install can be installed .) Then, read the source code. Starting with makefile (I think this is the best way to understand a software), I start with makefile and have the following content:

Prefix =/usr/local
Exec_prefix =$ {prefix}
Bindir =$ {exec_prefix}/bin
Mandir =$ {prefix}/man/Man1
Etcdir =/etc
Cc = gcc
Cflags =-wall-O2
Libs =-LGD-lpng-LZ-LM
Defs =-detcdir = "/etc"-dhave_getopt_h = 1-dhave_math_h = 1
Ldflags =
Install =/usr/bin/install-C
Install_program =$ {install}
Install_data =$ {install}-M 644
# Where are the GD header files?
Gdlib =/usr/include

 

These define the installation path, the installation path of the execution program, the compiler, the installation path of the configuration file, the compilation options, the installation program, and the installation program options. Note that these are not written by the software author, but the output results of./configure. Haha. :-) below is the theme content, which we are also concerned about.

# Shouldn't have to touch below here!
ALL: Webalizer
Webalizer: Webalizer. O Webalizer. h hashtab. O hashtab. h
Linklist. O linklist. h preserve. O preserve. h
Dns_resolv.o dns_resolv.h parser. O parser. h
Output. O output. h graphs. O graphs. h Lang. h
Webalizer_lang.h
$ (CC) $ {ldflags}-O Webalizer. O hashtab. O linklist. O preserv
E. O parser. O output. O dns_resolv.o graphs. o $ {libs}
Rm-F webazolver
Ln-s Webalizer webazolver
Webalizer. O: Webalizer. c Webalizer. h parser. h output. h preserve. h
Graphs. h dns_resolv.h webalizer_lang.h
$ (CC) $ {cflags }$ {defs}-C Webalizer. c
Parser. O: parser. c parser. h Webalizer. h Lang. h
$ (CC) $ {cflags }$ {defs}-C parser. c
Hashtab. O: hashtab. c hashtab. h dns_resolv.h Webalizer. h Lang. h
$ (CC) $ {cflags }$ {defs}-C hashtab. c
Linklist. O: linklist. c linklist. h Webalizer. h Lang. h
$ (CC) $ {cflags }$ {defs}-C linklist. c
Output. O: output. c output. h Webalizer. h preserve. h
Hashtab. h graphs. h Lang. h
$ (CC) $ {cflags }$ {defs}-C output. c
Preserve. O: preserve. c preserve. h Webalizer. h parser. h
Hashtab. h graphs. h Lang. h
$ (CC) $ {cflags }$ {defs}-C preserve. c
Dns_resolv.o: dns_resolv.c dns_resolv.h Lang. h Webalizer. h
$ (CC) $ {cflags }$ {defs}-C dns_resolv.c
Graphs. O: graphs. c graphs. h Webalizer. h Lang. h
$ (CC) $ {cflags }$ {defs}-I $ {gdlib}-C graphs. c

 

Well, you don't have to read it any more. That's enough. Here we can see several source code files and their structures of the software. Webalizer. c is the file where the main program is located,OthersSome helper program modules. Compare the files in the directory,

$ Ls *. C *. h
Dns_resolv.c graphs. h Lang. h output. c parser. h Webalizer. c
Dns_resolv.h hashtab. c linklist. c output. h preserve. c Webalizer. h
Graphs. c hashtab. h linklist. h parser. c preserve. h webalizer_lang.h

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.