How to read source code

Source: Internet
Author: User

Preface:
Since I published some articles on linuxaid.com.cn, some netizens have been sending emails or discussing some of these questions, or query the addresses of other articles (these netizens often read my articles reposted on other websites). I am glad that so many people have responded to my articles, this is my best appreciation, and I'm glad that so many people are interested in my articles. But it is often due to work relationships. There are a lot of emails asking where other articles can be found. I may not be able to reply in time, and I feel it is troublesome to reply to the same question. So I will repeat it here, all my articles for linuxaid.com.cn can be found in the Application Development Section of www.linuxaid.com.cn. Some of my articles are collected in bambi. may10.ca /~ Ariesram/articles below (this is a very simple web page, only text format of the article, also welcome interested netizens help me design the page), my mail address: ariesram@linuxaid.com.cn, or ariesram@may10.ca. Please repost this description on the website, and you are welcome to email me to discuss the problem, although I cannot guarantee timely reply.
Body:

Due to the work relationship, I often need to read some source code, make some modifications and use them, or use some of them for reference. It can be said that open source is meaningful for programmers. Based on my experience, reading Source Code has at least three advantages. The first advantage is that you can learn a lot of programming methods, optimistic source code, to improve your programming level, more help 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, rather than teaching you how to compile programs. Instead, it records some fixed patterns in programming and continuously tests and improves them, some experiences distributed to 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 have found many books for analyzing source code, such as 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!
The example I am looking for is a log statistics Tool, 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, so I can finish it in the length of an article. Another reason is that I used it to modify 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, and you can also download the latest version to its Web http://www.mrunix.net/webalizer. 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.
# Shouldnt 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, and others are some auxiliary 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
So let's start With webalizer. c.
As a C program, in the header file, the extern variables and structures defined in the C file are certainly not very small. However, we cannot understand this program simply by looking at these things. Therefore, starting from the main function, we will analyze the data structure and look back at the data structure definitions as needed. (By The Way, Visual C ++ and other IDE tools in windows provide a convenient way to obtain the function list, C ++ class list, and resource files, it is helpful for reading the source code. These tools are also available in Unix/Linux, but we will not talk about them here for the time being, but only through the simplest text editor vi ). Skip the copyright description Section Starting With webalizer. c (GPL

Related Article

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.