NATE HARDISON: In programming, a library is a collection of
related prewritten code.
Libraries are how we, as programmers, share common and
helpful code with each other, with the different programs we
write, and even with the different processes running at
the same time on our computers.
>> Let's explore a bit.
One of the more common functions that you've probably
been using is the printf function.
Now, printf isn't some magic function that's been
hard-coded into the computer--
rather, it's part of the C Standard Libraries, which are
a collection of functions that comes with the
C programming language.
Since printf isn't built into the computer, that means that
there was some programmer who actually went in and wrote the
printf function and bundled it with the rest of the standard
libraries so that future programmers wouldn't have to
duplicate the effort.
And we're sure thankful that that's the case, because check
out how much code it actually takes to implement printf.
>> The C Standard Libraries, of which printf is a part, are
one of the most important tools you
have in tackling CS50.
In addition to the input-output library where
printf lives, there are a bunch of other libraries that
you'll probably use over the course of CS50.
For example, the string library has functions to
compare C strings, get their length, and concatenate them.
And the math library gives you helpful
constants, like pi and e.
Now, compared to the standard libraries of other programming
languages, the C Standard Libraries are really small,
but they still have a significant amount of stuff.
And that's not to say that the Standard Libraries are the
only C libraries; there are many more out there in the
world for you to use, including the CS50 Library,
cartography libraries with encryption and decryption
functions, libraries with functions to encode and
play back video, and so on.
>> This brings us to an important aside.
One of the essential skills of engineering is knowing the
tools available in your toolbox so that you can use
them and avoid reinventing the wheel unless
you have to do so.
As programmers, libraries are one of the most important
tools we have.
When you find yourself facing a task that seems tedious or
commonplace, like finding the length of a C string, you
should ask yourself if it might be the case that
someone's already done the same thing before.
Chances are that you could find some
help within a library.
>> So in technical terms, a library is a binary file that
has been produced by linking together a collection of
object files using a tool called, you
guessed it, the linker.
Object files are those .o files that you get when you
compile C source code.
When programmers write libraries, they typically
separate their C code into two types of files:
header files and implementation files.
A header file by convention is given the .h file extension,
and it contains code that declares the library's
resources that are available for you to use.
These resources include functions, variables, structs,
type definitions, and so on, and together, they are
commonly referred to as the interface.
In fact, if you're ever wondering what's in a library,
the header files are the place to look.
>> For example, you can pull up usr/include/stdio.h and check
out everything you ever wanted to know about
the standard io library.
There's a lot of stuff in here, and it can take a bit of
time to wade through.
However, along with Google and the manpages, this is the
place to go if you're wondering how the standard I/O
library functions work.
So one key thing to note here, though, is that header files
typically do not include function definitions--
that is, the actual function implementations.
This is an illustration of an important computer science
principle called information hiding, also referred to at
times as encapsulation.
>> As the user or the client of a library, you don't need to
know about the internals of the library in
order to use it.
Remember seeing printf just a bit ago?
Had you ever seen the actual code before?
Well, I assume that you haven't, even though you might
have used printf tons of times.
All you had to know was the function declaration present
in the stdio.h header file.
Anyway, the benefit of good information hiding is that you
and the programs you write are insulated from any changes in
the code of the implementation of library functions.
If the implementation of printf changes, you don't have
to go and change all of your programs that use printf.
>> So where does implementation go?
Well, again, by convention, the implementation goes in a
file with the .c file extension.
Usually, header files and implementation files go hand
in hand, so if you've declared a bunch of new string
functions in mystring.h, then you'll define all of
those functions in mystring .c.
So then the header and implementation files are then
compiled, creating object files that are then linked
together to produce the binary library file.
The library writer will then ship the binary file along
with the header files to programmers who want to use
the library.
At this point, the .c implementation files aren't
needed, except to recompile the library from scratch, so
they're often left out.
>> To use the C library, you have to do two things.
First, you must #include the library's header files and the
source code file where you wish to use
the library's code.
This informs the compiler of all of the declarations of
functions, variables, structs, et cetera, present in the
header files so that the compiler can one, insure that
you're using the library's resources in the proper way,
and two, generate the appropriate assembly code.
Depending on where the library's header files are
located, the #include syntax varies.
If the header files are located in the system's
default include directory--
typically, usr/include--
then the angle brackets syntax is used, as in #include
.
However, if the header files are located in the same
directory as your own code, the double quote syntax is
used, as in #include "mylib.h".
>> The second thing you have to do is to link in the binary
library file when you compile your code.
This is a super important step.
Remember our earlier discussion about how header
files don't contain implementation code?
If you forget this step, you'll get errors about
undefined symbols referenced in your code.
To solve this, use the -l flag followed immediately
without a space by the name of the library.
On many systems, including the appliance, the C Standard
Libraries are automatically linked for you.
However, the math library is sometimes bundled separately
and might need to be linked with -lm.
On the appliance, we take care of this for you, and we also
link the CS50 library with -lcs50.
You can see all of this on the appliance when you run make.
>> Now you should have the knowledge you need to use
libraries in C. Have fun exploring.
My name is Nate Hardison, and this is CS50.