Compiling C programs with Multiple Files

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
everybody welcome back I have been making a lot of videos lately just life's got the best of me things have been really crazy most of my videos to date have assumed that you know how to compile your C programs but I've actually recently had a bunch of requests looking at how do we compile programs especially programs that have multiple different source files and so today that's what we're gonna do okay so let's get into it often we think of our C compiler is a magic box we take our source code we stick it into the box and out pops a executable binary but obviously it's a lot more complicated than that and hopefully in the next few minutes they don't make more sense to you so I'm gonna use clang for my examples you could also use GCC this shouldn't make any difference so let's start out with a simple program it doesn't have to be hello world but it can be in in this case I'm just gonna use hello world because it will serve our purposes and now you can see I can simply compile it on the command line in goes the C code and out comes the executable binary and this is where we get the idea of this magic black box is this program that we just ran and it produced executable code but really this happens in stages first the C code is pre process that means includes pound defines any macros these things all get resolved you basically end up with one big block of C code then that C code is actually compiled from C to assembly code the assembler then takes the assembly code and generates binary executable code in these object files that these dot o files and then the dot o files are linked together by the linker and that produces your executable binary and sometimes you'll hear people refer to a compiler as a tool chain and this is what they mean is it's not actually just one tool it's a whole bunch of tools that are working one after the other to convert this code into something useful but it happens in stages it's a bunch of different tools so in that recent example I just ran it all of these stages all together as one but it's useful to know that you can actually just run one stage or another and so you don't have to run this whole tool chain all in one shot it's useful for example to be able to see what the preprocessor is actually doing to your C code or be able to see what kind of assembly code is being generated from your C code but let's face it most programmers don't spend a lot of time looking at pre process C code or looking at assembly code and so why am I telling you this and the reason I'm telling you this is the building your projects gets a little more complicated as your projects get more complicated so let's add another C file to our example for those of you who are just beginning maybe you haven't ever done before maybe all of your programs have just been in 1c file as your projects get more complex it's really handy to be able to organize your code into modules or libraries or components whatever you want to call them but the idea is that is that you have these these pieces of your code that logically fit together they maybe they work they have a particular task that they were doing together I don't want to re-implement a linked list for every project of mine that needs one I'd prefer to have one chunk of code that I use in all of my different projects it implements my linked list functionality and if I fix a bug in one of those if I find a bug in my linked list I want to fix it once and have it fix it in all of my projects and this is going to make your code easier to read easier to understand and easier to maintain it also makes a little portable it's easier to actually take code from one project and use it in the other so I have this extra C file over here has some functions in it I want to call those functions from my main function so I can still compile these with one command I can still list them all on the command line basically we'll just list all the C files and this is gonna work it's basically going to just take all of this all of these C files and just compile them all together into one program but I can also compile each of the C files into object files independently and then in a final stage I can link all the object files into a single binary and this is actually a really common pattern that you're gonna see a lot of projects do but it may not be clear to you why so first of all this approach feels more organized to me if they're errors in one particular component or module I can deal with those one module at a time it feels it feels organized it feels modular it makes me at least feel organized and finally breaking up your code into individual files and compiling them separately can actually make compiling faster now I know for most of you you don't really think of compilation as something it takes a lot of time most student projects are pretty simple you're not used to taking more than a couple of seconds but for the nonbelievers out there go compile the Linux kernel and it could take you a half an hour it could take you an hour depends on the machine you're running on it depends on a lot of factors but as your projects become more complex compilation can actually become a factor and if you can save yourself some time that's gonna be very valuable so that's what we want to do today we want to set up a project where just the pieces that we've changed actually need to be recompiled and and so how we do that is we simply just compile each of our dot C files into a dot o file and then like I said before then we just use the linker link them all together in a binary at the end now if I change one of those C files I still have to recompile that dot seed file into a dot o file and do the real inking stage but I don't have to go back and recompile each of my dot C files so as I build up a lot of different components a lot of different modules a lot of different C files can save me a lot of time I don't have to recompile all of them so we use the - C option to say just compile the C file - the dot o file and of course if you're typing this yourself into the terminal all the time this is going to mean more commands more typing and this may seem really tedious but this is where I want to stop you and say you shouldn't be typing any compiled commands on your own you should instead be using something like make or some other build system to automate your build process so I'm assuming you'll have something like a make file and that it's gonna look something like this and you'll notice that I have targets for compiling each of my C files - dot o files and I have a final stage that does the linking and I can simplify this even more by using automatic variables and make and now anytime I type make is gonna compile my program and best of all it's only gonna compile the parts that have changed so if I've changed one of my dot C files that's gonna get recompiled but I'm not gonna have to compile the rest of the project so if any of that stuff on make was mysterious to you I do have some other make videos by all means please go look them up I'll link to them in the description so you can find them easily make is a great tool that you definitely want to know but anyway that's it for today that's how you take multiple C files and compile them into a single unified binary executable I did also mention libraries and I don't have time today to add libraries to this video but I promise in a future video we'll talk about how to make libraries and make your code even more portable than it already is and so that's all I have for you today I hope this is helpful it would help you in your next project and so until next time happy coding and I'll see you later
Info
Channel: Jacob Sorber
Views: 28,965
Rating: 4.9285712 out of 5
Keywords:
Id: 2YfM-HxQd_8
Channel Id: undefined
Length: 6min 9sec (369 seconds)
Published: Tue Mar 20 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.