- 5/28/2025
Hey there, coding enthusiasts! Ready to master compiling, linking, and executing programs on Linux? In this fun and approachable video, we dive into the nuts and bolts of turning your code into runnable programs. We'll create a simple C++ program, compile it with G++, and explore object files, linking, and execute permissions. Plus, we'll mix in C and assembly code, tackle name mangling, and build a bash script to automate it all! Perfect for beginners or anyone curious about Linux programming, this guide is packed with practical tips and real-world examples. Don't miss out?subscribe for more coding adventures, leave a comment with your thoughts, and let's geek out together! Scan the QR code to visit my site for more tutorials. #LinuxProgramming #CodingForBeginners #CPlusPlus
Introduction to Compiling, Linking, and Executing 00:00:00
Overview of the Process 00:00:05
Writing a Simple C++ Program 00:00:28
Compiling with G++ 00:02:01
Understanding Executable Files 00:03:04
Executing a Program 00:04:21
Handling Execute Permissions 00:05:13
Creating a Compile Script 00:07:00
Using Shebang for Scripts 00:08:43
Specifying Output Files 00:10:53
Breaking Down Compilation Steps 00:12:26
Compiling to Object Files 00:13:17
Linking Object Files 00:15:24
Handling Multiple Source Files 00:18:39
Mixing C and C++ with Name Mangling 00:22:52
Enhancing Script with Error Handling 00:24:29
Adding Assembly Source File 00:31:16
Using Bash Variables for Flags 00:41:53
Organizing Compiler Flags 00:44:41
Final Script Enhancements 00:46:36
Conclusion and Call to Action 00:47:35
Thanks for watching!
Find us on other social media here:
- https://www.NeuralLantern.com/social
Please help support us!
- Subscribing + Sharing on Social Media
- Leaving a comment or suggestion
- Subscribing to our Blog
- Watching the main "pinned" video of this channel for offers and extras
Introduction to Compiling, Linking, and Executing 00:00:00
Overview of the Process 00:00:05
Writing a Simple C++ Program 00:00:28
Compiling with G++ 00:02:01
Understanding Executable Files 00:03:04
Executing a Program 00:04:21
Handling Execute Permissions 00:05:13
Creating a Compile Script 00:07:00
Using Shebang for Scripts 00:08:43
Specifying Output Files 00:10:53
Breaking Down Compilation Steps 00:12:26
Compiling to Object Files 00:13:17
Linking Object Files 00:15:24
Handling Multiple Source Files 00:18:39
Mixing C and C++ with Name Mangling 00:22:52
Enhancing Script with Error Handling 00:24:29
Adding Assembly Source File 00:31:16
Using Bash Variables for Flags 00:41:53
Organizing Compiler Flags 00:44:41
Final Script Enhancements 00:46:36
Conclusion and Call to Action 00:47:35
Thanks for watching!
Find us on other social media here:
- https://www.NeuralLantern.com/social
Please help support us!
- Subscribing + Sharing on Social Media
- Leaving a comment or suggestion
- Subscribing to our Blog
- Watching the main "pinned" video of this channel for offers and extras
Category
🤖
TechTranscript
00:00Hey there, let's talk about compiling, linking, and executing.
00:05This video is going to focus on Linux, but I think it'll be useful for all other operating
00:09systems that you might be working with at home.
00:11I'm just going to basically explain like what does it mean to compile?
00:14What does it mean to link a program?
00:17What does it mean to execute a program?
00:18If you don't know that, then I think this video might be for you.
00:24Okay, so for starters, imagine you have written a program.
00:32I'm going to write the program here real fast.
00:35I'm going to say, let's do an empty file.
00:37We'll call it, let's see, I got my answer key up here.
00:40We'll call it first.cpp.
00:43First.cpp.
00:44So I'm just like making a first piece of source code and I'm editing it in my little editor.
00:50Notice how I'm not calling this the main.cpp program because a lot of beginners, they
00:55always do main.
00:56I want you to know that you don't actually need a source code file called main.cpp.
01:04Even if you're writing a C++ program, all you really need is just one instance of the
01:09function main.
01:11Beyond that also, you only need an instance of main if you're writing a hybrid program
01:16or some kind of program that is in pure C++ or that has the GCC libraries.
01:21GCC libraries call on the main function as your entry point.
01:24But if you're writing pure assembly, the entry point is actually underscore start as a label.
01:29We're not really going to do, this is not an assembly tutorial.
01:32I just wanted you to know real fast.
01:34Okay, so I'm going to do like IO stream and then I'm going to say std cout and I'm just
01:41going to print, you know, hello from first.cpp.
01:46std endl.
01:49Okay, so we kind of have like a hello world program that will just basically print a message
01:53and do nothing else.
01:54How do we compile this program and execute it?
01:59You know, what does that actually even mean?
02:01Okay, so for starters, the first thing that you probably do when you're compiling a program,
02:07you've probably been told to do something like this.
02:08Let me get my little command line open here.
02:11You've probably been told to do something like this.
02:13Let's compile with GCC or G++ and we'll say the standard for C++ that we're going to use
02:19is 23.
02:20There are a bunch of other standards you can use.
02:23We can turn on all warnings.
02:26We can have the compiler be pedantic in terms of the warnings that it gives us.
02:31You know, these two things at the end don't really matter that much.
02:34It's really all about G++ and then std C++ 23 to get it compiled.
02:39And then we'll give an input file, we'll say burst.cpp and then I think probably usually
02:47this is where most of you stop.
02:48We just kind of like specify a couple flags, specify the input file, then hit enter and
02:54it compiles.
02:55If we list the contents of the directory now, this might be familiar.
02:59You'll probably see something called a.out.
03:02A.out, notice how the permissions look a little bit different for that file.
03:08It's kind of printed in green in Ubuntu.
03:10And if you look at the left side right here, you can see that it's got a bunch of X's in
03:15the permissions area.
03:16That just means it has execute permission.
03:18The compiler does that automatically for you, but essentially this is like just, you know,
03:23an executable binary file.
03:26It's not a text file.
03:27It's just something that the computer is meant to run.
03:30We can look inside of it real fast.
03:31There's a program called xxd on Linux and it'll sort of spill the contents of that file.
03:37So if I say xxd and then I go a.out, it just spills like a whole lot of like binary nonsense,
03:43right?
03:44You can see at the very beginning, elf, that's, uh, I think that's executable linking format
03:50for Linux.
03:51And then there's like a bunch of stuff and it just doesn't make sense.
03:53This is not text that you can read.
03:55Here's like some sort of a shared argument that it's using.
03:59And then, uh, some register information.
04:02I don't even know what most of this is somewhere in here.
04:05If you look carefully, you'll probably see that message.
04:07Where is that?
04:08Oh yeah.
04:09Hello from C first dot CPP.
04:11So it's saying that the message we typed in the program is actually embedded in the executable.
04:15That makes sense.
04:18So hopefully I've proved to you that this is just an executable program.
04:21How do you execute the program?
04:24Well usually you'll have to type, you know, the full path to the program that you want
04:28to execute because your custom program is not in the system library paths.
04:33So your system's not going to know where to find it automatically, even if it's in the
04:36current folder.
04:38So you would usually do something like, you know, the full path to your executable, like
04:42a dot out, but there's a shortcut we can use in Linux.
04:46Uh, the, the dot directory usually just means, or it always means the current directory.
04:53So if we say dot and then a slash, that's the same thing as typing out the full path
04:57to the current directory.
04:58Then it's really easy to just type the name of the file that we compiled.
05:01Okay.
05:03We executed it now.
05:04So that's what it means to execute.
05:06We just compiled without a lot of options.
05:08We're going to try to do that better in a second.
05:11And um, we know that you need execute permissions.
05:14Let me show you what happens if we take away execute permissions.
05:17I'm going to use a command called chamod, like change the mode.
05:20I'm going to say take away execute permissions from the a dot out program.
05:24But if I list the directory again, notice how there are a bunch of X's missing now on
05:29the left side of that a dot out permissions area.
05:32Notice how it's not in green anymore, depending on your system, it might not be green or gray
05:36or whatever, but just look at the permissions.
05:39So now if I do a dot out, it'll say permission denied.
05:44Maybe you've seen this before, especially if you're syncing with the cloud that doesn't
05:47carry execute permissions from one machine to another, which happens to me all the time.
05:52Uh, we can reverse this with, uh, just the chamod, you know, plus X execute permissions.
05:59And uh, then we can just execute it again.
06:02The funny thing about execute permissions is, uh, you can actually execute a bash program,
06:08which is basically a text file without, without needing to execute a binary program.
06:13So this is not a bash video.
06:14I'll make other videos in the future, uh, on my channels, uh, for, for bash.
06:20But for now, just know that, uh, something you compile is not the only type of thing
06:23that you can actually execute.
06:25In fact, in Linux under the hood, a lot of system tools are just written in bash or Python
06:30or something else that looks like a human readable language.
06:35And they are not necessarily all compiled languages, uh, you know, like, like C plus
06:40plus.
06:41Okay.
06:42So now that we kind of know, um, that we're compiling a program, let's be a little bit
06:48better about, um, specifying the name of the output file.
06:51So I just want you to see one more flag here.
06:54And while I'm doing that, I'm going to create a script that will compile our program for
06:58us.
06:59So I'm gonna say nano, and I'll just name a, a file.
07:04I'll name it compile, and it'll be our compiler program.
07:07Okay.
07:08So what we did before is it was, I think, G plus plus, and then we named the standard,
07:12um, is C plus plus 23.
07:15And then we said, wall wall just means give me all warnings, which is good to help your,
07:20you know, to help you write better code.
07:22Same thing for, for pedantic just helps you write better code.
07:26It gives you more annoying, uh, warning messages, but you know, it's pretty good.
07:29Okay.
07:30So this is where we stopped last time.
07:33So I'm going to say now, let me remove a dot out.
07:38Now instead of typing the command by ourselves, we can run the compile script.
07:43Uh, the language I've just written was, was, well, I haven't really written in any particular
07:48language, but if we use a shell interpreter called bash,
07:52which is actually what's running in the terminal right now, it'll interpret any command line
07:56that we give it as just something that we want it to execute to the system.
08:00So if I type bash to run the bash interpreter, and I just specify the name of the script,
08:07it should compile the program automatically by executing that one line that I put inside
08:11of it.
08:12See, now I've got the a dot out again.
08:14If you rewind the video, you'll see the a dot out wasn't there when I originally ran
08:18it.
08:19So one more time, just to prove it to you, uh, bash compile.
08:23Now you can see that it's back.
08:25Okay.
08:26So let's make this compile program a little bit better.
08:28Instead of typing bash every single time, I think I just want to try to execute the
08:32compile program directly.
08:34I can do that pretty easily.
08:36There's something in, uh, in our script that we can put that will designate what interpreter
08:41we should use to run the program.
08:43So right now it's just like some text and it doesn't really mean anything.
08:47There's something called the shebang, which just means hash bang, and, uh, we can just
08:52provide the path to some interpreter.
08:54I'm going to type shebang and then a path to the bash interpreter.
08:58If you don't know where bash is on your system, you can go, uh, which bash and I, well, I
09:03guess mine is a user bin bash.
09:05I think I can get away with bin bash.
09:08Let me see.
09:09Uh, let me just type it out to make sure.
09:10Bin bash.
09:11Yeah.
09:12Okay.
09:14I'm actually in another version of bash, which is inside of bash.
09:17If I do exit, I should just kind of stay where I am.
09:20Yeah.
09:21Okay.
09:22So bin bash will work.
09:23User bin bash will also work.
09:24I'm going to go back to editing this, uh, this compiler program.
09:27And now all I have to do because the system is going to know what interpreter to use to
09:32execute my little script program here.
09:36All I have to do is add execute permissions on it and it should work.
09:40So let me first remove a dot out and then, uh, let me try first to execute compile.
09:46It should not work because I don't have execute permissions.
09:48Remember we talked about that before.
09:50So if I do that, it says permission denied.
09:52Oh no.
09:54So I'm going to give it permission to execute.
09:56I'm going to say change the mode plus X on compile.
09:59If I list the directory again, notice how it's green.
10:01Notice how there's a bunch of Xs everywhere.
10:05So now I should be able to just say compile.
10:10And then it seems to have worked.
10:11And now notice how it has an a dot out.
10:13I'm coughing.
10:14So a script is really useful for you because it's a pain in the butt to type out all the
10:19compile commands every single time, especially if you have a complicated compile command
10:24or many files that you want to compile in the same shot.
10:27So, um, you know, of course in another video, I'm going to talk about make files, uh, build
10:33systems, proper build systems are a lot better than just making a script.
10:36But for now, let's just make a script because I just want you to understand, you know, compiling,
10:41linking, executing as a, as a basic concept and just some of the surrounding ideas.
10:45Okay.
10:46So I'm going to, let's see, I'm going to go nano compile.
10:53And the next upgrade that I want to make is I want to specify the output file because
10:57it's kind of a bad idea to not specify the output file and just hope that the compiler
11:03gives you the file that you wanted.
11:05I don't know.
11:06So I'm going to do dash O main, and that's going to give me a file called main, which
11:10is going to be my program now.
11:12So if I remove a dot out just to keep my area clean, I can now do compile.
11:19And if I list again, notice how I've got main so I can execute main and it's the same program
11:25that we already had.
11:27In fact, uh, maybe sometimes just as a shortcut, since this is a script, what if I'm always
11:34hitting LS dash LA?
11:35I always want to list the folder after I compile.
11:37I could just put that into the script.
11:40The more commands you put into the script, the more convenient, uh, things can get unless
11:44I guess it gets too messy.
11:46So I'm going to do list first and I'm going to remove main just to prove to you that we're
11:52doing this from scratch.
11:53And then I'll do a compile.
11:55Notice how it compiles and then lists everything for me.
11:58Seems like it didn't actually turn the compile green.
12:00That's probably another flag that we have to stick in there.
12:02Let me see if I can do that real fast.
12:04If not, I won't waste time.
12:05This is not an LS video.
12:07I think probably a color or colors is what I got to do.
12:11Let me just do that one more time.
12:13Yeah.
12:14Okay.
12:15So that's just a flag on LS.
12:16That's not what this video is about.
12:20So now we're compiling directly to main, but here's the problem though.
12:24We're actually kind of sort of skipping over some steps that we could be aware of.
12:28Uh, when we compile our program, we're actually taking our source code and we're assembling
12:34it down to assembly code under the hood.
12:36And then we're compiling it to an object file, which is sort of just machine code.
12:41It's like a level lower than assembly.
12:43And then as a last step, it's getting linked into an executable named main.
12:47So we're skipping a ton of steps.
12:50Really what's supposed to be happening is every single source file.
12:53Oh, whoops.
12:54I'm on the wrong machine right now.
12:56Okay.
12:57Where's that?
12:58Oh, the machine that I'm recording on.
13:00There we go.
13:02Normally we're saying like first dot CPP, and we're thinking that it just goes directly
13:06to main.
13:07You know, that works if you only have one source file or if you don't want to have fine
13:10tuned control over your compiling, but I would like to do something a little bit more detailed.
13:16Let's compile our CPP source file into an object file.
13:20I'll name it first.
13:21Oh, and then we'll link that into main.
13:23Why is that useful?
13:25Because later when we have multiple source files, we might, we might want to compile
13:28them all to object files first and then link them all together in one step into main.
13:34This will be even more useful when you later realize that you can use a build system that
13:38does kind of the same thing to save compilation time.
13:42I won't talk too much about make files here, but let me just tell you, when you start using
13:45a build system, your compile time can be drastically reduced a story.
13:51I used to have a program a long time ago that I wrote myself took like an hour to compile.
13:55I would literally every little change I made, I'd have to get up and leave and go make a
13:58sandwich or something, and then come back by the time it was finished compiling.
14:02When I started using object files as intermediate steps and upgraded to a build system, I was
14:08using GNU make.
14:10Then my compile time went down to like a minute each time.
14:13So it was like so much more efficient.
14:15Anyway, let's do that.
14:16Let's do that now.
14:17Let's, let's, let's nano our compile script.
14:20And you just need another flag in here for the GCC compiler to say, I would like to compile
14:27this source code to an object file and not actually produce a fully linked executable
14:32that you can execute.
14:34That means I should probably rename main to something else because main is supposed to
14:38be my executable.
14:39At least that's what I've chosen.
14:41I would like to have every object file be named similarly to the source file it belongs
14:46to.
14:47So I'm going to type the output now is going to be first.o.
14:50And then the C flag just says only compile don't link.
14:54This means of course, that I'm not going to produce a program that I can run.
15:00Let's see.
15:02I'm going to end up accidentally deleting the compiler.
15:04Okay.
15:05But we can see what we're doing.
15:06Okay.
15:07So compile and CPP for first.
15:10So I'm going to compile it.
15:11And now notice how we have an object file and no main and just the source file and its
15:18corresponding object file.
15:20So I can't run this program.
15:22I have to do the additional linking step now.
15:25So I'm going to do compile.
15:28And in my editor here, let's see, where is my command that I wanted to do for linking?
15:33Okay.
15:34So I'm going to link with the GCC libraries.
15:36I'm going to use the G++ command.
15:38I'm just going to put a couple of flags.
15:39I'll explain them real fast.
15:40I'm going to say that I want a 64 bit executable, which you probably actually don't have to
15:44specify.
15:46I'm going to anyway.
15:47And then I'm going to tell it again that I want it to link up against the C++ 23 libraries.
15:54This is important if multiple modules reference the C++ libraries.
15:58I'm not even sure.
16:00I think I need it in both the compiling command and the linking command.
16:04I'm going to leave it in both because it should work.
16:08Usually if you want to embed debugging symbols into your program, you want to put a dash
16:11G there, which is a great idea.
16:15But we're not going to talk about debugging in this video.
16:18You can also specify the type of debugging symbols that you want.
16:21I'm just going to put that in here right now, gdwarf2.
16:25And then there's some new flags that you kind of need.
16:29There's one here called nopy, which I think is just an Ubuntu thing.
16:33Your program probably won't execute if you don't do that.
16:35Just put it in there and trust me.
16:37There's another one that we have to put in now called noexecstack, which basically prevents
16:43your program from executing code sitting inside of the stack, which is just sort of something
16:47to help you not get hacked, hopefully.
16:50It's not bulletproof, but you know, it might help.
16:53And then I'm going to give it input files.
16:55The input files for the linking stage, maybe I should do a comment.
16:59Linking stage.
17:03Compile the C++ program or source or something.
17:07For the linking stage, you want to provide all of the object files that you're going
17:12to link together.
17:13In this case, we just have, you know, the first object file named first.
17:18And then we can specify the output file as main.
17:21If we didn't specify, it would probably be a.out like we saw before.
17:25So if I got this right, let's see if I did.
17:27Now this should compile the C++ source code file into its corresponding object file and
17:34then scoop up the object file, turn it into an executable.
17:38Okay, so I'm going to clear and then list, and then I'm going to remove the object file.
17:43I'm going to put star.o, which is a shell trick to basically say that star.o is going
17:49to expand to really mean every file that ends in .o.
17:54So in this case, it's actually just going to end up being first.o.
17:57But it's a trick you can use to grab multiple object files or files of a certain type all
18:02in one command.
18:04So I'm going to do that, and then I'm going to list again, and then I'm going to compile.
18:08And you can see that the compile command, it created first.o, the object file, and it
18:13also created the executable main.
18:16So if I execute main, the same thing should happen.
18:19So great, we're ready to move on to another step.
18:23Let me remove star.o.
18:24I got to be so careful because I'm going to totally ruin this video by erasing the wrong
18:28thing.
18:29OK, because I should be in a Git repository, but I'm trying to be fast here.
18:33OK, so let's nano compile.
18:37Let's create a second source file, actually.
18:39OK, so let me get out of this.
18:41I'm going to create a second source file.
18:42I'm going to call it second.c.
18:44It's going to be a pure C program.
18:48And because we don't have C++ libraries in C source files, I'm just going to say standard
18:53input output .o for the header.
18:56I'm going to make a function called void second, probably, and it doesn't do anything except
19:02just sort of print a little message.
19:04So I'm going to say hello from second and then do a new line because we don't have indels
19:09in C. All right, I think that's probably going to work.
19:13So then let's edit our compiling script and we'll just add another line here to compile
19:21the C source.
19:24OK, so I'm going to go gcc because it's not C++ anymore.
19:30And I'm going to do all warnings and pedantic again.
19:35You don't really even need to worry about that.
19:37And I'm going to give it an input of second.c because that's the file we just worked on.
19:42And I'm going to tell it I only want you to compile with the dash C flag.
19:46If I forgot that on this one, then I'll probably end up with an object file, a main, and then
19:51a.out that's only based on the C source code.
19:53And that probably wouldn't work because the second source code doesn't have a main function
19:58inside of it.
20:00So let's see, we'll do output and it's going to be second.o and then I think that should
20:08be OK.
20:09You know, something that I just realized I should mention, too, is when you're making
20:12hybrid programs like this or programs where there are just many, many source files, like
20:18I said before, you don't need to have a source code named main.cpp.
20:22You can name all your source codes anything that you want.
20:25The one thing that you really do need is a piece of source code.
20:29Oh, I'm editing inside the terminal suddenly.
20:31Sorry, I got sidetracked.
20:33I wanted to edit all this stuff inside of Genie because it's a little bit better.
20:41Maybe it doesn't matter.
20:44So notice how we have the main function inside of our first.cpp program and then there's
20:49no main function inside of our other source codes.
20:53For your program that uses the GCC libraries, you need exactly one instance of main.
21:00So that means, you know, you have to have at least one and if you have more than one,
21:04it's probably not going to link.
21:06So if I put a main inside of here, probably not going to work.
21:09This also kind of means you can arrange your source code in a funny way.
21:12You could have a bunch of different pieces of source code that you could consider maybe
21:16libraries.
21:17Maybe there are a bunch of classes or something that will help your program and you can release
21:21multiple different executable files by just having different source code files, each with
21:27their own mains and linked separately against the object files that contain your library
21:33or classes or whatever.
21:34That's just a little side tip, but anyway, let's see if I actually did this right.
21:40So we're linking this and I'm going to see if it works.
21:45I get stuck inside of the terminal and I start thinking I'm going to edit everything inside
21:49there.
21:50Okay.
21:51So it compiled.
21:52Well, let me, let me remove star.o and remove the main and then just clear it again in the
21:58list.
21:59Okay.
22:00So now I'm going to run the compiler and after the compiler ran, everything seemed to be
22:05okay.
22:06So it compiled first into its object file and it also compiled second into its object
22:10file.
22:12If I run the main executable, notice how it only prints hello from first.cpp.
22:16Well, what about the second one that we just added?
22:19Well, what we need to do is improve our linking stage.
22:23Notice how in the linking stage right here, we just only link first.o.
22:27So we should also include the other object file that we want.
22:29I'm going to glob that in a second, but let me show you compile.
22:35Now if we run main, what did I do wrong?
22:42Hold on.
22:43Let's see here.
22:44Second.c.
22:45Oh, I forgot to actually call the second function.
22:47So that's cool.
22:48Just wrote a bunch of stuff for no reason at all.
22:52Let me have the main function call on the second function.
22:56We're going to encounter an issue here too.
22:58The second function is written in C and the first function, uh, it's its source code is
23:04in C plus plus.
23:06And the thing is C plus plus uses something called name mangling.
23:09I won't go over it too much in this video, but it uses name mangling so you can overload
23:14functions.
23:15You can have functions with the same name, but like different signatures, right?
23:17So it'll go in there and it'll mangle the name of a function according to its overload
23:22prototype so that we can actually call overloaded functions, but we only have that in C.
23:28So that means if we try to call a C function from C plus plus, we're probably going to
23:33end up calling the wrong name because there's the expectation that the other module has
23:38name mangling enabled, but it's not because it's C.
23:41So I just have to do this little block extern C and just name all the stuff that is not
23:47going to be name mangled.
23:48I'll just provide the prototypes of all the functions that I want to be able to call.
23:53So just void second.
23:54If you look here again, we just have void second.
23:56And now when the C plus plus module calls on second, it won't expect name mangling,
24:03so it should work.
24:04So we're calling it and then we linked it.
24:07Now everything I think should be okay.
24:08I'm going to compile one more time and then I'm going to run the main program and now
24:14you see that we have both calls in there.
24:16Awesome.
24:17Let's give this script a little bit more output though.
24:19This is kind of precarious.
24:21What if something failed inside of there?
24:23Maybe you don't want the script to continue if something fails and maybe you kind of want
24:27to know where the script is at while it's compiling.
24:30So I'm going to upgrade it just a little bit.
24:32So compile the source.
24:34I'm just going to say echo compiling the, you know, compiling burst dot CBP.
24:41And then here I'll just do another echo and I'll say compiling a second dot C. And then
24:46here I'll say echo linking the executable or the binary, AKA the binary.
24:56I don't even know if that's the proper term.
24:58I just like to call these things binaries because they're not in text anyway.
25:02So if we do another, I'm going to do, I'm going to do a compound command line here.
25:08I'm going to go clear and compile so that I don't have to continue to clear every single
25:13time.
25:15Now notice how it prints what it's doing.
25:17So if it kind of failed somewhere along the way, or if it took a long time, you'd kind
25:19of know what's going on.
25:21Now let's use a little bash trick because remember this is a bash script.
25:26It's not just going to only execute actual commands.
25:30I'm going to say, or exit with the return code of the previous command.
25:38So basically here on the left side, we're going to try to execute G plus plus if G plus
25:43plus fails for some reason, then the whole program or the whole script is going to exit
25:48and it's going to exit with the return code mentioned here.
25:53I think it's not going to print it.
25:55I'll show you how to print it in a second, but we can actually make a function called
25:59die up here in a second, which might be better.
26:02So I'm going to do the same thing over here.
26:04I'm going to say, or exit with the return code of the GCC command and then here or exit
26:10with the return code of the linking command.
26:14We'll do it one more time here.
26:16Notice how it seems to work.
26:18So now let's make the first C++ program not actually compile.
26:24I'll put a bunch of Zs there.
26:27And so if we try to compile it again, it should just quit right after it tries to compile
26:31the C++ program.
26:32Right?
26:33So that saves you some time and helps draw your attention to what went wrong.
26:36The C++ compiler is saying, Hey, there's like a type there.
26:40It doesn't show the exit code though.
26:41So let's make a function in bash that will die and print an exit code.
26:47So I'm going to say do die or maybe just die by itself, something like that.
26:52Functions and whoops.
26:53I'm in the wrong language.
26:54Let me go back to the compiler, make a function in bash called die, and it'll take one argument
27:01and the argument will just be the exit code that we want it to alert us of.
27:06So I'm going to say exit echo, I'm going to say we are dying now.
27:13Previous exits code was, maybe I'll do a little variable here.
27:18I'll do a local variable.
27:21Exit code is going to be the first incoming argument.
27:24This is a bash program, not C++.
27:27And then here I can just say exit code.
27:30Okay.
27:32So then after writing all that, I can say or die and then just, you know, so I'm calling
27:40a function and the first argument I'm giving it is the actual exit code of the thing that
27:44died.
27:45So we're just kind of like using indirection to print a pretty, pretty message to get a
27:50little bit more information.
27:51So I'm going to try that one more time.
27:56Notice how it says we are dying now.
27:58Previous exit code was one.
27:59So that just tells us that GCC exited with the exit code of one.
28:04Not super useful, but it's nice to see.
28:07Could be something else entirely.
28:08Like, I don't know, maybe, maybe we misnamed the first source code file.
28:13Let's see if the code changes.
28:14I think it probably will.
28:16Nope.
28:17Maybe it's exit code was one, no such file or directory.
28:22All right.
28:24Exit code.
28:25Am I doing something wrong?
28:27I guess I'm all right.
28:28Okay.
28:30I wanted it to be fancier.
28:33We could also upgrade a little bit if we wanted to.
28:37Instead of echoing, I could make a function called log here.
28:40And I could just say that the log command just echoes and maybe we'll put a cute label
28:45and we'll say, you know, my super cool program compiler, or how about super cool compiler.
28:54And then just do the $1 because that's the first incoming argument that you get.
28:57Now I can type log instead of echo.
29:01So that's nice.
29:02And why would you want to do this?
29:03Well, now it's going to go inside of a function, which means later on you can start adding
29:06extra commands like a logger, a log to the system daemon.
29:10Oh no.
29:11So this would write to your system log in addition to printing something.
29:15You could do anything you want.
29:16You know, it's just like a regular program, adding more power by sticking everything inside
29:21of functions.
29:22I'm going to put log here and let's see if it works.
29:26Compile.
29:27Okay, nice.
29:28Notice how we have a little message with a little header, which seems pretty cool.
29:34Maybe instead of echoing here, let's make a complaint function.
29:38Let's do a complain and same thing as the echo function, but maybe I think there's a
29:47flag where you can tell echo to print to standard error instead of standard output, but I'm
29:51just going to do it the easy way.
29:53I'm going to say one goes to two so that the first pipe standard output is going to
29:58be redirected into the second pipe standard error.
30:01So now I can use complain instead of echo down here.
30:07Complain before I die.
30:09Oh, I forgot to actually die.
30:13I forgot to actually exit.
30:14So that means it was, oh yeah, it's still compiling everything.
30:18What a dope.
30:19Okay.
30:20Let me, let me fix that.
30:21Let me fix the die function.
30:23Hopefully you were at home cringing along with me.
30:26Let's see.
30:27Let's exit with the actual exit code that was specified just to make things a little
30:31bit more automatable.
30:32Yeah, now it stops right away and the complainer is like, you know, pretty cool.
30:39Let me fix the first source code real fast and we'll, we'll say that the second source
30:43code is broken.
30:44We'll put a bunch of Zs there.
30:46All right.
30:48So now the first one compiles.
30:50The second one it breaks and we're starting to, you know, get a hang for a compiler script.
30:57That's a lot more sophisticated than just typing things on the command line, right?
31:00You can take this as far as you want.
31:03The next step above this is using a proper build engine.
31:06So let's see, well, let's, let's do another source code.
31:12Let's do, um, whoops, let's do a, an assembly source code.
31:18So we'll say it's third.asm and it's just going to be Yasm.
31:22This is not a Yasm or assembly tutorial.
31:24I'm just showing you how to, you know, build a, you know, compile link and execute.
31:30We'll make a data section so I can just stick some, uh, defines in here real fast.
31:34I'll say system write is going to be equal to a one.
31:39And then the file descriptor for std out is going to be equal to one.
31:44Do you ever feel like red jumps out at, uh, at you on the screen?
31:48Does that mean I have eye problems?
31:51I am kind of old now.
31:51I don't know.
31:52Hello from third.asm.
31:56And I'll just put a little new line there so I don't have to do it elsewhere.
32:00And then I'll have to specify the message length because this is not a sophisticated program
32:05that can loop through the message looking for a null terminator.
32:09Um, and then I'll do a text section, which is where your code goes.
32:14And, uh, you know, in hybrid programs, if I have this third function, which is marked
32:19as a, which is marked using a label, and then in return statement, I still have to mark
32:25a third as being accessible outside this module.
32:27So I'm going to do the global statement and then I'm just going to do really quickly.
32:33I'm going to move something into rex.
32:35It's going to be system, right?
32:36So basically I would like to, uh, write something somewhere to some file descriptor.
32:41Its first argument wants a, where I want to write it.
32:44So, uh, I'm going to write it to the standard output instead of an actual file.
32:49And then I'm going to move RSI.
32:51The next argument is going to be the actual message or a pointer to the message that I
32:55defined above.
32:56And, uh, you know, check out my other videos.
32:59If you want to learn assembly, that's, this is not, this is not an assembly learning video.
33:03I'm just kind of doing it real fast in case you're interested.
33:07So I have an assembly program, probably, unless I ruined it somehow.
33:11Let's start, uh, compiling or assembling the assembly program.
33:15So just one more, we'll say assemble the assembly source or module or whatever you want to call
33:23it.
33:24We're going to use Yasm to do that.
33:26Um, gdwarf2 is the debugging symbols, not necessary in this video.
33:32The format is I want a 64 bit executable.
33:35That's how you do it in Yasm.
33:36And I'm going to give it an input of just the third assembly source file.
33:40And I want it to output the third object file.
33:44I got to remind myself to glob line 36 after this or die with the, the exit code.
33:52Okay.
33:53So now this might work.
33:54Oh, I got to put, I got to put third.o in the linking stage.
33:58I'm going to do clear and I'm going to go here and compile.
34:07Unrecognized debug format, DWORD.
34:10Why didn't you stop me?
34:12It's gdwarf.
34:13Let's see.
34:14What did I do wrong?
34:15Gdwarf.
34:16Oh, dwarf-2.
34:17Oh, it's not even G.
34:19It's just dwarf-2.
34:20Sorry.
34:21And I got to put a dash, I think, for Yasm.
34:25Yeah.
34:26Okay.
34:27Let's see if that works.
34:33What did I do wrong?
34:34Oh, no, no.
34:36Yasm doesn't need the dash, but GCC wants the dash.
34:40Okay.
34:41Had it right the first time.
34:43Doop, doop, doop.
34:45Okay.
34:46So it probably linked.
34:47Let me run it and see what happened.
34:49Hello from the second.
34:50Oh, we still have to actually call it from the C++ module.
34:54So now we'll do the same thing.
34:57We'll say that the assembly module, it's not going to use name mangling.
35:01There's no overload.
35:02So we're going to put it into the extern C block and we'll just say, you know, void third.
35:07How do we know that it's a void function with no arguments?
35:10Well, you know, that's just a convenience that happens under the hood with C and C++.
35:15In assembly, it's just a label that you're jumping to and returning from a void means
35:21return type.
35:22All that means is I'm trying my best not to make this a C plus or an assembly tutorial,
35:27but all that means is there is no return value getting returned.
35:34That would be the RAX register if I wanted to move something in there.
35:37And then similarly, if I wanted to catch arguments, I'd be grabbing from other registers.
35:42So I'm not grabbing any register, any arguments from registers, and I'm not sending a value
35:48back with RAX or XMM zero or anything.
35:52So that just means it's void and no arguments.
35:54So let's do it again.
35:55Compile it and then run main.
35:57Hello from second.
35:58Oh, I sure put in the name mangling thing, but I forgot to call it.
36:02Let me do that third.
36:04So now one more time, compile and then run it.
36:10What's happening in here?
36:12Third, I guess I should probably open this up now.
36:18Third, ASM.
36:20So global third and then third.
36:22Oh, I forgot the most important part of a system call.
36:26You got to put the keyword or the instruction system call in there.
36:31One more time.
36:32I hope you're laughing and cringing at home trying to make this more fun.
36:36Maybe.
36:37I don't know.
36:38Okay.
36:39So now it's all it's all working.
36:41We're using different modules in different languages.
36:42We're compiling them all to object files first, and we're, uh, we're linking them all into
36:47one executable and we're doing this in a really, really easy way by using a script instead of
36:52typing all the commands out manually.
36:55Pretty convenient, right?
36:56Let me give you another upgrade real fast here.
36:59So this is kind of irritating.
37:02I have like all of these object files that I have to specify.
37:05What if I forget to specify some, since this line 36 is just going to be something that
37:09goes directly, uh, you know, to the shell as a command line.
37:13This is not like bash language type stuff, at least for that part.
37:16Um, I can just use globbing or, or, or wildcards.
37:21I can say star dot Oh, meaning I'm going to give every object file to the linker as an
37:26input file.
37:27And that should just work automatically.
37:29Keep in mind if you want to use an advanced strategy, like I mentioned earlier, where
37:32you have multiple different object files and they've each got their own main functions
37:38because they're each going to be their own separate executables.
37:41Then globbing all object files, probably not going to work.
37:43You might want to organize it differently somehow, like stick the executable object
37:48files or stick the object files for executables in like their own folders or something like
37:55that.
37:56We're not going to go that far, but I just want you to know.
37:58So let's do that again real fast here.
37:59We'll say clear and compile.
38:02And I'll just try to like run main, uh, assuming that the compiler works.
38:07Yeah.
38:08So it worked.
38:09Everything compiled by the way, this end end.
38:11That means only execute the next part of the command if the previous one succeeded.
38:16So if I, uh, you know, clear is obviously going to succeed and then compile, it might
38:21succeed or not.
38:22Then I'm going to try to run a main.
38:26Let me actually, let me actually do that real fast.
38:27Let me show you what I'm talking about.
38:28I'm going to remove main you're okay.
38:31So then I'm going to do a clear and compile and main.
38:34So obviously if everything works, then everything works.
38:37But if I do that again, where something is not actually going to compile, like maybe
38:44the second, the C source program is not going to compile by putting some Zs in there.
38:49Then the, uh, dot main or yeah, that part shouldn't get executed.
38:54So it won't try to execute our program.
38:57Notice how it just stops.
38:58So that's convenient.
39:00If I do it again, though, with a different command, if I chain, instead of between the
39:06commands, instead of putting end end to say one must execute before the next one can,
39:11if I just put a semi-colon, then they will all execute no matter whether, whether they
39:15succeeded or not.
39:17Maybe in certain things you're doing, you actually want that behavior, but in this case
39:20is going to be bad.
39:21Watch what happens.
39:22It fails to compile.
39:23And then at the end, it also tries to run a program that doesn't exist because it never
39:28got linked because the script stopped.
39:31So I'm just letting you know, you have lots of different options when you're writing these
39:34scripts.
39:35We do it again.
39:36And then let me fix the Zs.
39:40And um, I think, well, maybe there's one other thing I want you to know.
39:44Sometimes these lines get really, really long, right?
39:46And it's, it's messy and irritating, at least for me.
39:50Maybe if it goes off the side of the screen for you, it's like, Oh, that's awful.
39:55You can actually just separate a command, uh, uh, uh, across multiple lines with a backslash
40:03or sorry.
40:04I think that is called a forward slash.
40:05It's the, it's the, the drive slash.
40:08I don't remember forward slash backslash.
40:10I just say web slash and drive slash.
40:12So I'm going to do G plus plus and do a little slash.
40:15Then I can put the whole rest of the thing on the next line.
40:17If I wanted to, I could start separating these out by category.
40:20So maybe I'm like, Oh, I don't know.
40:23Maybe M64 on its own line.
40:25And then I'll keep these other two on their own line.
40:27Uh, maybe these two on their own line, you know, it's up to you how you organize this.
40:32And then I'll say, give me all the input files on its own line.
40:35And then the output file on another line, you could even put the or bars, uh, for the
40:40die part on its own line, I guess I'll, I'll just try that.
40:46And now let's just make sure that this still compiles.
40:50Let me remove star dot O and remove main.
40:54It's not there.
40:55Okay.
40:56So clear.
40:57And then list.
40:58And then two showed up somewhere.
41:01Oh, my mistake.
41:02I probably should have caught that.
41:04Some file got saved as a two, I should have noticed.
41:10Okay.
41:11So let's do clear and, uh, compile and I'll just run main.
41:21Okay.
41:22So now everything works.
41:23Uh, notice how, even though everything was on a different line, it, it all just seemed
41:26to work cool.
41:29What about repeating code?
41:31Because notice how we're kind of doing something here, uh, on line 38, we're using the C plus
41:37plus 23 standard, but we're also doing that up here.
41:40And we're also using the, uh, all warnings flag up here and, um, down here and pedantic
41:46also.
41:47And even the linker, like, what if I want to put all warnings into the linker?
41:51We could make variables in bash.
41:52And by the way, this is totally starting to become a job for, for mark, uh, for make files.
41:58But, uh, for now we're just going to stay in bash so I can make a variable in bash.
42:03I can say, uh, uh, maybe GCC flags, name it something, and I'll just put equals whatever.
42:10And I'll just type the stuff that I know is going to appear in every single invocation
42:15of G plus plus or GCC.
42:16So I'm going to go wall because I want to put that everywhere.
42:20And I'm going to say, um, C plus plus 23, cause I'm going to put that everywhere and
42:25probably pedantic.
42:26I'm going to put everywhere.
42:29I'm not going to put the dash C flag everywhere because notice how I'm only putting that on
42:34the compiler, uh, commands when I just want to make an object file, but when I'm linking,
42:37there's no dash C because that means you've got to actually link.
42:40So let's see, we got G dwarf two, G dwarf two.
42:46Maybe I could put G dwarf two there and see if it still works.
42:50So I want debugging symbols and I want them to be in dwarf dash two format, which honestly
42:56is the default format.
42:57If you just put dash G, let's see if I can get away with that for now.
43:01So I'm going to go GCC flags and here I can get rid of C plus plus 23 and wall and pedantic.
43:08And I'll just insert instead GCC flex.
43:11And if you want to be extra careful, you can put these little braces around the variable
43:15name of that probably should be fine, but because I don't want to type that again, I'm
43:19just going to copy paste it into the other commands.
43:22So wall pedantic, just paste that.
43:25And then I'm going to leave it alone for Yasm because Yasm is different than GCC.
43:30And then here, I'm going to do another line where I just kind of put the flags and that
43:34means I can get rid of those two flags and then a wall and pedantic are going to show
43:38up.
43:42No pi is kind of a linker only thing.
43:44So is the no exec stack.
43:46I think probably that will be okay.
43:48Let's see if I got that right or wrong.
43:50Oh no, unrecognized command line option, wall pedantic, GCC, oh, I guess cause I quoted
43:58it.
44:00The command line thought that that was all one argument I should have remembered.
44:04So I'm going to unquote it right now.
44:07This should help the command line understand that all of the things inside of that variable,
44:11the GCC flags are separate arguments instead of this one argument.
44:16Let me try one more time.
44:19Doop doop doop.
44:20Yay.
44:21It seems to work.
44:22Okay.
44:23We've got a warning here.
44:24Command line option C++23 is valid for object, but not for C. Okay.
44:30So basically I made the mistake of trying to send a C++ object, which is this right
44:33here or sorry, a C++ argument, which is this right here into a C compiling command.
44:39Not great.
44:40So what if I did this?
44:41I'll do C flags and it will contain only flags for C and then I'll have another one for C++
44:51flags which will only contain things that are for C++ compiling.
44:57So the regular C compiling can take wall and pedantic.
45:01Okay.
45:03And then it can take the G dwarf too.
45:06Okay.
45:08The C++ flags, it can take everything except for, um, sorry, the C++ flags can actually
45:14take all of the C flags and then additionally the, the C++23 flags.
45:20So I could, I could embed a variable inside of itself.
45:22I could say a C flags or sorry, I could embed a variable inside of another variable so that
45:28I can just steal all of the flags from the C flags variable.
45:32Stick them also in the C++ flags variable and then just add one more at the end of that.
45:38Hopefully that works.
45:39I think it probably will.
45:40Then in my compiling, I can just say, use the CPP flags here and then use the C flags
45:47here just for compiling.
45:48I could probably name the variable better, but the word compiling there or something,
45:51but I don't really want to, um, I think some people put CC to mean like C compiling, but
45:57I don't know.
45:58I just want to do it this way.
46:00So we have that.
46:01And then we're going to do CPP flags, um, like this.
46:09We could also organize the variables differently so that the, uh, we could have some linker
46:13flags, which basically, uh, would probably have all of this stuff minus dash C.
46:19And then we could have, uh, linking flags and we can have linking flags that has all
46:23that stuff minus the C.
46:25And then we'll have like some compiler flags that has the linking flags plus the dash C.
46:31But I don't really want to spend a bunch of time on this now.
46:34For now, we'll just say we have C flags and CPP flags, and we'll just manually add that
46:37dash C.
46:38But of course, keep in mind, you can organize these things any way you want.
46:43So I'm going to do, well, this might actually work.
46:47I'm getting distracted.
46:49I think it works.
46:50Okay.
46:51Let me just double check by removing all the object files.
46:56There's a two in there still.
46:58Let's debug that.
46:59If it happens again, then I'm going to remove main.
47:02Okay.
47:03And then I'm just going to list everything real fast.
47:06Okay.
47:07Let's see.
47:08Is there going to be a two in there now?
47:10No.
47:11Is that only when it's fails only when something fails to compile?
47:16I mean, it's working.
47:17So that's good news.
47:19You know what?
47:20Leave a comment somewhere letting me know where you think I went wrong with this two.
47:26And maybe I'll release another video or something in the future at some distant point in the
47:31future.
47:32But yeah, anyway, I hope you learned a lot of stuff in this video and you had a little
47:36bit of fun too.
47:38This has been, you know, a little intro for compiling, linking, and executing in Linux,
47:44but also probably on a bunch of other systems.
47:46If you have these same tools and you're working inside of bash, thank you so much for watching
47:52this video.
47:53I'll see you in the next one.
47:54Hey everybody, thanks for watching this video again from the bottom of my heart.
48:04I really appreciate it.
48:05I do hope you did learn something and have some fun.
48:08If you could do me a please a small little favor, could you please subscribe and follow
48:14this channel or these videos or whatever it is you do on the current social media website
48:18that you're looking at right now?
48:20It would really mean the world to me and it'll help make more videos and grow this community.
48:24So we'll be able to do more videos, longer videos, better videos, or just I'll be able
48:29to keep making videos in general.
48:30So please do me a kindness and subscribe.
48:35You know, sometimes I'm sleeping in the middle of the night and I just wake up because I
48:38know somebody subscribed or followed.
48:40It just wakes me up and I get filled with joy.
48:42That's exactly what happens every single time.
48:45So you could do it as a nice favor to me or you could, you could troll me if you want
48:48to just wake me up in the middle of the night, just subscribe and then I'll, I'll just wake
48:51up.
48:52I promise that's what will happen.
48:54Also if you look at the middle of the screen right now, you should see a QR code which
48:58you can scan in order to go to the website, which I think is also named somewhere at the
49:02bottom of this video.
49:04And it'll take you to my main website where you can just kind of like see all the videos
49:07I published and the services and tutorials and things that I offer and all that good
49:12stuff.
49:13And uh, if you have a suggestion for a, uh, clarifications or errata or just future videos
49:21that you want to see, please leave a comment or if you just want to say, Hey, what's up,
49:25what's going on?
49:26You know, just send me a comment, whatever.
49:27I also wake up for those in the middle of the night.
49:29I get, I wake up in a cold sweat and I'm like, it would really, it would really mean the
49:34world to me.
49:35I would really appreciate it.
49:37Again, thank you so much for watching this video and um, enjoy the cool music as, as
49:43I fade into the darkness, which is coming for us all.
Recommended
0:46