Header files and libraries (Kevin Lynch)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
we've been making use of the CIE standard input/output library by including at the beginning of our program this statement pound include standard IOH now what this does is it copies into our CE file function prototypes for scanf and printf and related functions so that our program will compile successfully to an object code and then from there in the last stage the linker is going to link in with our object code the object code for printf and scanf so that we can create a successful executable now often you want to create your own libraries for functions that you'll be using over and over again and to do that you create a header file dot H file like at standard IOH as well as a C code that contains the functions or the library could be already a pre compiled object code so let's say for example that you're often going to want to be able to calculate the volume of a sphere given the radius of that sphere so you decide to create a library called rad to volume so let's take a look at what a header file might look like so here's a header file that does two things basically it defines pi or we call it my pi so this is a constant that we define 3.14 and now we've made that available to any program any C file that includes rad to volume dot H just like we include standard input/output that age and we've also provided a function prototype here called radius to volume it takes in a double which is the radius of the sphere and it's going to return another double and that's going to be the volume of the sphere the only other thing that's going on here in this header file is this what's called an include guard so this is a preprocessor command here that says if there's never been defined a constant called rad to volume H then go ahead and define it and this is the ending of that if statement on the other hand if it's already been defined we're going to skip everything between if defined to the end if so why is that it's because when we compile this code we want to make sure that we don't accidentally include rad to volume dot H twice during the same compilation to object code otherwise we're going to have two definitions for example of the function prototype and that would cause Kazakh compiler error so this is our header file gives to us a constant and a function prototype for the function radius two-volume now let's take a look at the C code that would go along with that in the library and our C code here is going to include a math header file so this gives us access to the function power power and it's also going to include include this header file itself so that it's got its own function prototype and it has access to my PI now within this code there's two functions one is called cuber and one is called radius to volume now radius the volume is the public function that we want to make available to other codes that are going to use this library and that's why radius to volume has a function prototype up here in the header file on the other hand there's this other function called cuber it's only job is to cube a double that's passed to it and that is just meant for internal use and because of that we only define it within this C file itself we don't make it available to external codes and we see that because there's no function prototype up here and the public header file okay so now this is our C library header file and the C code that goes with it so I'm going to define a function that uses them just to demonstrate it so here's main dot C and main dot C is going to include our standard input/output library but it's also going to include the rad two-volume header okay and then within the function all it does is it uses radius the volume to calculate the volume and then it prints out both my PI here as well as the volume at the end so to compile this we could say GCC main dot C as well as red to volume Z and I'll call the output main compiles we run it and it just prints out the value of pi that we got from the header file as well as the volume that it calculated using the helper libraries function rad - volume radius - volume sorry okay so this is a simple example of creating your own library now let's look at a more generalized example of this so here's a project that it's going to have two helper libraries we call them helper 1 dot C and helper to see each of them has their own helper fot helper header files and we also have the main function over here now the way you see the dependencies here is showing which files are including which other files so helper 1 dot C is including its helper 1 dot H file which actually includes another file because an include file a header file can include another header file so helper 1 dot H includes this general H similarly a helper to dot C includes helper 2 dot H which also includes general H and then main dot C over here uses both helper 1 and helper 2 header files for those two libraries but it's also making use of this general dot H header file ok so this is an overview of a project 3 C files 3 header files and their dependencies and now let's see what happens when we build this project so each of the C files is going to compile independently into its own object code so for example here's main dot C as you can see it includes helper 1 dot H helper - H as well as general H this all gets compiled and turned into main dot 0 here helper to dot C includes its to include files compiles to helper 2.0 and helper 1 dot see here compiles to helper 1.0 so we've got our two library object codes now and our main object code and in the end these are linked to make the final program my Prague so to summarize here are things that you would not include in a library header file you would not include private function prototypes constant or macros so for instance if only one c file needs to use that function if only one c file needs to use that constant or macro then you would just define that in the c file itself you wouldn't put it in the header file because you don't want to make them available to external programs in our earlier example that would be the cuber function for example another thing you don't want to decide or define in a library header file is a line of the form int global now if your program sees int global it's going to define an allocate space for a variable called global if during the compilation of a single or the building of a single project you have two different C files compiled to object files and each of them has included this header file they've both defined their own global variable called global and now when you link them together at that stage there's going to be an error because we're going to see there's two global variables that have been defined so you don't want to put that in a header file you want to put these global variables in the C file itself here are things that you do want to put in a library header file you always want to have an include guard this makes sure that you don't accidentally include the same header twice during the compilation to the same object code you might want to have other include files other pound includes for perhaps data types or other things that your header file needs you might want to include other header files you might want to define a new data type in a header file you want to give public function prototypes so other C files can make use of the functions in your library also constants and macros that you want to make publicly available and finally you could have a command you don't want to have int global which actually allocates the space in the header file but in the header file you could have a command of the form extern int'l and what this extern means is that you're not allocating space here you're just saying hey there is a variable of the name global that I want you to have access to so you're declaring it but you're not defining or allocating the space for it now if your SI helper file uses the function global and allocates that Speight we're sorry these are the variable global and allocates that space with int global by putting this in the header file you make that variable available to outside functions outside C files
Info
Channel: Northwestern Robotics
Views: 64,253
Rating: 4.9047618 out of 5
Keywords:
Id: 5UMHbzZGQuE
Channel Id: undefined
Length: 9min 32sec (572 seconds)
Published: Mon Dec 07 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.