How to Include File Guards - Why to Include Fileguards in C or C++ Header Files

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody this is Paul in this video I'm going to be explaining how and why to use file guards in C or C++ programs so right now I've just got the iostream library included so that I can print some information to the screen I am including two files I'm including engine H and car dot H I'm using the standard namespace so that I don't have to type STD colon colon here for my C out and then I've just got a main program and all it does is print the words broom broom to the screen if we wanted to use this engine file in this car file inside of our main program here we could just include those two files like this and then any information that's within the engine or the car file we could use within our main function here so if we look at engine dot H basically all we're doing is we're just describing a class here so we're creating a class and then what we would do is we would put the code that describes what an engine is inside of these curly braces here same with the car dot H file we've got a class here and inside of the curly braces we would put all the code in here that describes what a car is so it would make sense for us if we were creating a car class we might need the car class to be able to have information about what an engine is so it would make sense to include the engine class as a header within the car class so here I'm just going to include the engine file so that when we write our car code our car knows everything about what's in the engine file and then we can put engines within our car so that makes sense cars need to know what engines are and then in our main program maybe we also want to know what an engine is we'll do some work with both cars and engines here so that's fine but what happens when we actually try to run this here I'm actually in the directory where I have those three files you can see car dot H engine H and main dot CPP live here and I'm just going to go ahead and try to compile this and you'll see that we get an error so compiling main dot CPP what it's going to do is it's going to go through here it's going to include engine H and car dot H so those will automatically get included as I compile this and when I push enter you can see I get this error that I'm redefining the engine class to show you why we're getting this error let's go ahead and walk through this so as we compile main dot CPP here it's going to include the engine file so it's going to jump to the engine file at that point and it's going to read all this information it's going to add that to the compiling process once it's done with this we're back here in our main program and the next thing it wants to do is it wants to include card IH so at this point it jumps over to the card H file and the first thing the car da h file says to do is it says include the engine by H file so then and it jumps back to the engine H file and it tries to read this again and that's where we're getting that second redefinition of the engine class so that matches up with our error we're having a redefinition of the engine class it's trying to read that engine h file two times so we can add file guards to our engine that h file to prevent this from happening so to add a file guard in the engine h file will say pound if n d EF so what this means is if and then the n stands for naught and then the DEF stands for defined so if not defined well if what's not defined we could put coffee we could put tree we could put Paul we could put whatever we want here but since this is the engine file we'll just call this engine and then we'll go down to the next line and we'll say define engine so what we're saying here is we're saying if engine is not defined go ahead and define engine and then from this point on everything down here is going to be considered this engine thing that we're defining and then once it's done we'll do a pound and if and then that will stop the definition of engine we'll put a comment after this end if and say that we're ending the definition of engine so if that doesn't make sense let me just walk through this step-by-step and I'll show you what's going to happen now so now when we try to compile main dot CPP as we're reading through this file we're going to get to include engine dot H then we're going to jump to this engine dot H file and then we're going to read if engine is not defined go ahead and define engine and so the first time through engine has not yet been defined so it's going to define engine and it's going to say okay I'm reading through all this and it's saying this is engine this code right here we're defining as engine and then it gets to this point and it says that's the end of the engine definition and then we jump back to our main program then it says okay go ahead and include car dot H next jumps to car dot H first instruction and car dot H it says you include Engine H so then it jumps to engine H now goes back to this line again that says if engine is not defined and then it goes oh wait a second engine has been defined the first time through so then it skips this section because engine has been defined once already and then it jumps back to car dot H goes ahead and defines the car dot H file then it jumps back to the main file and it continues on down the main file and continues to compile the rest of the code by adding these file guards we have ensured that we're only going to define the engine not H file one time so now making sure everything is saved here we'll go ahead and clear the screen and then we'll try this once again ge+ plus main dot cpp and this time it compiles so now we have this new executable here called a dot out that was created by compiling the main dot CPP file and now when we run it out we see the words roomed room for this particular example we didn't need to include file guards for car dot H year but it's always good practice to go ahead and include file guards and everything so that we don't try to include a header file twice so once again to include a file guard for this file we could just say pound if not defined and this time we'll just choose the name car and then we'll say pound define and we'll define what car is and we want car to be the car dot H file so at the end of everything in the car dot H file will say pound end if and then we'll just put a comment here that says we're ending the file guards for the car file we'll save that make sure everything else is saved and then we'll just double check to make sure everything went well g plus plus main.cpp and everything compiles fine again as we run the program we get the broom groom one more time so anyway that's how and why you would need to include file guards in your header files so thank you guys for watching have an excellent day and if you haven't already don't forget to subscribe
Info
Channel: Paul Programming
Views: 42,598
Rating: 4.9654322 out of 5
Keywords: How to Include Header Guards, Why to Include Header Guards, headerguards, header guards, how to include fileguards, why to include fileguards, Fileguards, file guards, What, is, file, guard, guards, fileguard, file guard, header guard, header, #define, #infndef, #endif, define, ifndef, endif, paul programming, paul, programming, tutorial, computer, code, C++, Sublime, Sublime Text, video tutorial, C++ header, C Header, Header File, .h file, .h, .h guard, .h fileguard, .h file guard
Id: RU5JUHAiR18
Channel Id: undefined
Length: 6min 19sec (379 seconds)
Published: Sat May 09 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.