C++ Header Files

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's go somewhere fun today alright let's try something new hey what's up guys my name is Luciano welcome back to my C++ series so today head of files and we're actually we're actually out started off is a great day then it started raining but I decided to get out of town because so we're here somewhere in Australia then we're going to be talking about C++ hope you guys don't start to think that header files are so boring that I felt the need to just get out of town to cover them that's just what I just felt like doing alright so header files what are they why do we need them why do they even exist in C++ a lot of other languages that you might have been used to like Java or C sharp they don't actually have header files or any kind of concept of of two different file types where we have our compilation file that we actually compile like a surplus file a translation unit and then we also have a header file which is just this weird kind of file that we always include in places and why is it even there header files are actually much more useful and they're used for much more than just to kind of declare certain declarations that you want to use in multiple CPP files and as the series kind of goes on we're just going to learn so many new concepts that really do require header files in order to work so don't dismiss them too quickly as far as the basics of surplice Bosco's header files are traditionally used to declare certain types of functions so they can be used throughout your program if you think back to my super loft compiler and linker videos i talked about how we need certain declarations to actually exist so that we know what functions and types we have available to us for example if we create function in one file and we want to use it in another file c++ isn't going to know that that function even exists when we try and compile that other file so we need a common place to kind of store just declarations not definition because remember we can only define a function one when in a common place to store declarations for functions just declarations no actual definitions no body for the functions but just a place that says hey this function exists let's take a simple example here so suppose that I have a here called log that is supposed to log something to the console it's going to take in a Const our message and it's going to simply see out that message if I then go ahead and create an additional file we're going to call this log dot CPP and then maybe that additional file has something that initializes our log and decides to log something to the console we are going to get an error because this log function doesn't actually exist in this file this file doesn't know about the log function of course back in main here it is the log function exists fine and we can actually replace our hello world logging to use that log function and if I try and compile my program by hitting ctrl f7 you'll see that it works fine and we don't get any errors however back in log if I try and compile this we get an error because of course as far as this file is concerned log doesn't exist so what does log CBP actually need in order to not errors how do we tell it that that log function does actually exist but it's just defined elsewhere that is where a function declaration comes in if we go back to our code here I need to simply declare that log does exist if we go back to main and we take a look at this actual signature you'll see that log is a function that returns void and takes in one parameter which is a constant are pointer and some child is chasing the kangaroos that's funny so this is the function signature we can literally just copy this go back to log dot cpp paste this in and just close it with a semicolon the fact that this function doesn't actually have a body means that it is the Declaration of the function we haven't defined what the function actually is what the function actually does we've just said hey there's a function called log the return void and takes in a concentrator we've that that function exists you can see that our intelligence error has gone away and if I hit ctrl f7 we do compile and if I right click on hello world and click build to actually build my program you can see that it links fine as well because it's fine it's found that log function all right fantastic so great we've found a way to actually tell our log does it appeared that that that log function exists somewhere but what if we make another file what if I what if I make some other file that needs to use this log function does that mean that I also have to copy and paste this void log declaration everywhere I go the answer is yes you actually do need to do that however there is a way to make that how to file write what our header file what are pedophiles traditionally I should say because really this is syphilis what you can you can do anything with anything but how to file the usually files that get included into CBP files so basically what we do is leave copy and paste the content of header files into CPP files and we do that via the hash include pre-press as the directors so hash include' has the ability to copy and paste files into other files and that is exactly what it seems that we need to do here we need to copy and paste this log declaration into every file that needs to use that log function so let's have a go at making a header file I'm going to right click on header files now note these folders here are called filters they're not actual real folders I can create a header file under source files there's going to be a few videos on how a visual studio works in the future but for now just know these are kind of fake folders it doesn't matter which one you right-click on to create your header file I'm going to create it on the header files because that makes sense but it doesn't really matter I'm going to make a header file called log door age you'll notice that it straight away inserted some code for me automatically if it says hash fragment once we'll talk about that in just a second so here I'm going to put in my declaration I'm going to cut my declaration from blog dot CPP file and put it into here into my header file so now the idea is this this header file log dot H I can include everywhere where I want to be able to use log and of course it's going to do for me what I didn't want to do manually and that is copy and paste this everywhere throughout every file that requires it I don't want to have to do that copying and pasting myself so I've basically found a way to kind of make it look a bit tidy and automated to some extent so back in logged on CPP you can see we get an error now because we're not actually declaring that function however if I type in hash include' log dot H check that out we don't get an error and our file compile fantastic what we can also do is actually included into main dot CPP now mando CBP contains the actual definitions to the function so it doesn't really require it we can call log anyway but just so you know it's not going to hurt for us to actually include log dot H into here and compile that's not going to be a problem all right so back in log dot CPP you can see we've defined this fantastic function called net log however no one actually knows about it except for logo CBP if I want to be able to call it from main I'm going to need that declaration if I call in this log over here it's going to give me an error because that's not declared anyway so let's go ahead and add that function signature in it log into our log header file just like that fantastic make sure that it matches the function signature that you've actually declared in your in your CBP file so now everything seems well in the world I'm also going to go ahead and cut and paste this log definition into my log file because that makes a lot more sense now I get an error here telling me see out is not found that's okay I can just include iostream awesome so back in Maine if I run my program we can see that we managed to initialize our log and then log hello world to the console fantastic all right brilliant so let's go back to that header file and take a look at what that pragma wants statement actually want so here we have a statement that Visual Studio has seemingly inserted for us called hash fragment one what is this so firstly anything that begins with a hash is known as a preprocessor command or a preprocessor directive it means that this is going to be evaluated by disable for preprocessor before this file actually gets compiled pragma is essentially an instruction that is sent to the compiler or to the preprocessor and what pragma wants essentially means is that only include this file once so pragma wants is something called a header guide what it does is it prevents us from including a single header file multiple times into a single translation unit now I chose my words there very carefully because you have to understand that it does not prevent us from including our header file into multiple places in our program just into a single translation unit so a single CPP file the reason is that if we accidentally include a file multiple times into a single translation unit we can get duplicate errors because we'll be copying and pasting that entire header file multiple time one of the best ways to demonstrate this is if we were to create a struct for example if I create a struct here called player I can leave it empty doesn't really matter if I were to include this file twice into a single translation unit with no header gods it would actually include that file twice which means that I would have two structures with exactly the same name player we can take a look at this example by commenting out our pragma ones and then back and log I will include log dot H twice if I try and compile my file you can see that we get a player structure redefinition error because we're redefining that struct player we can only define one structure called player structure need unique name so again you say Cherno why would I do this I'm not as the programmer I'm not as dumb as you think why would I include a file twice well young one this comes back to how include works remember how include works is it copies and pastes filed into other files meaning you can create a bit of a change of includes so you could have a header file called player which includes blog then players included in some other file and then maybe that third file contains every include so if I create another header file here called common common is just going to contain some common includes for example it will include logs if I go to log dot H and make sure that pragma once is commented out and into logged or CBP I include logged or H and Commons or H if I compile my file guess what I still get that error because that struct player is being redefined if we were to resolve what the preprocessor actually did you could see that it actually has included logged wine however back in log duration we get rid of that comment and try and compile our file win again an error because it recognizes the log has already been included and it does not include it's fine now there is one other way that we could add a head of garden I actually kind of like it for teaching purposes because it makes a little bit more sense than some kind of fragment one although broken one filled with a lot cleaner the traditional way to add header guides is actually very if def so what we can do is we can actually type in an if and def and then we can get it to check some kind of symbol for example log h we're going to define log age and then at the very end of our file we're going to type an ended so what this is going to do is first of all check to see if a symbol called log age is actually defined if it's not defined it's going to go ahead and include the following code in compilation if this was defined then none of this will be included it will be all disabled so once we do pass this initial check we're going to define log age which basically means that next time we go through this code it will be defined so it will not be repeated a really easy way to demonstrate this is just a copy and paste this entire file into our logs EVP file and then if we take a look at this I also comment out log the edge and comment age so you can see over here that the first time this is all fine it includes the file and everything's okay and then the second time it's all grayed out because logs page has already been defined so this kind of how-to guide is something that we'll use a lot in the past however now we have this new pre-processing statement called chroma one and so we mostly use that it doesn't matter which one you use to some extent although primer one is lot cleaner so it's something that I personally use and it's something that most people do use in the industry pretty much every compiler now supports pragma one so there isn't it's not like a visual studio only thing GCC clang em SBC they all support pragma one so don't be afraid to use it that being said if you do find legacy code or just code the people of rhythmicity different styles you may encounter this if and des had a guide so just be wary of what it is but again I would never write EF and F I would always use program one last thing I want to show you is with header guides we have this kind of discrepancy between include statements some include statements use quotes some include statements use angular brackets what is the deal deals actually pretty simple they mean two different things when we compile our program we have the ability to tell the compiler of certain include part these are basically part of two folders in our computer that contained include files if the include file that we want to include is inside one of these folders we can use angular brackets to tell the compiler to search our include past folders where as quotes on the other hand are usually used to include files that are relative to the current file so for example if I had a file called logger edge that was up one directory from this current log dot CPP file I could use flash to go back and that would actually go back to a directory because this is relative to this current file whereas with angular brackets there never relative to this current file they just have to be in one of the include directories we'll talk more about setting up include directories and all that in the future I want to complicate things too much but that's basically the gist of how that's why now quotes can also be used to specify files that are relative to include directories that are pass through our compiler so you can actually use quotes anywhere I can replace this iostream to be in quotes and it would totally work so angular brackets only for compiler include past quotes for everything however I usually like to use them just for Elsa fun so you might as well use the angular brackets for something all right final thing for today one other thing that you might notice is this iostream doesn't actually look like a file because it doesn't contain any extension it's just called iostream what's up with that well it actually is a file it's just that it doesn't have an extension this is something that whoever wrote this about the standard library decided to do today I separate out these standard library header files mz4 plus standard location header files that are in the c standard library will often have a dot h extension on the end however these are flat files do not so this is just one other way you can differentiate between what is in the c standard library and a c++ standard library whether on it has that Diet extension iostream is a file just like anything else in fact in Visual Studio if we right click on it and hit open documents you can see that it takes us through this iostream header file and if we right click on that on this tab here and hit open containing folder you'll see where it's actually located on our computer inside this directory and here it is that iOS stream pattern files alright so that's it Edda files pretty easy we're going to be using this extensively in this series so there'll be plenty of examples to follow you'll see how I use them you'll see how you should be using them but you should now understand how they work and what they're useful so anyway I'm going to get out of here this getting really really cold but I hope you guys enjoyed this video let me know what you think of this whole like me going out and doing stuff in in I mean this is way more fun I love traveling this is way more fun for me to do than just sitting at home especially on a Saturday afternoon even though the weather today was pretty bad so I probably should have just at home but anyway I'm here now let me know what you think of this you can follow me on Twitter and Instagram I'm going to be posting some of the photos I took today on Instagram so be sure to follow me there if you really enjoy the series and you want to see more of these videos you can support me on patreon the Concours that setsuna and I will see you guys in the next video goodbye [Music]
Info
Channel: The Cherno
Views: 447,264
Rating: undefined out of 5
Keywords: thecherno, the, cherno, project, thechernoproject, c++, how c++ works, learn c++, c++ tutorial, game, programming, development, engine, game programming, game development, how to make a game, tutorial, source, code, complete, game engine, how to make a game engine, opengl, glfw, C (Programming Language)
Id: 9RJTQmK0YPI
Channel Id: undefined
Length: 15min 10sec (910 seconds)
Published: Sun May 07 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.