1
00:00:00,000 --> 00:00:00,990
In this lesson,

2
00:00:00,990 --> 00:00:02,580
you're going to learn how to build software

3
00:00:02,580 --> 00:00:03,960
from its source code.

4
00:00:03,960 --> 00:00:05,040
This is a critical skill

5
00:00:05,040 --> 00:00:07,650
for any Linux system administrator to learn.

6
00:00:07,650 --> 00:00:10,140
One of the great things about most Linux distributions

7
00:00:10,140 --> 00:00:12,120
is the fact that they have a package manager

8
00:00:12,120 --> 00:00:13,680
that allows you to download software

9
00:00:13,680 --> 00:00:16,140
from a repository very easily.

10
00:00:16,140 --> 00:00:17,490
With these package managers,

11
00:00:17,490 --> 00:00:18,810
you have the ability to download

12
00:00:18,810 --> 00:00:20,760
and install new software on your computer

13
00:00:20,760 --> 00:00:22,950
in a completely automated manner.

14
00:00:22,950 --> 00:00:25,020
But despite all their efforts,

15
00:00:25,020 --> 00:00:27,090
the package maintainers simply can't handle

16
00:00:27,090 --> 00:00:28,830
each and every use case,

17
00:00:28,830 --> 00:00:31,050
nor can they carry every single package

18
00:00:31,050 --> 00:00:33,660
for every piece of software that might be out there.

19
00:00:33,660 --> 00:00:35,430
And so there are some situations

20
00:00:35,430 --> 00:00:36,810
where you're going to need to compile

21
00:00:36,810 --> 00:00:39,360
and install new software by yourself.

22
00:00:39,360 --> 00:00:41,160
Sometimes this is because you want to run

23
00:00:41,160 --> 00:00:43,170
a specific version of a piece of software,

24
00:00:43,170 --> 00:00:45,330
or because you want to modify the source code

25
00:00:45,330 --> 00:00:47,700
and use your own version that you're creating.

26
00:00:47,700 --> 00:00:48,930
And in both of these cases,

27
00:00:48,930 --> 00:00:51,300
you're going to have to download the original source code,

28
00:00:51,300 --> 00:00:53,040
make those modifications you want,

29
00:00:53,040 --> 00:00:54,990
and then compile it yourself.

30
00:00:54,990 --> 00:00:56,310
Now compiling software

31
00:00:56,310 --> 00:00:58,800
means you're taking the text based source code

32
00:00:58,800 --> 00:01:01,290
and compiling it down into binary code

33
00:01:01,290 --> 00:01:04,140
that your computer can understand and actually run.

34
00:01:04,140 --> 00:01:05,370
Now because of this,

35
00:01:05,370 --> 00:01:07,230
you're going to be able to modify that software

36
00:01:07,230 --> 00:01:08,850
when it's in a text based format

37
00:01:08,850 --> 00:01:10,710
but once you compile it into a binary,

38
00:01:10,710 --> 00:01:13,230
it no longer can have any changes made to it

39
00:01:13,230 --> 00:01:15,540
until you compile a new version.

40
00:01:15,540 --> 00:01:17,310
Now, compiling software manually

41
00:01:17,310 --> 00:01:19,320
does not use a management tool

42
00:01:19,320 --> 00:01:21,330
like the package software does.

43
00:01:21,330 --> 00:01:24,120
Now as you know, computers love binary.

44
00:01:24,120 --> 00:01:25,620
They like ones and zeros,

45
00:01:25,620 --> 00:01:27,990
but we as humans, we don't like that as much.

46
00:01:27,990 --> 00:01:30,030
We would rather communicate with words.

47
00:01:30,030 --> 00:01:31,560
So when I'm creating a program,

48
00:01:31,560 --> 00:01:33,690
I'm going to use a programming language.

49
00:01:33,690 --> 00:01:34,770
Now a programming language

50
00:01:34,770 --> 00:01:36,210
will allow me to type in things

51
00:01:36,210 --> 00:01:37,950
in a human readable format

52
00:01:37,950 --> 00:01:40,230
and then later on, when I'm ready to,

53
00:01:40,230 --> 00:01:42,750
I can compile that down into the ones and zeros

54
00:01:42,750 --> 00:01:44,820
that my computer can understand.

55
00:01:44,820 --> 00:01:46,620
This is the process of compiling,

56
00:01:46,620 --> 00:01:49,380
going from source code written by a programmer

57
00:01:49,380 --> 00:01:53,430
into this final binary software that the computer can run.

58
00:01:53,430 --> 00:01:54,660
Now, this is all done

59
00:01:54,660 --> 00:01:58,530
by using compilation software known as a compiler.

60
00:01:58,530 --> 00:02:00,840
Compilers are able to translate the source code

61
00:02:00,840 --> 00:02:02,550
that's written in that programming language,

62
00:02:02,550 --> 00:02:06,960
such as C or C++, into a machine readable binary.

63
00:02:06,960 --> 00:02:08,970
Now on Linux, there is a compiler

64
00:02:08,970 --> 00:02:11,640
built in by default known as GCC,

65
00:02:11,640 --> 00:02:14,100
which is the new compiler collection.

66
00:02:14,100 --> 00:02:16,020
This is the GCC utility

67
00:02:16,020 --> 00:02:18,660
and it has the ability to support different libraries

68
00:02:18,660 --> 00:02:20,340
for you to be able to compile software

69
00:02:20,340 --> 00:02:21,990
in different languages.

70
00:02:21,990 --> 00:02:24,510
Typically, software developers are going to provide a list

71
00:02:24,510 --> 00:02:26,160
of all the necessary libraries

72
00:02:26,160 --> 00:02:29,220
or dependencies for that application source code.

73
00:02:29,220 --> 00:02:31,140
And depending on the language you're using,

74
00:02:31,140 --> 00:02:33,660
this is either going to be inside of a header file

75
00:02:33,660 --> 00:02:36,750
known as a .h file, or a library file

76
00:02:36,750 --> 00:02:39,300
which has a .a file extension.

77
00:02:39,300 --> 00:02:41,970
Now program libraries are going to be chunked together

78
00:02:41,970 --> 00:02:44,250
with all these different pieces of compiled code

79
00:02:44,250 --> 00:02:45,930
that can then be used in the programs

80
00:02:45,930 --> 00:02:47,910
to accomplish specific tasks.

81
00:02:47,910 --> 00:02:51,090
These shared libraries enable more modular programming

82
00:02:51,090 --> 00:02:54,060
and allows us to reduce time when we're writing programs

83
00:02:54,060 --> 00:02:56,160
and when we're compiling them.

84
00:02:56,160 --> 00:02:57,690
Now, these are typically going to be included

85
00:02:57,690 --> 00:02:59,430
with most Linux distributions

86
00:02:59,430 --> 00:03:03,120
and they're placed in the /usr/lib/

87
00:03:03,120 --> 00:03:05,880
or /user/lib directory.

88
00:03:05,880 --> 00:03:08,370
This means anyone in the system has access to it

89
00:03:08,370 --> 00:03:10,320
and can call those libraries.

90
00:03:10,320 --> 00:03:11,880
There's also some other libraries

91
00:03:11,880 --> 00:03:15,210
in the /lib directory or /library directory

92
00:03:15,210 --> 00:03:17,160
and these are used by essential binaries

93
00:03:17,160 --> 00:03:20,370
such as the kernel and pieces of the operating system.

94
00:03:20,370 --> 00:03:21,510
Now, if you're trying to determine

95
00:03:21,510 --> 00:03:24,390
what libraries are being used by a given application,

96
00:03:24,390 --> 00:03:26,640
you can use the ldd command.

97
00:03:26,640 --> 00:03:28,740
The ldd command enables the user

98
00:03:28,740 --> 00:03:31,647
to view the shared library dependencies for any application.

99
00:03:31,647 --> 00:03:33,570
And this is useful when you're troubleshooting

100
00:03:33,570 --> 00:03:34,890
or gathering information

101
00:03:34,890 --> 00:03:36,450
about different system requirements

102
00:03:36,450 --> 00:03:37,830
for an application.

103
00:03:37,830 --> 00:03:39,540
To run the ldd command,

104
00:03:39,540 --> 00:03:41,910
simply type in ldd options,

105
00:03:41,910 --> 00:03:44,490
and the program binary that you want to check.

106
00:03:44,490 --> 00:03:45,930
Now, as a system administrator,

107
00:03:45,930 --> 00:03:47,730
you're going to download your software

108
00:03:47,730 --> 00:03:49,830
but now you have to build that software

109
00:03:49,830 --> 00:03:52,800
and this is what we call software compilation.

110
00:03:52,800 --> 00:03:55,290
To do this, you're going to download the file,

111
00:03:55,290 --> 00:03:56,520
you're going to decompress it,

112
00:03:56,520 --> 00:03:59,580
and now you're going to have that code ready to go.

113
00:03:59,580 --> 00:04:01,440
So when you download the code,

114
00:04:01,440 --> 00:04:05,250
you're going to download it usually as a tar.gzip file.

115
00:04:05,250 --> 00:04:06,330
Once you have that,

116
00:04:06,330 --> 00:04:09,030
you're going to decompress the gzip part of the file,

117
00:04:09,030 --> 00:04:11,460
and then you're going to untar the tar ball

118
00:04:11,460 --> 00:04:14,160
so you have access to all the underlying files.

119
00:04:14,160 --> 00:04:15,900
Now in this archive of files,

120
00:04:15,900 --> 00:04:18,600
you're going to find a file called the make file.

121
00:04:18,600 --> 00:04:20,880
The make file contains system information

122
00:04:20,880 --> 00:04:22,650
that is going to be able to tell the system

123
00:04:22,650 --> 00:04:24,150
how to compile this binary

124
00:04:24,150 --> 00:04:26,580
for your particular system and your options.

125
00:04:26,580 --> 00:04:28,530
To be able to configure this file properly,

126
00:04:28,530 --> 00:04:31,710
you should run ./configure as a command

127
00:04:31,710 --> 00:04:33,600
to gather information about your system

128
00:04:33,600 --> 00:04:35,880
that may be needed for that application.

129
00:04:35,880 --> 00:04:37,320
Now, inside of this make file,

130
00:04:37,320 --> 00:04:38,880
you can make the modifications

131
00:04:38,880 --> 00:04:41,610
to optimize the software for your particular needs

132
00:04:41,610 --> 00:04:43,770
or your particular systems' needs.

133
00:04:43,770 --> 00:04:45,360
Now, once you've done that,

134
00:04:45,360 --> 00:04:47,340
you can then use the make command

135
00:04:47,340 --> 00:04:49,020
to compile the application

136
00:04:49,020 --> 00:04:51,810
using the information stored in that make file

137
00:04:51,810 --> 00:04:54,420
and then you're going to type in make install

138
00:04:54,420 --> 00:04:57,150
and hit enter to install the resulting binary.

139
00:04:57,150 --> 00:04:59,817
So as you can see, this is a three step process.

140
00:04:59,817 --> 00:05:03,600
./configure gets the information about your system.

141
00:05:03,600 --> 00:05:05,370
Make is going to make that thing

142
00:05:05,370 --> 00:05:07,680
and compile the application and get it ready

143
00:05:07,680 --> 00:05:10,320
and then make install is going to install

144
00:05:10,320 --> 00:05:12,840
that resulting binary onto your system.

145
00:05:12,840 --> 00:05:15,870
Now, in most cases, once you have that make file created,

146
00:05:15,870 --> 00:05:17,310
you can simply issue the make

147
00:05:17,310 --> 00:05:19,110
and then the make install command

148
00:05:19,110 --> 00:05:22,050
without any special arguments to install the application.

149
00:05:22,050 --> 00:05:23,430
This is because the make command

150
00:05:23,430 --> 00:05:25,290
and the make install sub command,

151
00:05:25,290 --> 00:05:26,940
automatically look for the make file

152
00:05:26,940 --> 00:05:28,770
in the current directory that you're in.

153
00:05:28,770 --> 00:05:32,550
However, you can't issue various options if you need to.

154
00:05:32,550 --> 00:05:35,340
This make file is a file that contains instructions

155
00:05:35,340 --> 00:05:36,840
that are used by your compiler

156
00:05:36,840 --> 00:05:39,000
to build the program from the source code.

157
00:05:39,000 --> 00:05:41,160
These instructions inside the make file

158
00:05:41,160 --> 00:05:43,860
define the resources that the program is going to depend on

159
00:05:43,860 --> 00:05:45,450
in order to function properly,

160
00:05:45,450 --> 00:05:47,250
as well as any additional directives

161
00:05:47,250 --> 00:05:49,290
as defined by the developer.

162
00:05:49,290 --> 00:05:50,160
For example,

163
00:05:50,160 --> 00:05:52,980
let's say I'm creating an executable program called Dion

164
00:05:52,980 --> 00:05:55,410
and it relies on two object files,

165
00:05:55,410 --> 00:05:59,460
namely dionmain.o and dionfunction.o.

166
00:05:59,460 --> 00:06:00,990
Now what I would need to do first

167
00:06:00,990 --> 00:06:02,940
is be able to create those object files

168
00:06:02,940 --> 00:06:03,900
and I create those

169
00:06:03,900 --> 00:06:06,930
from the C programming language from my source code.

170
00:06:06,930 --> 00:06:09,270
So I would first compile those objects

171
00:06:09,270 --> 00:06:10,590
to their binary format

172
00:06:10,590 --> 00:06:15,590
by entering gcc -c dionmain.c and hitting enter.

173
00:06:15,660 --> 00:06:18,000
This will create the Dion main object file.

174
00:06:18,000 --> 00:06:22,320
Then I would enter gcc -c dionfunction.c

175
00:06:22,320 --> 00:06:26,190
to compile that into the binary file of dionfunction.o.

176
00:06:26,190 --> 00:06:28,860
Notice both of these end with a .o extension

177
00:06:28,860 --> 00:06:32,220
because they're compiled binary object files now.

178
00:06:32,220 --> 00:06:34,860
Now, if I want to link those back to my program,

179
00:06:34,860 --> 00:06:39,180
I can do that using gcc -o dion,

180
00:06:39,180 --> 00:06:40,560
which is the name of my program,

181
00:06:40,560 --> 00:06:42,575
and then the object files I want to link,

182
00:06:42,575 --> 00:06:45,570
dionmain.o and dionfunction.o.

183
00:06:45,570 --> 00:06:47,310
This allows Dion to run

184
00:06:47,310 --> 00:06:49,980
by relying on those two linked objects.

185
00:06:49,980 --> 00:06:51,600
Now, why would we use this approach

186
00:06:51,600 --> 00:06:52,680
instead of creating everything

187
00:06:52,680 --> 00:06:55,410
in a single C file and compiling it?

188
00:06:55,410 --> 00:06:57,750
Well, we do this because it allows us to make changes

189
00:06:57,750 --> 00:07:00,390
to each individual object or library

190
00:07:00,390 --> 00:07:03,210
without affecting the overall program dramatically.

191
00:07:03,210 --> 00:07:05,490
This also makes it easier for us to update things

192
00:07:05,490 --> 00:07:08,340
or troubleshoot things when things go wrong.

193
00:07:08,340 --> 00:07:10,230
And so we want to be able to do that

194
00:07:10,230 --> 00:07:13,830
by going through and compiling these things individually.

195
00:07:13,830 --> 00:07:14,820
Now, if I make a change

196
00:07:14,820 --> 00:07:17,010
to one of these underlying object files,

197
00:07:17,010 --> 00:07:19,917
I would then just need to use gcc -c

198
00:07:19,917 --> 00:07:23,940
and the name of that code, such as dionfunction.c,

199
00:07:23,940 --> 00:07:27,330
to be able to recompile that back to an object file.

200
00:07:27,330 --> 00:07:29,130
Now, whenever you go to install a program

201
00:07:29,130 --> 00:07:30,720
and you use the make command,

202
00:07:30,720 --> 00:07:33,180
it's going to be able to rebuild the program completely

203
00:07:33,180 --> 00:07:35,010
based on all those object files

204
00:07:35,010 --> 00:07:36,510
that have already been compiled

205
00:07:36,510 --> 00:07:39,150
and linking them all together into the finished product

206
00:07:39,150 --> 00:07:41,550
that you're going to be able to use on your system.

