You get a bonus - 1 coin for daily activity. Now you have 1 coin

31 - Installing programs from source codes

Lecture



In this lecture, we will look at how to install the program from source codes in Linux . They also say to install from source-codes or " source ". Currently, for all popular Linux distributions, the software comes in the form of packages and there is no longer any need to compile and install programs from source codes. But you still need to be able to perform this operation.

As a rule, the source code of the program is distributed in the .tar.gz archive. Sometimes there may be other types of archives, but this is still the most common. What do tar.gz mean? They say that the source code of the program was archived with *** lump tar and then compressed with *** gzip . The com *** and tar ( type archive ) is one of the oldest Linux commands. Its task is to make an archive as a single file from any branch of the file system. Thus, the tar utility does not compress the data, but writes it uncompressed into a single file. Then, the created tar file is compressed by the gzip archiver. For example, create a directory targz in which create several subdirectories and files:

one
2
3
four
five
6
7
eight
9
ten
eleven
12
13
$ mkdir targz
$ mkdir -p ./targz/1
$ mkdir -p ./targz/2
$ mkdir -p ./targz/3
$ mkdir -p ./targz/4
$ echo "Hello hello HELLO"> ./targz/1.txt
$ cp ./targz/1.txt ./targz/2.txt
$ cp ./targz/1.txt ./targz/3.txt
$ cp ./targz/1.txt ./targz/4.txt
$ cp ./targz/1.txt ./targz/1/1.txt
$ cp ./targz/1.txt ./targz/2/1.txt
$ cp ./targz/1.txt ./targz/3/1.txt
$ cp ./targz/1.txt ./targz/4/1.txt

To create a tar archive (tar file) of the targz directory, execute a com *** at:

one
$ tar -cf targz.tar ./targz/

As a result, we get the file targz.tar . To unpack the tar archive, execute the following comma *** y:

one
$ tar -xvf targz.tar

To create a tar.gz archive from a tar archive, you need to compress it with the gzip utility:

one
$ gzip targz.tar

As a result, we get a tar.gz archive - targz.tar.gz . Note also that gzip deletes the original file when compressing. That is, the targz.tar file will be automatically deleted after the targz.tar.gz file is created . To unpack the gz-archive you need to use the com *** th gunzip :

one
$ gunzip targz.tar.gz

So, to unpack the tar.gz-archive you can use the following *** s tar and gunzip :

one
2
$ gunzip targz.tar.gz
$ tar -xvf targz.tar

or use a com *** in tar with an additional key - z , which automatically calls a com *** in gunzip to unpack the gz-archive:

one
$ tar -xvzf targz.tar.gz

As a result, we get the original targz directory. Another useful, albeit slightly cumbersome, construction that allows you to output the contents of the tar.gz archive to the console without unpacking it:

one
$ gzip -dc targz.tar.gz 2> / dev / null | tar tvvf -

So, we have learned how to unpack tar.gz archives in which the source codes of many programs are distributed. Now let's see how to compile a program from them and install it. For example, I propose to use the source code http-server - mini_httpd . You can download from here.

Let's get acquainted with the main service files, which are usually contained in source-codes. Extract the tar.gz archive:

one
$ tar -xzvf mini_httpd-1.19.tar.gz

Go to the source directory and see its contents:

one
2
3
four
five
6
7
eight
9
ten
eleven
12
13
14
15
sixteen
17
18
nineteen
20
21
22
$ cd ./mini_httpd-1.19/
$ ls -l
total 192
drwxr-xr-x 3 igor igor 4096 2003-12-20 06:05 contrib
-r - r - r-- 1 igor igor 340 2001-12-22 20:54 FILES
-r - r - r-- 1 igor igor 414 1999-09-28 21:49 htpasswd.1
-r - r - r-- 1 igor igor 4959 2001-12-19 02:08 htpasswd.c
-r - r - r-- 1 igor igor 326 2005-06-29 20:32 index.html
-rw-r - r-- 1 igor igor 3140 2002-11-02 01:02 Makefile
-r - r - r-- 1 igor igor 2661 2005-06-29 20:31 match.c
-r - r - r-- 1 igor igor 1679 2005-06-29 20:31 match.h
-rw-r - r-- 1 igor igor 199 2001-12-23 22:09 mime_encodings.txt
-rw-r - r-- 1 igor igor 4632 2003-10-26 19:00 mime_types.txt
-r - r - r-- 1 igor igor 16625 2005-06-29 20:31 mini_httpd.8
-r - r - r-- 1 igor igor 88322 2005-06-29 20:31 mini_httpd.c
-r - r - r-- 1 igor igor 1132 2001-12-20 09:14 mini_httpd.cnf
-r - r - r-- 1 igor igor 2249 2002-07-30 21:45 port.h
-r - r - r-- 1 igor igor 2157 2005-06-29 20:31 README
drwxr-xr-x 2 igor igor 4096 2003-12-20 06:05 scripts
-r - r - r-- 1 igor igor 8284 2005-06-29 20:31 tdate_parse.c
-r - r - r-- 1 igor igor 1575 2005-06-29 20:31 tdate_parse.h
-r - r - r-- 1 igor igor 231 2003-12-20 06:03 version.h

The rule of good tone is the presence in the source code of the README file in which the developers describe what the package is for, what it performs, the purpose of the main source files, the compilation conditions and other information that the developers consider necessary to write.

The next important file is the Makefile . This file is for the make utility. The make utility is designed to automatically implement a certain algorithm of actions necessary to compile or install an application. This algorithm of actions is described in the file Makefile . We will say that the make utility implements the goal described in the Makefile file. There may be several goals. Open the *** *** Makefile com and see some of its contents:

one
2
3
four
five
6
7
eight
9
ten
eleven
12
13
14
15
sixteen
17
18
nineteen
20
21
22
23
24
25
.....
BINDIR = / usr / local / sbin
MANDIR = / usr / local / man
CC = gcc
CDEFS = $ {SSL_DEFS} $ {SSL_INC}
CFLAGS = -O $ {CDEFS}
#CFLAGS = -g $ {CDEFS}
LDFLAGS = -s
#LDFLAGS = -g
LDLIBS = $ {SSL_LIBS} $ {SYSV_LIBS} $ {CRYPT_LIB}

all: mini_httpd htpasswd

mini_httpd: mini_httpd.o match.o tdate_parse.o
$ {CC} $ {CFLAGS} $ {LDFLAGS} mini_httpd.o match.o tdate_parse.o $ {LDLIBS} -o mini_httpd

mini_httpd.o: mini_httpd.c version.h port.h match.h tdate_parse.h mime_encodings.h mime_types.h
$ {CC} $ {CFLAGS} -c mini_httpd.c

match.o: match.c match.h
$ {CC} $ {CFLAGS} -c match.c

tdate_parse.o: tdate_parse.c tdate_parse.h
$ {CC} $ {CFLAGS} -c tdate_parse.c
.....

At the very beginning, the variables are initialized that will be used in the Makefile . For example, the variable BINDIR = / usr / local / sbin . Then comes this construction:

one
all: mini_httpd htpasswd

all is the name of the target . To the right of the colon is the condition for accomplishing this goal . This line reads like this: the all target will be reached when the mini_httpd and htpasswd goals are reached. Let's look at the mini_httpd target:

one
2
mini_httpd: mini_httpd.o match.o tdate_parse.o
$ {CC} $ {CFLAGS} $ {LDFLAGS} mini_httpd.o match.o tdate_parse.o $ {LDLIBS} -o mini_httpd

The mini_httpd goal , in turn, will be achieved when the *** *** and $ {CC} $ {CFLAGS} $ {LDFLAGS} mini_httpd.o match.o tdate_parse.o $ {LDLIBS} -o mini_httpd com number is successfully executed. binary file mini_httpd . But in order for the file to be successfully compiled, the goals mini_httpd.o , match.o and tdate_parse.o must be achieved. And so on through the chain. One goal depends on another.

How to understand the line $ {CC} $ {CFLAGS} $ {LDFLAGS} mini_httpd.o match.o tdate_parse.o $ {LDLIBS} -o mini_httpd ? Very simple. By substituting the values ​​of variables, it is converted to the form:

one
gcc -s mini_httpd.o match.o tdate_parse.o -lcrypt -o mini_httpd

gcc is the standard Linux compiler for C / C ++ languages . That is, if you type make mini_httpd on the command line, then a chain of actions described in the Makefile file will be executed, after which, if successful, a binary file will be created in the source directory.

Other goals in the Makefile are install , clean and tar . The clean target clears the source directory of the intermediate compilation files leaving only the source code. The tar target creates a tar.gz archive. And the install target installs the application into the system:

one
2
3
four
five
6
7
eight
9
install: all
rm -f $ {BINDIR} / mini_httpd $ {BINDIR} / htpasswd
-mkdir -p $ {BINDIR}
cp mini_httpd htpasswd $ {BINDIR}
rm -f $ {MANDIR} /man8/mini_httpd.8 $ {MANDIR} /man1/htpasswd.1
-mkdir -p $ {MANDIR} / man8
cp mini_httpd.8 $ {MANDIR} / man8
-mkdir -p $ {MANDIR} / man1
cp htpasswd.1 $ {MANDIR} / man1

As you can see, in order for the install target to succeed, it is necessary that the target all be completed , and the installation implies a simple operation to copy the compiled files to the appropriate file system directories.

Thus, if there is a Makefile in the directory with source files, then you can start compiling. By default, a com *** and make without a target will execute the target all . That is, make is equivalent to make all . After the successful execution of the make command, you can execute the *** at make install , which completes the installation of the program on the system.

If there is no Makefile in the source directory, but there is a Makefile.in file, then there should be a config script. The config script checks the system and creates a Makefile . Thus, in general, the commands for installing the program from source codes in the absence of a Makefile will be:

one
2
3
$ ./config
$ make
$ make install

If you execute a com *** in make in the directory ./mini_httpd-1.19/ , then we get an error compiling the target htpasswd.o , respectively, the target all cannot be realized. The README file says that the htpasswd utility is designed to change the password. The main file is mini_httpd . Therefore, to install the server, at least partially, you can write this:

one
2
$ make clean
$ make mini_httpd

The compilation is error-free and as a result, the mini_httpd file will be compiled. If you run this file as root, then open the browser and type http: // localhost in it , then we will see a message that the mini_httpd-server is working. It will only copy the file to the directory with binary files.

For homework, try changing the install target in the Makefile so that mini_httpd can work on your distribution and be controlled by the standard com *** s /etc/init.d/mini_httpd start | stop | restart .


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

LINUX operating system

Terms: LINUX operating system