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
Hey there! Let’s talk about compiling, linking and executing.
This video is going to focus on Linux, but I think it’ll be useful for all other operating systems that you might be working with at home.
I’m just going to basically explain like what does it mean to compile?
What does it mean to link a program?
What does it mean to execute a program?
If you don’t know that, then I think this video might be for you.
Okay, so for starters, imagine you have written a program.
I’m going to write the program here real fast.
I’m going to say, let’s do an empty file.
We’ll call it, let’s see, I got my answer key up here.
We’ll call it first.cpp.
First.cpp.
So I’m just like making a first piece of source code and I’m editing it in my little editor.
Notice how I’m not calling this the main.cpp program because a lot of beginners, they always
do main.
I want you to know that you don’t actually need a source code file called main.cpp.
Even if you’re writing a C++ program, all you really need is just one instance of the function main.
Beyond that also, you only need an instance of main if you’re writing a hybrid program
or some kind of program that is in pure C++ or that has the GCC libraries.
GCC libraries call on the main function as your entry point.
But if you’re writing pure assembly, the entry point is actually underscore start as a label.
We’re not really going to do this is not an assembly tutorial.
I just wanted you to know real fast.
Okay.
So I’m going to do like IO stream and then I’m going to say
STDC out and I’m just going to print, you know, hello from first.
Cpp.
STD.
And L.
Okay.
So we kind of have like a hello world program that’ll just
that’ll just basically print a message and do nothing else how do we compile
this program and execute it you know what does that actually even mean okay
so for starters the first thing that you probably do when you’re compiling a
program you’ve probably been told to do something like this let me get my little
command line open here you’ve probably been told to do something like this
let’s compile with GCC or G++ and we’ll say the standard for C++ that we’re
There are a bunch of other standards you can use.
We can turn on all warnings.
We can have the compiler be pedantic in terms of the warnings that it gives us.
You know, these two things at the end don’t really matter that much.
It’s really all about G++ and then STD C++ 23 to get it compiled.
And then we’ll give an input file.
We’ll say burst.cpp.
And then I think probably usually this is where most of you stop.
you stop we just kind of like specify a couple flags specify the input file then
hit enter and it compiles if we list the contents of the directory now this might
be familiar you’ll probably see something called a.out a.out notice how
the permissions look a little bit different for that for that file it’s
kind of printed in green in Ubuntu and if you look at the left side right here
you can see that it’s got a bunch of X’s in the permissions area that just means
That just means it has execute permission.
The compiler does that automatically for you,
but essentially this is like just, you know,
an executable binary file.
It’s not a text file.
It’s just something that the computer is meant to run.
We can look inside of it real fast.
There’s a program called XXD on Linux,
and it’ll sort of spill the contents of that file.
So if I say XXD and then I go a.out,
it just spills like a whole lot of like binary nonsense,
right?
you can see at the very beginning elf that’s uh i think that’s executable linking format
for linux and then there’s like a bunch of stuff and it just doesn’t make sense this is not text
that you can read here’s like some sort of a shared argument that it’s using and then some
register information i don’t even know what most of this is somewhere in here if you look
carefully you’ll probably see that message where is that oh yeah hello from c first.cpp so it’s
saying that the message we typed in the program is actually embedded in the executable.
That makes sense.
So hopefully I’ve proved to you that this is just an executable program.
How do you execute the program?
Well, usually you’ll have to type, you know, the full path to the program that you want to execute
because your custom program is not in the system library paths.
So your system’s not going to know where to find it automatically,
even if it’s in the current folder.
So you would usually do something like, you know, the full path to your executable,
path to your executable like a.out. But there’s a shortcut we can use in Linux. The dot directory
usually just means or it always means the current directory. So if we say dot and then a slash,
that’s the same thing as typing out the full path to the current directory. Then it’s really easy to
just type the name of the file that we compiled. Okay, so a.out. We executed it now. So that’s what
it means to execute. We just compiled without a lot of options. We’re going to try to do that better
We’re going to try to do that better in a second.
And we know that you need execute permissions.
Let me show you what happens if we take away execute permissions.
I’m going to use a command called chamad, like change the mode.
I’m going to say take away execute permissions from the a.out program.
Then if I list the directory again, notice how there are a bunch of X’s missing now
on the left side of that a.out permissions area.
Notice how it’s not in green anymore.
Depending on your system, it might not be green or gray or whatever.
be green or gray or whatever but just look at the permissions so now if i do a dot out
it’ll say permission denied maybe you’ve seen this before especially if you’re syncing with
the cloud that doesn’t carry execute permissions from one machine to another which happens to me
all the time we can reverse this with just the chamad you know plus x execute permissions
and then we can just execute it again the funny thing about execute permissions is
you can actually execute a bash program which is basically a text file without
without needing to execute a binary program so this is not a bash video I’ll
make other videos in the future on my channels for for bash but for now just
know that something you compile is not the only type of thing that you can
actually execute in fact in Linux under the hood a lot of system tools are just
written in bash or Python or something else that looks like a human readable
like a human readable language and they are not necessarily all compiled languages, you
know, like C++.
Okay, so now that we kind of know that we’re compiling a program, let’s be a little bit
better about specifying the name of the output file.
So I just want you to see one more flag here and while I’m doing that, I’m going to create
a script that will compile our program for us.
nano and I’ll just name a file I’ll name it compile and it’ll be our compiler program okay
so what we did before is it was I think G++ and then we named the standard is C++ 23 and then we
said wall wall just means give me all warnings which is good to help your you know to help you
write better code same thing for pedantic just helps you write better code it gives you more
but you know it’s pretty good. Okay so this is where we stopped last time so
I’m going to say now let me remove a dot out now instead of typing the command by
ourselves we can run the compile script. The language I’ve just written was was
well I haven’t really written in any particular language but if we use a
shell interpreter called bash which is actually what’s running in the terminal
right now it’ll interpret any command line that we give it as just something
line that we give it as just something that we want it to execute to the system. So if I type
bash to run the bash interpreter and I just specify the name of the script, it should compile the
program automatically by executing that one line that I put inside of it. See now I’ve got the a.out
again. If you rewind the video you’ll see the a.out wasn’t there when I originally ran it.
One more time just to prove it to you. bash compile. Now you can see that it’s back. Okay
Okay, so let’s make this compile program a little bit better instead of typing bash every single time
I think I just want to try to execute the compile program directly. I
Can do that pretty easily. There’s something in
In our script that we can put that will designate what interpreter we should use to run the program
So right now it’s just like some text and it doesn’t really mean anything
there’s something called the shebang which just means hash bang and
just provide the path to some interpreter. I’m going to type shebang and then a path
to the bash interpreter. If you don’t know where bash is on your system, you can go which bash.
And I guess mine is a user bin bash. I think I can get away with bin bash. Let me see.
Let me just type it out to make sure bin bash. Yeah. Okay. So if I type exit, I’m actually in
another version of bash, which is inside of bash. If I do exit, I should just kind of stay where I
Okay, so bin bash will work, user bin bash will also work.
I’m going to go back to editing this compiler program.
And now all I have to do, because the system is going to know what interpreter to use to execute my little script program here,
all I have to do is add execute permissions on it, and it should work.
So let me first remove a dot out.
And then let me try first to execute compile.
It should not work because I don’t have execute permissions.
Remember we talked about that before.
permissions. Remember we talked about that before? So if I do that it says
permission denied. Oh no! So I’m going to give it permission to execute. I’m going
to say change the mode plus X on compile. If I list the directory again notice how
it’s green notice how there’s a bunch of X’s everywhere. So now I should be able
to just say compile and then it seems to have worked and notice how it has an
because it’s a pain in the butt to type out all the compile commands every single time,
especially if you have a complicated compile command or many files that you want to compile
in the same shot. So, you know, of course, in another video, I’m going to talk about make files,
build systems, proper build systems are a lot better than just making a script. But for now,
let’s just make a script because I just want you to understand, you know, compiling, linking,
some of the surrounding ideas. Okay, so I’m going to, let’s see, I’m going to go nano compile.
And the next upgrade that I want to make is I want to specify the output file because it’s
kind of a bad idea to not specify the output file and just hope that the compiler gives you
the file that you wanted. I don’t know. So I’m going to do dash O main, and that’s going to give
So if I remove a.out just to keep my area clean, I can now do compile.
And if I list again, notice how I’ve got main.
So I can execute main and it’s the same program that we already had.
In fact, maybe sometimes just as a shortcut since this is a script,
what if I’m always hitting ls-la, I always want to list the folder after I compile.
I could just put that into the script.
The more commands you put into the script, the more convenient things can get, unless
I guess it gets too messy.
So I’m going to do, I’m going to list first, and I’m going to remove main, just to prove
to you that we’re doing this from scratch, and then I’ll do compile.
Notice how it compiles and then lists everything for me.
Seems like it didn’t actually turn the compile green.
That’s probably another flag that we have to stick in there.
Let me see if I can do that real fast.
If not, I won’t waste time.
This is not an LS video.
probably color or colors is what I got to do. Let me just do that one more time. Yeah, okay. So
that’s just a flag on LS. That’s not what this video is about. So now we’re compiling directly
to main, but here’s the problem though. We’re actually kind of sort of skipping over some
steps that we could be aware of. When we compile our program, we’re actually taking our source code
and we’re assembling it down to assembly code under the hood, and then we’re compiling it to
And then we’re compiling it to an object file, which is sort of just machine code.
It’s like a level lower than assembly.
And then as a last step, it’s getting linked into an executable named main.
So we’re skipping a ton of steps.
Really what’s supposed to be happening is every single source file.
Oh, whoops.
I’m on the wrong machine right now.
Okay.
Where’s that?
Oh, the machine that I’m recording on.
There we go.
CPP and we’re thinking that it just goes directly to main you know that works if you only have one source file or if you don’t want to have
fine-tuned control over your compiling
But I would like to do something a little bit more detailed. Let’s compile our
CPP source file into an object file. I’ll name it first.o and then we’ll link that into main
Why is that useful because later when we have multiple source files?
We might we might want to compile them all to object files first and then link them all together in one step into main
all together in one step into main this will be even more useful when you later realize that you
can use a build system that does kind of the same thing to save compilation time i won’t talk too
much about make files here but let me just tell you when you start using a build system
your compile time can be drastically reduced uh story i used to have a program a long time ago
that i wrote myself took like an hour to compile i would literally every little change i made i’d
have to get up and leave and go make a sandwich or something and then come back by the time it
back by the time it was finished compiling when i started using object files as intermediate steps
and upgraded to a build system i was using gnu make then my compile time went down to like a
minute each time so it was like so much more efficient anyway let’s do that let’s do that now
let’s let’s uh let’s nano our compile script and you just need another flag in here for the gcc
an object file and not actually produce a fully linked executable that you can execute that means
i should probably rename main to something else because main is supposed to be my executable at
least that’s what i’ve chosen i would like to have every object file be named similarly to the source
file it belongs to so i’m going to type the output now is going to be first.o and then the c flag
just says only compile don’t link this means of course that i’m not going to produce a program
a program that I can run. Let’s see, I’m gonna end up accidentally deleting the
compiler. Okay, but we can see what we’re doing. Okay, so compile and CPP for first.
So I’m gonna compile it and now notice how we have an object file and no main
and just the source file and its corresponding object file. So I can’t run
this program. I have to do the additional linking step now. So I’m gonna do
step now so I’m going to do compile and in my editor here let’s see where is my
command that I wanted to do for linking okay so I’m going to link with the the
GCC libraries I’m gonna use the G++ command I’m just gonna put a couple of
flags I’ll explain them real fast I’m gonna say that I want a 64-bit
executable which you probably actually don’t have to specify I’m going to
anyway and then I’m gonna tell it again that I want it to link up against the
23 libraries. This is important if multiple modules reference the C++ libraries. I’m not
even sure. I think I need it in both the compiling command and the linking command. I’m going to
leave it in both because it should work. Usually if you want to embed debugging symbols into your
program, you want to put a dash G there, which is a great idea. But we’re not going to talk about
debugging in this video. You can also specify the type of debugging symbols that you want.
symbols that you want I’m just gonna put that in here right now G dwarf 2 and
then there’s some new flags that you kind of need there’s one here called no
PI which I think is just an Ubuntu thing your program probably won’t execute if
you don’t do that just just put it in there and trust me there’s another one
that we have to put in now called no exec stack which basically prevents your
program from executing code sitting inside of the stack which is just sort
not get hacked hopefully it’s not bulletproof but you know it might help
and then i’m going to give it input files the input files for the linking
stage maybe i should do a comment linking stage
compile the c++ program or source or something
for the linking stage uh you want to you want to provide all of the object files
that you’re going to link together in this case we just have you know
file named first and then we can specify the output file as main if we didn’t
specify it would probably be a dot out like we saw before so if I got this
right let’s see if I did now this should compile the C++ source code file into
its corresponding object file and then scoop up the object file turn it into an
executable okay so my clear and then list and then I’m gonna remove the object
put star dot o which is a shell trick to basically say that that star dot o is
going to expand to really mean every file that ends in dot o so you know in
this case it’s actually just gonna end up being first dot o but it’s a trick you
can use to grab multiple object files or files of a certain type all in one
command so I’m gonna do that and then I’m gonna list again and then I’m gonna
compile and you can see that the compile command it created first dot o the
the object file, and it also created the executable main.
So if I execute main, the same thing should happen.
So great. We’re ready to move on to another step.
Let me remove star dot O.
I got to be so careful because I’m going to totally ruin this video by erasing the wrong thing.
Okay. Because this is, I should be in a Git repository, but I’m trying to be fast here.
Okay. So let’s nano compile.
Let’s create a second source file actually.
Okay. So let me get out of this.
Let me get out of this.
I want to create a second source file.
I’m going to call it second.c.
It’s going to be a pure C program.
And because we don’t have C++ libraries in C source files,
I’m just going to say, you know,
standard input output dot O for the header.
I’m going to make a function called void second probably.
And it doesn’t do anything except just sort of print
a little message.
So I’m going to say hello from second.
And then do a new line.
because we don’t have endels in c all right i think that’s probably going to work so then
let’s uh edit our compiling script and we’ll just add another line here to compile the c source
okay so i’m going to go gcc because it’s not c plus plus anymore and um i’m going to do all
warnings and pedantic again uh you don’t really need even need to worry about that and i’m going
need even need to worry about that and i’m going to give it an input of second.c because that’s the
file we just worked on and i’m going to tell it i only want you to compile with the dash c flag if i
forgot that on this one then i’ll probably end up with an object file a main and then an a.out that’s
only based on the c source code and that probably wouldn’t work because the second source code
doesn’t have a main function inside of it so um let’s see we’ll do output and it’s going to be
and then I think that should be okay you know something that I just realized I
should mention too is when you’re making hybrid programs like this or or
programs where there are just many many source files like I said before you
don’t need to have a source code named main.cpp you can name all your source
codes anything that you want the one thing that you really do need is a piece
of source code oh I’m editing inside the terminal suddenly sorry I got
Sorry, I got sidetracked.
I wanted to edit all this stuff inside of Genie because it’s a little bit better.
Maybe it doesn’t matter.
So notice how we have the main function inside of our first.cpp program,
and then there’s no main function inside of our other source codes.
For your program that uses the GCC libraries, you need exactly one instance of main.
so that means um you know you have to have at least one and if you have more than one it’s
probably not going to link so if i put a main inside of here probably not going to work
this also kind of means you can arrange your source code in a funny way you could
have a bunch of different pieces of source code that you could consider maybe libraries maybe
there are a bunch of classes or something that will help your program and you can release multiple
different executable files by just having different source code files each with their own
mains and linked separately against the object files that contain your library or classes or
whatever that’s just a little side tip but um anyway let’s see if i actually did this right
we’re linking this and i’m going to see if it works i get stuck inside of the terminal i start
thinking i’m going to edit everything inside there okay so it compiled well let me let me
remove the main and then just clear it again in the list okay so now I’m gonna
run the compiler and after the compiler ran everything seemed to be okay it
compiled first into its object file and it also compiled second into its object
file if I run the main executable notice how it only prints hello from first dot
CPP well what about the second one that we just added well what we need to do is
improve our linking stage notice how in the linking stage right here we just
stage right here we just only link first.o so we should also include the other object file that we
want i’m going to glob that in a second but uh let me show you compile now if we run main
what did i do wrong hold on let’s see here second.c oh i forgot to actually call the second
function so that’s cool just uh wrote a bunch of stuff for no reason at all let me uh let me have
have the main function call on the second function we’re going to encounter an issue here too the
second function is written in c and the first function uh its its source code is in c plus plus
and the thing is c plus plus uses something called name mangling i won’t go over it too much in this
video but it uses name mangling so you can overload functions you can have functions with the same
name but like different signatures right so it’ll go in there and it’ll mangle the name of a function
the name of a function according to its overload prototype so that we can actually call overloaded
functions but we don’t really have that in C so that means if we try to call a C function
from C++ we’re probably going to end up calling the wrong name because there’s the expectation
that the other module has name mangling enabled but it’s not because it’s C so I just have
to do this little block extern C and just name all the stuff that is not going to be
name mangled i’ll just provide the prototypes of all the functions that i want to be able to call
so just void second if you look here again we just have void second and now when um when the c plus
plus module calls on second it won’t expect name mangling so it should work so we’re calling it
and then we linked it now everything i think should be okay i’m going to compile one more time
and then i’m going to run the main program and now you see that we have both calls in there
Awesome. Let’s give this script a little bit more output though. This is kind of
precarious. What if something failed inside of there? Maybe you don’t want the script to continue
if something fails and maybe you kind of want to know where the script is at while it’s compiling.
So I’m going to upgrade it just a little bit. So compile the source. I’m just going to say echo
compiling the, you know, compiling burst.cbp. And then here I’ll just do another echo and I’ll say
and then here I’ll say echo linking the executable or the binary aka the binary
I don’t even know if that’s the proper term I just like to call these things binaries because
they’re not in text anyway so if we do another I’m going to do I’m going to do a compound
command line here I’m going to go clear and compile so that I don’t have to continue to
continue to clear every single time. Now notice how it prints what it’s doing. So if it kind of
failed somewhere along the way, or if it took a long time, you’d kind of know what’s going on.
Now let’s use a little bash trick, because remember this is a bash script. It’s not just
going to only execute actual commands. I’m going to say or exit with the return code of the previous
We’re going to try to execute G++.
If G++ fails for some reason, then the whole program or the whole script is going to exit
and it’s going to exit with the return code mentioned here.
I think it’s not going to print it.
I’ll show you how to print it in a second, but we can actually make a function called
die up here in a second, which might be better.
So I’m going to do the same thing over here.
I’m going to say or exit with the return code of the GCC command and then here or exit with
And then here or exit with the return code of the linking command.
We’ll do it one more time here.
Notice how it seems to work.
So now let’s make the first C++ program not actually compile.
I’ll put a bunch of Zs there.
And so if we try to compile it again, it should just quit right after it tries to compile the C++ program.
So that saves you some time and helps draw your attention to what went wrong.
The C++ compiler is saying hey, there’s like a type there. It doesn’t show the exit code though. So let’s make a function in bash
That will die and print an exit code. So I’m going to say do die or maybe just die by itself something like that
Functions whoops. I’m in the wrong language
Let me go back to the compiler
make a function in bash
Called die and it’ll take one argument and the argument will just be the exit code
that we want it to alert us of.
So I’m going to say echo,
I’m going to say we are dying now.
Previous exits code was,
maybe I’ll do a little variable here,
I’ll do a local variable.
Exit code is going to be the first incoming argument.
This is a bash program, not C++.
And then here I can just say exit code.
Okay.
So then after writing all that,
then after writing all that I can say or die and then just you know so I’m calling
a function and the first argument I’m giving it is the actual exit code of the
thing that died so we’re just kind of like using indirection to print a pretty
pretty message to get a little bit more information so we’ll try that one more
time notice how it says we are dying now previous exit code was one so that just
that just tells us that gcc exited with the exit code of one not super useful but it’s nice to see
could be something else entirely like i don’t know maybe uh maybe we misname the first source
code file let’s see if the code changes i think it probably will nope maybe this exit code was one
no such file or directory all right exit code am i doing something wrong i guess i’m all right
Alright, okay, so let’s just move on.
I wanted it to be fancier.
We could also upgrade a little bit if we wanted to.
Instead of echoing, I could make a function called log here,
and I could just say that the log command just echoes,
and maybe we’ll put a cute label, and we’ll say, you know,
my super cool program compiler, or how about super cool compiler?
And then just do the $1, because that’s the first incoming argument that you get.
the first incoming argument that you get now I can type log instead of echo so
that’s nice and why would you want to do this well now it’s gonna go inside of a
function which means later on you can start adding extra commands like logger
a log to the system daemon oh no so this would write to your system log in
addition to printing something you could do anything you want you know it’s just
like a regular program adding more power by sticking everything inside of
functions I’m gonna put log here and let’s see if it works
let’s see if it works pile okay nice notice how we have a little message with a little header which
seems pretty cool maybe instead of echoing here let’s make a complaint function let’s do a
complain and same thing as the echo function but maybe I think there’s a flag where you can tell
echo to print to standard error instead of standard output but I’m just going to do it the easy way
it the easy way I’m going to say one goes to two so that the the first pipe standard output is going
to be redirected into the second pipe standard error so now I can use complain instead of um
echo down here complain before I die oh I forgot to actually
or die I forgot to actually exit so that means it was oh yeah it’s still compiling everything
Let me fix that.
So in the actual die function, hopefully you were at home cringing along with me.
Let’s see, let’s exit with the actual exit code that was specified just to make things
a little bit more automatable.
Yeah, now it stops right away.
And the complainer is like, you know, pretty cool.
Let me fix the first source code real fast.
And we’ll say that the second source code is broken.
We’ll put a bunch of Z’s there.
So now the first one compiles, the second one, it breaks.
And we’re starting to, you know, get a hang for a compiler script that’s a lot more sophisticated
than just typing things on the command line, right?
You can take this as far as you want.
The next step above this is using a proper build engine.
So let’s see.
Well, let’s do another source code.
an assembly source code. So we’ll say third dot ASM and it’s just going to be Yasm. This is not
a Yasm or assembly tutorial. I’m just showing you how to, you know, build a, you know, compile,
link and execute. We’ll make a data section so I can just stick some defines in here real fast.
I’ll say system write is going to be equal to a one and then the file descriptor for std out
Do you ever feel like red jumps out at you on the screen?
Does that mean I have eye problems?
I am kind of old now.
I don’t know.
Hello from third dot ASM and I’ll just put a little new line there.
So I don’t have to do it elsewhere.
And then I’ll have to specify the magic message length because this is not a sophisticated program that can loop through the message looking for a null terminator.
And then I’ll do a text section, which is where your code goes.
which is where your code goes and you know in hybrid programs if I have this
third function which is marked as a which is marked using a label and then
in return statement I still have to mark a third as being accessible outside
this module so I’m going to do the global statement and then I’m just going
to do really quickly I’m going to move something into RAX it’s going to be
system right so basically I would like to write something somewhere to some
some file descriptor its first argument wants where I want to write it so I’m
gonna write it to the standard output instead of an actual file and then I’m
gonna move RSI the next argument is going to be the actual message or a
pointer to the message that I defined above and you know check out my other
videos if you want to learn assembly that’s this is not this is not an
assembly learning video I’m just kind of doing it real fast in case you’re
probably unless I ruined it somehow let’s start compiling or assembling the
assembly program so just one more we’ll say assemble the assembly source or
module or whatever you want to call it we’re going to use the ASM to do that
G dwarf 2 is the debugging symbols not necessary in this video the format is I
want a 64-bit executable that’s how you do it in Yasm and I’m going to give it
and I’m going to give it an input of just the third assembly source file.
And I want it to output the third object file.
I got to remind myself to glob line 36 after this or die with the exit code.
Okay. So now this might work.
Oh, I got to put, I got to put third dot O in the linking stage.
I’m going to do clear and I’m going to go here and compile.
and compile.
Unrecognized debug format, dward.
Why didn’t you stop me?
It’s g dwarf.
Let’s see, what did I do wrong?
g dwarf, oh, dwarf dash two.
Oh, it’s not even g, it’s just dwarf two.
Sorry.
And I got to put a dash, I think, for EASM.
Yeah, okay.
Let’s see if that works.
What did I do wrong?
Oh no no, Yasm doesn’t need the dash, but GCC wants the dash.
Okay, I had it right the first time.
Doop doop doop, okay, so it probably linked.
Let me run it and see what happened.
Hello from the second, oh, we still have to actually call it from the C++ module.
So now we’ll do the same thing.
We’ll say that the assembly module, it’s not going to use name mangling.
use name mangling there’s no overloads so we’re going to put it into the extern C
block and we’ll just say you know void third how do we know that’s that it’s a
void function with no arguments well you know that’s just a convenience that
happens under the hood with C and C++ in assembly it’s just a label that you’re
jumping to and returning from void means return type all that means is I’m
trying my best not to make this a C++ or an assembly tutorial but all that
All that means is there is no return value getting returned.
That would be the RAX register if I wanted to move something in there.
And then similarly, if I wanted to catch arguments, I’d be grabbing from other registers.
So I’m not grabbing any register, any arguments from registers, and I’m not sending a value back with RAX or XMM0 or anything.
So that just means it’s void and no arguments.
So let’s do it again.
Compile it and then run main.
then run main hello from second oh i sure put in the name mingling thing but i forgot to call it
let me do that third so now one more time compile and then run it
what’s happening in here
third i guess i should probably open this up now third asm so global third and then third oh
important part of a system call you got to put the keyword or the instruction system call in there
one more time i hope you’re laughing and cringing at home trying to make this more fun maybe i don’t
know okay so now it’s all it’s all working notice how we have a bunch of different modules in
different languages we’re compiling them all to object files first and we’re uh we’re linking
them all into one executable and we’re doing this in a really really easy way by using a script
script instead of typing all the commands out manually. Pretty convenient, right? Let me give
you another upgrade real fast here. So this is kind of irritating. I have like all of these object
files that I have to specify. What if I forget to specify some? Since this line 36 is just going to
be something that goes directly, you know, to the shell as a command line. This is not like bash
wildcards I can say star dot o meaning I’m going to give every object file to
the linker as an input file and that should just work automatically keep in
mind if you want to use an advanced strategy like I mentioned earlier where
you have multiple different object files and they’ve each got their own main
functions because they’re each going to be their own separate executables then
globbing all object files probably not going to work you might want to organize
files or stick the object files for executables in like their own folders or
something like that we’re not going to go that far but I just want you to know
so let’s do that again real fast here we’ll say clear and compile and I’ll
just try to like run main assuming that the compiler works yes so it worked
everything compiled by the way this and end that means only execute the next
So if I, you know, clear is obviously going to succeed and then compile, it might succeed
or not.
Then I’m going to try to run main.
Let me actually, let me actually do that real fast.
Let me show you what I’m talking about.
I’m going to remove main.
You’re okay.
So then I’m going to do clear and compile and main.
So obviously if everything works, then everything works.
But if I do that again, where something is not actually going to compile, like maybe
the second the c source program is not going to compile by putting some z’s in there then the
dot main or yeah that part shouldn’t get executed so it won’t try to execute our program
notice how it just stops so that’s convenient if i do it again though with a different command
if i chain instead of between the commands instead of putting and end to say one must execute before
semicolon then they will all execute no matter whether whether they succeeded or not maybe in
certain things you’re doing you actually want that behavior but in this case it’s going to be bad
watch what happens it fails to compile and then at the end it also tries to run a program that
doesn’t exist because it never got linked because the script stopped so i’m just letting you know
you have lots of different options when you’re writing these scripts we do it again and then let
And I think, well, maybe there’s one other thing I want you to know.
Sometimes these lines get really, really long, right?
And it’s messy and irritating.
At least for me.
Maybe if it goes off the side of the screen for you, it’s like, oh, that’s awful.
You can actually just separate a command across multiple lines with a backslash.
Or sorry, I think that is called a forward slash.
It’s the drive slash.
I don’t remember forward slash backslash.
I just say web slash and drive slash.
So I’m going to do G++ and do a little slash.
Then I can put the whole rest of the thing on the next line.
If I wanted to, I could start separating these out by category.
So maybe I’m like, oh, I don’t know.
Maybe M64 on its own line.
And then I’ll keep these other two on their own line.
And maybe these two on their own line.
You know, it’s up to you how you organize this.
And then I’ll say, give me all the input files on its own line.
And then the output file on another line.
on another line you could even put the or bars for the die part on its own line I
guess I’ll just try that and now let’s just make sure that this still compiles
let me remove star dot o and remove main it’s not there okay so clear and then
list and then two showed up somewhere oh my mistake I probably should have caught
some file got saved as a two I should have noticed okay so let’s do clear and
compile and I just run main okay so now everything works notice how even though
everything was on a different line it all just seemed to work cool what about
something here on line 38 we’re using the C++23 standard but we’re also doing
that up here and we’re also using the all warnings flag up here and down here
and pedantic also and even the linker like what if I want to put all warnings
into the linker we could make variables in bash and by the way this is totally
starting to become a job for for mark for make files but for now we’re just
stay in bash so I can make a variable in bash I can say maybe GCC flags name it something and I’ll
just put equals whatever and I’ll just type the stuff that I know is going to appear in every
single invocation of G++ or GCC so I’m going to go wall because I want to put that everywhere
and I’m going to say C++23 because I’m going to put that everywhere and probably pedantic I’m
I’m not going to put the dash C flag everywhere because notice how I’m only putting that on the compiler commands when I just want to make an object file.
But when I’m linking, there’s no dash C because that means you got to actually link.
So let’s see.
We got G dwarf two, G dwarf two.
Maybe I could put G dwarf two there and see if it still works.
So I want debugging symbols and I want them to be in dwarf dash two format, which honestly is the default format.
which honestly is the default format if you just put dash G.
Let’s see if I can get away with that for now.
So I’m going to go GCC flags.
And here I can get rid of C++23 and wall and pedantic.
And I’ll just insert instead GCC flags.
And if you want to be extra careful,
you can put these little braces around the variable name of that.
Probably should be fine.
But because I don’t want to type that again,
I’m just going to copy paste it into the other command.
So wall pedantic, just paste that.
Just paste that and then I’m going to leave it alone for Yasm because Yasm is different than GCC.
And then here I’m going to do another line where I just kind of put the flags.
That means I can get rid of those two flags.
And then wall and pedantic are going to show up.
No pi is kind of a linker only thing.
So is the no exec stack.
I think probably that will be okay.
Let’s see if I got that right or wrong.
Oh no.
Unrecognized command line option.
Wall pedantic GCC.
Oh, I guess because I quoted it.
I shouldn’t have done that.
Because I quoted it, the command line thought that that was all one argument.
I should have remembered.
So I’m going to unquote it right now.
This should help the command line understand that all of the things inside of that variable,
the GCC flags, are separate arguments instead of just one argument.
Let me try one more time.
Yay, it seems to work.
Okay, we got a warning here.
Command line option C++23 is valid for object,
but not for C.
Okay, so basically I made the mistake
of trying to send a C++ object,
which is this right here, or sorry,
a C++ argument, which is this right here,
into a C compiling command.
Not great.
So what if I did this?
I’ll do C flags, and it will contain only flags for C,
for C and then I’ll have another one for C++ flags
which will only contain things that are for C++ compiling so the regular C compiling can take
wall and pedantic okay and then it can take the G dwarf 2 okay the C++ flags it can take everything
except for um sorry the C++ flags can actually take all of the C flags and then additionally the
and then additionally the the c++ 23 flag so i could embed a variable inside of itself i could
say c flags or sorry i could embed a variable inside of another variable so that i can just
steal all of the flags from the c flags variable stick them also in the c++ flags variable and
then just add one more at the end of that hopefully that works i think it probably will
here and then use the C flags here just for compiling I could probably name the
variable better put the word compile in there or something but I don’t really
want to I think some people put CC to mean like C compiling but I don’t know I
just want to do it this way so we have that and then we’re gonna do CPP flags
like this we could also organize the variables differently so that the we
We could have some linker flags, which basically would probably have all of this stuff minus dash C.
And then we could have linking flags.
No, we can have linking flags that has all that stuff minus the C.
And then we’ll have like some compiler flags that has the linking flags plus the dash C.
But I don’t really want to spend a bunch of time on this now.
For now, we’ll just say we have C flags and CPP flags.
And we’ll just manually add that dash C.
But of course, keep in mind, you can organize these things any way you want.
and organize these things any way you want.
So I’m gonna do,
well, this might actually work.
I’m getting distracted.
I think it works.
Okay.
Let me just double check by removing all the object files.
There’s a two in there still.
Let’s debug that if it happens again,
then I’m gonna remove main.
Okay, then I’m just gonna list everything real fast.
Okay, let’s see.
Is there gonna be a two in there now?
Is there going to be a two in there now?
No, is that only when it fails?
Only when something fails to compile?
I mean, it’s working, so that’s good news.
You know what?
Leave a comment somewhere letting me know
where you think I went wrong with this two,
and maybe I’ll release another video
or something in the future,
at some distant point in the future.
But yeah, anyway, I hope you learned a lot of stuff
in this video and you had a little bit of fun too.
had a little bit of fun too. This has been, you know, a little intro for compiling, linking
and executing in Linux, but also probably on a bunch of other systems if you have these
same tools, and you’re working inside of bash. Thank you so much for watching this video.
I’ll see you in the next one.
of my heart i really appreciate it i do hope you did learn something and have some fun if you could
do me a please a small little favor could you please subscribe and follow this channel or these
videos or whatever it is you do on the current social media website that you’re looking at right
now it would really mean the world to me and it’ll help make more videos and grow this community so
we’ll be able to do more videos longer videos better videos or just i’ll be able to keep making
Do me a kindness and and subscribe. You know sometimes I’m sleeping in the middle of the night
And I just wake up because I know somebody subscribed or followed it just wakes me up and I get filled with joy
That’s exactly what happens every single time
So you could do it as a nice favor to me or you could you could troll me if you want to just wake me up in the middle
And I just subscribe and then I’ll just wake up. I promise that’s what will happen
Also, uh, if you look at the middle of the screen right now
You should see a QR code which you can scan in order to go to the website
go to the website which i think is also named somewhere at the bottom of this video and it’ll
take you to my main website where you can just kind of like see all the videos i published and
the services and tutorials and things that i offer and all that good stuff and uh
if you have a suggestion for uh uh clarifications or errata or just future videos that you want to
see please leave a comment or if you just want to say hey what’s up what’s going on you know just
wake up for those in the middle of the night i get i wake up in a cold sweat and i’m like
it would really it really mean the world to me i would really appreciate it so again thank you so
much for watching this video and um enjoy the cool music as as i fade into the darkness which is
coming for us all
Thank you.