How to Create a Simple Makefile - Introduction to Makefiles

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody this is Paul in this example I'm going to show you how to create a simple make file so here I've got a few files open I've got this main dot CPP file that we're looking at and inside of here we've got our main function and all the main function does is it creates this message object and we're calling that object M and then our message object M calls its print message function so the information to create the message object comes from the message H file so if we take a peek at that we've got file guards here and ending here and the file guards are just protecting the body of our message H file from being included more than one time and so what we're doing here is we've got a class we're naming that class message and we're saying that message objects have this function called print message and that message does not return any value and so then if we go take a look at message CPP we can see we're defining a function from the message class print message that's the one we just saw and all it does is it prints a message to the screen and it says make file example so there's not a lot going on here but there's enough information going on here that we can kind of see what's happening when we create a make file so if we look at our make file right now it's completely empty going over to our terminal if i type LS you can see i'm in the directory that contains those four files that we just looked at so if i was to compile this here i could just simply type g plus plus main.cpp message dot cpp and message h is already included in both of these so if i push enter it will just include all the code we need and it will create an executable called a dot out so now that we've compiled that we'll do LS again and we can see our new executable called ADA out here and we can invoke it by typing dot forward slash a dot out and we can see our printed message that says make file example so if we just have a few files we can just go ahead and do this and compile everything together that's not really a big deal but if we had let's say a thousand or ten thousand files that all had to be compiled together to create this program we probably first of all wouldn't want to type a thousand filenames here in the command line to compile our program and the other thing is is that might take a really long time and so what we're really going to want to do is we'll just want to compile the pieces that are necessary to compile based on the changes that we made to our program so if we go over here to this make file the basic structure of a make file we're going to have a target and then we'll put a colon and then we're going to have dependencies and then we're going to go down one line and we'll make sure we have a tab here and that our target is the very left of the page make files or whitespace sensitive so this is all the way to the left we've got exactly one tab of space here and then here we're going to perform some sort of action so let's go ahead and leave this on the bottom here as something we can look back at as we create our make file so the make file is going to look at the top target here as its primary target and so I'm going to call mine output because that's what I'm going to name my executable and I want to recreate my executable anytime that my object files main dot o n message dot o change and so you're probably going wait a second I didn't see those files where are these dot o files coming from so we'll get to how these are created in just a second but what we're going to do when there's a change in either one of those is we're going to go ahead and just recompile both of those into a single executable that we're going to name with this minus o flag and we're just going to name this executable output so this is saying any time that main dot o or message dos change we're going to recompile the two of them together into this executable that we've named output all right so let's go ahead and instruct our make file how to create the main dot o and the message to object files so starting with the main dot o we'll create a target for that and we're going to want to create this anytime that main dot cpp changes to create this object file we're just going to say g + + - c main dot CPP so what this is saying is it's saying go ahead and compile all the code in here and then the - C flag says don't try to create an executable but instead just compile this into an object file a file that the computer can understand and by default when we use the - C flag it will keep the name of the file but instead of dot CPP it will remove the CPP and replace it with a dot o so this is how we're creating the main dot o object file so let's go ahead and create a target for message zero now so for my tower it I'll put message zero and I want to create message zero anytime that message dot CPP or message H has been modified and to create the message that object file I'll just call G plus plus we're going to do the minus C flag again saying that we don't want to create the executable we just want to compile this into an object file and we're just going to compile the message dot CPP file so this command here compiles the message CPP into a message o object file this command right here compiles the main de CPP file into a main dot o object file and then this construction here says go ahead and compile both of those object files into an executable that we have named output so let's go ahead and get rid of this that was just kind of a placeholder I think we've got the point now and then I'm going to add one more rule here and I'm going to call this one clean and this one doesn't depend on anything this is just something we can invoke from the command line so I'll just push enter we'll tab over and then here I'm just going to remove all of our object files so the star means match anything so anything that ends in the dot o extension will be removed when we invoke this clean target here and we're also going to remove output so we'll see how this is used also so we'll save our make file now going back to our terminal going to clear the screen by doing LS we can see we've got our four files plus our a dot out here so now that we've got our make file in place if I simply type the word make we can see we're creating the main dot o file here we're creating the message to file here and then we're going to go ahead and compile and link these two together and we're going to name the executable from that output so now if we type LS we can see that we've got our files here and here and we've got our executable named output so now if I do dot forward slash output we can see the results of our program which simply prints the message make file example so I'll go ahead and clear the screen and now if I try to invoke make again it doesn't do anything because it recognizes that there is nothing that's really been changed we don't need to compile this again it's already up to date if I run make clean we can see that it runs the clean command that we invoke that's this last one right here so it just ran the remove all the dot o files and remove our exit suitable so now when I type ls' all the dot o files and our executable that was named output is gone so if I type make again it goes ahead and recreates our program and if i type LS everything's back and i can run our executable once again by typing in dot for slash output and we get the output from our program that says make file example so let's say that we change our message so if we go to message cpp then we can just say ok we don't want this to say make file example anymore so instead for our message we want to say hello so we can save this file now go ahead and clear this and now when i run make you can see it only ran two of the rules here it invoked this g + + - c message cpp that's this action right here so that means it detected a change in message cpp or message h in this case it detected a change in message CPP since that's the file we changed notice that it didn't invoke this command right here because there was no changes in main dot CPP so we can see G + + - see main dot CPP is not here because it didn't need to recompile that part of our program to make our new executable so if I run dot slash output I get our new message hello notice that it did invoke this action right here which comes from this rule once this command was completed we have a new message a file so then this rule here detected that there was a change in the message to file and so it reran this command that's why we see both this line and this line being executed but we don't see this line being executed because we didn't have a change in our main dot CPP file so let's go ahead and make a change in our main dot CPP file and we can see the difference here so if I just say touch main dot CPP all that's going to do is it's not really going to change the contents of the file but it will give the main dot CPP file a new timestamp so as far as make is concerned this is considered a change to the main dot CPP file so I push enter and now if I type make again this time it detected the change in the main dot CPP file because here we say we want to invoke this rule anytime that main dot CPP changes so it detected that and then also detected that a new object file main dot o was created any time that changes it invokes this rule right here and we can see that rule right there when we run our executable we should see the hello message against since the only thing we did was update the timestamp so you can see here how this would work if we had several thousand files if we set up our make file correctly then when we recreate our executable we're only going to recompile the piece it's necessary for us to create the output correctly so anyway I think that was a pretty good introduction to make files 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: 495,435
Rating: 4.9475513 out of 5
Keywords: How to Create a Simple Makefile, makefile, Makefile, make file, gnu make, C++ makefile, What is a Makefile, How to write a makefile, How, to, Create, make, write, simple, tutorial, paul, programming, Paul Programming, Tutor, Computer Science, Makfile Structure, Simple Makefile, Begginer, C++, Sublime Text, Makefile help, makefile lesson, help, lesson, how to, what, is, makefile target, target, makfile dependency, makefile dependencies, dependency, dependencies
Id: _r7i5X0rXJk
Channel Id: undefined
Length: 9min 24sec (564 seconds)
Published: Mon May 18 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.