1 00:00:06,692 --> 00:00:09,470 NATE HARDISON: In programming, a library is a collection of 2 00:00:09,470 --> 00:00:11,380 related prewritten code. 3 00:00:11,380 --> 00:00:14,350 Libraries are how we, as programmers, share common and 4 00:00:14,350 --> 00:00:16,930 helpful code with each other, with the different programs we 5 00:00:16,930 --> 00:00:19,470 write, and even with the different processes running at 6 00:00:19,470 --> 00:00:21,380 the same time on our computers. 7 00:00:21,380 --> 00:00:23,000 >> Let's explore a bit. 8 00:00:23,000 --> 00:00:25,070 One of the more common functions that you've probably 9 00:00:25,070 --> 00:00:27,500 been using is the printf function. 10 00:00:27,500 --> 00:00:29,820 Now, printf isn't some magic function that's been 11 00:00:29,820 --> 00:00:31,670 hard-coded into the computer-- 12 00:00:31,670 --> 00:00:34,320 rather, it's part of the C Standard Libraries, which are 13 00:00:34,320 --> 00:00:36,080 a collection of functions that comes with the 14 00:00:36,080 --> 00:00:38,050 C programming language. 15 00:00:38,050 --> 00:00:40,920 Since printf isn't built into the computer, that means that 16 00:00:40,920 --> 00:00:43,520 there was some programmer who actually went in and wrote the 17 00:00:43,520 --> 00:00:46,310 printf function and bundled it with the rest of the standard 18 00:00:46,310 --> 00:00:48,880 libraries so that future programmers wouldn't have to 19 00:00:48,880 --> 00:00:50,310 duplicate the effort. 20 00:00:50,310 --> 00:00:52,620 And we're sure thankful that that's the case, because check 21 00:00:52,620 --> 00:00:54,710 out how much code it actually takes to implement printf. 22 00:00:59,590 --> 00:01:02,860 >> The C Standard Libraries, of which printf is a part, are 23 00:01:02,860 --> 00:01:04,480 one of the most important tools you 24 00:01:04,480 --> 00:01:06,770 have in tackling CS50. 25 00:01:06,770 --> 00:01:08,780 In addition to the input-output library where 26 00:01:08,780 --> 00:01:11,370 printf lives, there are a bunch of other libraries that 27 00:01:11,370 --> 00:01:14,230 you'll probably use over the course of CS50. 28 00:01:14,230 --> 00:01:16,730 For example, the string library has functions to 29 00:01:16,730 --> 00:01:20,960 compare C strings, get their length, and concatenate them. 30 00:01:20,960 --> 00:01:22,410 And the math library gives you helpful 31 00:01:22,410 --> 00:01:25,062 constants, like pi and e. 32 00:01:25,062 --> 00:01:27,650 Now, compared to the standard libraries of other programming 33 00:01:27,650 --> 00:01:31,190 languages, the C Standard Libraries are really small, 34 00:01:31,190 --> 00:01:34,060 but they still have a significant amount of stuff. 35 00:01:34,060 --> 00:01:36,370 And that's not to say that the Standard Libraries are the 36 00:01:36,370 --> 00:01:39,020 only C libraries; there are many more out there in the 37 00:01:39,020 --> 00:01:42,460 world for you to use, including the CS50 Library, 38 00:01:42,460 --> 00:01:45,150 cartography libraries with encryption and decryption 39 00:01:45,150 --> 00:01:48,000 functions, libraries with functions to encode and 40 00:01:48,000 --> 00:01:50,970 play back video, and so on. 41 00:01:50,970 --> 00:01:53,190 >> This brings us to an important aside. 42 00:01:53,190 --> 00:01:55,500 One of the essential skills of engineering is knowing the 43 00:01:55,500 --> 00:01:58,090 tools available in your toolbox so that you can use 44 00:01:58,090 --> 00:01:59,850 them and avoid reinventing the wheel unless 45 00:01:59,850 --> 00:02:01,510 you have to do so. 46 00:02:01,510 --> 00:02:04,120 As programmers, libraries are one of the most important 47 00:02:04,120 --> 00:02:05,520 tools we have. 48 00:02:05,520 --> 00:02:08,520 When you find yourself facing a task that seems tedious or 49 00:02:08,520 --> 00:02:11,940 commonplace, like finding the length of a C string, you 50 00:02:11,940 --> 00:02:14,020 should ask yourself if it might be the case that 51 00:02:14,020 --> 00:02:16,520 someone's already done the same thing before. 52 00:02:16,520 --> 00:02:17,760 Chances are that you could find some 53 00:02:17,760 --> 00:02:20,110 help within a library. 54 00:02:20,110 --> 00:02:23,260 >> So in technical terms, a library is a binary file that 55 00:02:23,260 --> 00:02:26,210 has been produced by linking together a collection of 56 00:02:26,210 --> 00:02:28,880 object files using a tool called, you 57 00:02:28,880 --> 00:02:31,010 guessed it, the linker. 58 00:02:31,010 --> 00:02:33,830 Object files are those .o files that you get when you 59 00:02:33,830 --> 00:02:35,860 compile C source code. 60 00:02:35,860 --> 00:02:38,420 When programmers write libraries, they typically 61 00:02:38,420 --> 00:02:41,560 separate their C code into two types of files: 62 00:02:41,560 --> 00:02:44,880 header files and implementation files. 63 00:02:44,880 --> 00:02:49,820 A header file by convention is given the .h file extension, 64 00:02:49,820 --> 00:02:51,940 and it contains code that declares the library's 65 00:02:51,940 --> 00:02:55,120 resources that are available for you to use. 66 00:02:55,120 --> 00:02:58,830 These resources include functions, variables, structs, 67 00:02:58,830 --> 00:03:01,980 type definitions, and so on, and together, they are 68 00:03:01,980 --> 00:03:04,620 commonly referred to as the interface. 69 00:03:04,620 --> 00:03:07,280 In fact, if you're ever wondering what's in a library, 70 00:03:07,280 --> 00:03:09,580 the header files are the place to look. 71 00:03:09,580 --> 00:03:14,030 >> For example, you can pull up usr/include/stdio.h and check 72 00:03:14,030 --> 00:03:16,210 out everything you ever wanted to know about 73 00:03:16,210 --> 00:03:18,570 the standard io library. 74 00:03:18,570 --> 00:03:20,610 There's a lot of stuff in here, and it can take a bit of 75 00:03:20,610 --> 00:03:22,420 time to wade through. 76 00:03:22,420 --> 00:03:25,840 However, along with Google and the manpages, this is the 77 00:03:25,840 --> 00:03:28,060 place to go if you're wondering how the standard I/O 78 00:03:28,060 --> 00:03:30,460 library functions work. 79 00:03:30,460 --> 00:03:33,910 So one key thing to note here, though, is that header files 80 00:03:33,910 --> 00:03:36,740 typically do not include function definitions-- 81 00:03:36,740 --> 00:03:40,020 that is, the actual function implementations. 82 00:03:40,020 --> 00:03:42,470 This is an illustration of an important computer science 83 00:03:42,470 --> 00:03:45,700 principle called information hiding, also referred to at 84 00:03:45,700 --> 00:03:48,960 times as encapsulation. 85 00:03:48,960 --> 00:03:52,630 >> As the user or the client of a library, you don't need to 86 00:03:52,630 --> 00:03:54,230 know about the internals of the library in 87 00:03:54,230 --> 00:03:55,380 order to use it. 88 00:03:55,380 --> 00:03:57,800 Remember seeing printf just a bit ago? 89 00:03:57,800 --> 00:04:00,275 Had you ever seen the actual code before? 90 00:04:00,275 --> 00:04:02,535 Well, I assume that you haven't, even though you might 91 00:04:02,535 --> 00:04:05,090 have used printf tons of times. 92 00:04:05,090 --> 00:04:08,110 All you had to know was the function declaration present 93 00:04:08,110 --> 00:04:10,940 in the stdio.h header file. 94 00:04:10,940 --> 00:04:14,010 Anyway, the benefit of good information hiding is that you 95 00:04:14,010 --> 00:04:16,820 and the programs you write are insulated from any changes in 96 00:04:16,820 --> 00:04:20,209 the code of the implementation of library functions. 97 00:04:20,209 --> 00:04:22,880 If the implementation of printf changes, you don't have 98 00:04:22,880 --> 00:04:26,310 to go and change all of your programs that use printf. 99 00:04:26,310 --> 00:04:28,450 >> So where does implementation go? 100 00:04:28,450 --> 00:04:31,020 Well, again, by convention, the implementation goes in a 101 00:04:31,020 --> 00:04:34,310 file with the .c file extension. 102 00:04:34,310 --> 00:04:36,820 Usually, header files and implementation files go hand 103 00:04:36,820 --> 00:04:39,380 in hand, so if you've declared a bunch of new string 104 00:04:39,380 --> 00:04:42,950 functions in mystring.h, then you'll define all of 105 00:04:42,950 --> 00:04:46,960 those functions in mystring .c. 106 00:04:46,960 --> 00:04:49,230 So then the header and implementation files are then 107 00:04:49,230 --> 00:04:52,230 compiled, creating object files that are then linked 108 00:04:52,230 --> 00:04:55,360 together to produce the binary library file. 109 00:04:55,360 --> 00:04:58,520 The library writer will then ship the binary file along 110 00:04:58,520 --> 00:05:00,720 with the header files to programmers who want to use 111 00:05:00,720 --> 00:05:02,280 the library. 112 00:05:02,280 --> 00:05:04,640 At this point, the .c implementation files aren't 113 00:05:04,640 --> 00:05:07,840 needed, except to recompile the library from scratch, so 114 00:05:07,840 --> 00:05:10,270 they're often left out. 115 00:05:10,270 --> 00:05:13,670 >> To use the C library, you have to do two things. 116 00:05:13,670 --> 00:05:16,890 First, you must #include the library's header files and the 117 00:05:16,890 --> 00:05:18,700 source code file where you wish to use 118 00:05:18,700 --> 00:05:20,530 the library's code. 119 00:05:20,530 --> 00:05:23,200 This informs the compiler of all of the declarations of 120 00:05:23,200 --> 00:05:26,890 functions, variables, structs, et cetera, present in the 121 00:05:26,890 --> 00:05:30,510 header files so that the compiler can one, insure that 122 00:05:30,510 --> 00:05:33,280 you're using the library's resources in the proper way, 123 00:05:33,280 --> 00:05:37,120 and two, generate the appropriate assembly code. 124 00:05:37,120 --> 00:05:38,910 Depending on where the library's header files are 125 00:05:38,910 --> 00:05:42,720 located, the #include syntax varies. 126 00:05:42,720 --> 00:05:44,880 If the header files are located in the system's 127 00:05:44,880 --> 00:05:46,690 default include directory-- 128 00:05:46,690 --> 00:05:48,780 typically, usr/include-- 129 00:05:48,780 --> 00:05:52,200 then the angle brackets syntax is used, as in #include 130 00:05:52,200 --> 00:05:53,450 . 131 00:05:57,440 --> 00:05:59,540 However, if the header files are located in the same 132 00:05:59,540 --> 00:06:02,960 directory as your own code, the double quote syntax is 133 00:06:02,960 --> 00:06:04,870 used, as in #include "mylib.h". 134 00:06:08,630 --> 00:06:11,220 >> The second thing you have to do is to link in the binary 135 00:06:11,220 --> 00:06:13,760 library file when you compile your code. 136 00:06:13,760 --> 00:06:15,810 This is a super important step. 137 00:06:15,810 --> 00:06:17,540 Remember our earlier discussion about how header 138 00:06:17,540 --> 00:06:20,200 files don't contain implementation code? 139 00:06:20,200 --> 00:06:22,200 If you forget this step, you'll get errors about 140 00:06:22,200 --> 00:06:24,990 undefined symbols referenced in your code. 141 00:06:24,990 --> 00:06:29,580 To solve this, use the -l flag followed immediately 142 00:06:29,580 --> 00:06:32,320 without a space by the name of the library. 143 00:06:32,320 --> 00:06:35,410 On many systems, including the appliance, the C Standard 144 00:06:35,410 --> 00:06:38,350 Libraries are automatically linked for you. 145 00:06:38,350 --> 00:06:41,700 However, the math library is sometimes bundled separately 146 00:06:41,700 --> 00:06:44,600 and might need to be linked with -lm. 147 00:06:44,600 --> 00:06:47,760 On the appliance, we take care of this for you, and we also 148 00:06:47,760 --> 00:06:52,170 link the CS50 library with -lcs50. 149 00:06:52,170 --> 00:06:54,540 You can see all of this on the appliance when you run make. 150 00:06:57,620 --> 00:06:59,130 >> Now you should have the knowledge you need to use 151 00:06:59,130 --> 00:07:02,150 libraries in C. Have fun exploring. 152 00:07:02,150 --> 00:07:05,880 My name is Nate Hardison, and this is CS50.