<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Linux programming Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/linux-programming/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/linux-programming/</link>
	<description></description>
	<lastBuildDate>Mon, 08 Sep 2025 07:44:42 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>Linux programming Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/linux-programming/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Master Command Line Arguments in x86-64 Assembly with YASM</title>
		<link>https://www.NeuralLantern.com/master-command-line-arguments-in-x86-64-assembly-with-yasm/</link>
					<comments>https://www.NeuralLantern.com/master-command-line-arguments-in-x86-64-assembly-with-yasm/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 08 Sep 2025 07:44:41 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[argc argv]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[assembly tutorial]]></category>
		<category><![CDATA[command line arguments]]></category>
		<category><![CDATA[Linux programming]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[stack pointer]]></category>
		<category><![CDATA[system calls]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=221</guid>

					<description><![CDATA[<p>Learn to access command line arguments in x86-64 YASM assembly on Linux. Master stack handling and argv looping in this clear, practical tutorial!</p>
<p>The post <a href="https://www.NeuralLantern.com/master-command-line-arguments-in-x86-64-assembly-with-yasm/">Master Command Line Arguments in x86-64 Assembly with YASM</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Master Command Line Arguments in x86-64 Assembly with YASM" width="1380" height="776" src="https://www.youtube.com/embed/hRl3hKvHiyM?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p>Want to master command line arguments in pure x86-64 YASM assembly on Linux? This video breaks down how to access argc and argv directly from the stack, without GCC libraries. Follow along as we write a program to loop through and print user-provided arguments, complete with a visual stack explanation. Whether you&#8217;re new to assembly or leveling up, this tutorial offers clear, practical insights for coding low-level programs. Check out my other videos for more assembly tips, and subscribe for more coding goodness!</p>



<p>Introduction to Command Line Arguments 00:00:00<br>Target Architecture and Assumptions 00:00:04<br>Recap of Command Line Arguments 00:00:56<br>Accessing Arguments in Pure Assembly 00:01:47<br>Program Structure and Data Section 00:02:19<br>Stack Pointer and Argument Count 00:04:16<br>Accessing Argument Vector (argv) 00:07:14<br>Looping Through Arguments 00:12:16<br>Incrementing to Next Argument 00:16:45<br>Visualizing the Stack 00:19:24<br>Running the Program with Arguments 00:21:56<br>Conclusion and Call to Subscribe 00:23:48</p>



<p>Thanks for watching!</p>



<p>Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>
</ul>



<p>Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



<li>Leaving a comment or suggestion</li>



<li>Subscribing to our Blog</li>



<li>Watching the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p>hey there let&#8217;s talk about accepting command line arguments in a pure assembly program</p>



<p>written for x86-64 aka amd yasm assembly</p>



<p>on linux anyway so uh there are going to be a lot of topics that i that i sort of mention in this</p>



<p>video that are not actually covered in this video so if you feel yourself getting lost like for</p>



<p>program in assembly in the first place.</p>



<p>If you don&#8217;t know what command line arguments are,</p>



<p>if you don&#8217;t know the terminal, things like that,</p>



<p>you probably want to check out my previous videos</p>



<p>because they will be explained in detail.</p>



<p>For now, I&#8217;m just going to assume you know how to do all this basic stuff.</p>



<p>And the only thing that I need to show you</p>



<p>is how to write a program in Yasm assembly that can</p>



<p>grab your command line arguments.</p>



<p>So, you know, just to do a quick recap of what I&#8217;m even talking about.</p>



<p>echo we can launch it with no arguments it doesn&#8217;t do anything but we can give</p>



<p>it some arguments we can say hello and then we can say goodbye and that&#8217;s two</p>



<p>arguments that I&#8217;m giving to echo echo will grab on to the arguments it was</p>



<p>given it&#8217;s actually going to receive three arguments the index zero argument</p>



<p>being the name of itself its own program and then the next two arguments being</p>



<p>the stuff that I typed after the program but you can see that it somehow figured</p>



<p>two arguments as strings and print them right so imagine you have an assembly</p>



<p>program and you want the user to be able to launch your program and add</p>



<p>arguments at the end of the command line and have your program behave in some</p>



<p>certain way according to what the user wants the question then becomes how do</p>



<p>you access those arguments it&#8217;s a little bit easier in GCC look at my other video</p>



<p>that I posted about the topic for GCC linked assembly programs it&#8217;s a little</p>



<p>You pretty much just look at RDI and RSI to get your argc and your character pointer array.</p>



<p>When you&#8217;re dealing with pure assembly though, the GCC libraries don&#8217;t bundle up the command</p>



<p>line arguments in a really convenient way for you.</p>



<p>You&#8217;ve got to look for them elsewhere.</p>



<p>It&#8217;s honestly not that much harder.</p>



<p>It&#8217;s just a little, feels a little weird.</p>



<p>I think at least for me, I was hoping to see the arguments in a register and I did not.</p>



<p>But anyway, so here&#8217;s my program.</p>



<p>Again, this is not an assembly programming video.</p>



<p>an assembly programming video if you need to know how to program assembly see my other videos for</p>



<p>now i&#8217;m just going to quickly go over it i&#8217;ve got my data section in yasm i am printing out a couple</p>



<p>of strings or i i have a couple of strings defined i&#8217;m saying like the module has started i&#8217;m going</p>



<p>to begin printing arguments i have some system call codes so that i can write just to standard</p>



<p>output and so that i can exit the program this is pure assembly so we don&#8217;t actually return from</p>



<p>we just get jumped into and then we call exit normally and then here&#8217;s the standard output pipe</p>



<p>again another video covers that and then i&#8217;ve got the exit success code again another video covers</p>



<p>that but basically you know exiting zero is usually what you do for success then i&#8217;ve got the</p>



<p>text section which holds all my assembly instructions and this is my entry point we don&#8217;t</p>



<p>really need to uh push and pop these registers because we&#8217;re just going to exit the program when</p>



<p>it&#8217;s totally fine we would need to preserve Kali saved registers if we were</p>



<p>jumping into a main function or if this was a different function that was being</p>



<p>jumped into but we&#8217;re not so we don&#8217;t have to anyway here&#8217;s my entry point the</p>



<p>underscore start for a pure assembly program and then I&#8217;m going to grab the</p>



<p>incoming arguments when you&#8217;re linking with GCC which we&#8217;re not doing here in</p>



<p>this video then the arguments come in very easily they just come in argc</p>



<p>up in RDI and arg sorry argc comes into RDI and the character pointer array comes in as RSI so you</p>



<p>can imagine in GCC this would be integer main and then integer argc character pointer array argv</p>



<p>right hopefully you&#8217;ve done this in a higher level language so you have a better idea of what I&#8217;m</p>



<p>talking about if not I guess that&#8217;s okay it will still allow you to grab even if you don&#8217;t</p>



<p>it will still allow you to grab,</p>



<p>even if you don&#8217;t understand this in C++.</p>



<p>So argc is actually the stack pointer.</p>



<p>The stack pointer register, RSP,</p>



<p>always tells you where the top of the stack is,</p>



<p>like what&#8217;s the last piece of data</p>



<p>that we actually have sitting on the stack.</p>



<p>So that&#8217;s gonna be the count of arguments.</p>



<p>argc is always gonna be the stack pointer.</p>



<p>So if you dereference the stack pointer register,</p>



<p>pointer register then what you&#8217;re doing is you know the stack pointer register</p>



<p>actually points to memory locations within the stack so if the memory</p>



<p>location that it&#8217;s looking at is where argc is stored then you dereference it</p>



<p>with the brackets and you&#8217;ll actually get argc so right away I&#8217;m just gonna</p>



<p>steal argc off the top of the stack and this is a good idea to do right away</p>



<p>because if you start doing other stuff you might end up modifying the stack</p>



<p>especially if you start calling functions and things like that and and</p>



<p>and and these these pieces of information will be lost or they&#8217;ll be a lot harder to find so right</p>



<p>away i&#8217;m just going to say argc goes directly into r12 and i have a little comment reminder</p>



<p>for myself at the top saying r12 is now argc same thing for uh r13 i&#8217;m going to say that&#8217;s a pointer</p>



<p>to my character pointer array i talked about this in depth on my other video where we talked about</p>



<p>an assembly program but basically if you look at uh if you look at argv right here it&#8217;s not just a</p>



<p>pointer and it&#8217;s not just an array it&#8217;s a pointer to an array and if you recall in higher level</p>



<p>languages or just i guess anything an array itself is a pointer because argv the symbol let&#8217;s say we</p>



<p>were in a higher level language argv can only point to one thing so it&#8217;s going to point to the very</p>



<p>but the beginning of your array is not going to be one string like one argument that the user gave</p>



<p>you because the user could give you many arguments instead it&#8217;s an array of pointers</p>



<p>so it&#8217;s a character pointer array meaning argv is a pointer to a pointer</p>



<p>the first item in any array is what is pointed to by the symbol so like imagine if we had just</p>



<p>had just for the sake of argument imagine if we had an integer array let&#8217;s say we had like 500</p>



<p>integers in like a c++ program so the symbol a is what i&#8217;m trying to say the symbol a</p>



<p>is really a pointer that points to the first integer in that array and then later of course</p>



<p>you can dereference that pointer with some sort of an index to get the index 5 integer or the</p>



<p>the array and a pointer to the first item.</p>



<p>Or I guess you could say it&#8217;s both a pointer to the array</p>



<p>and a pointer to the first item,</p>



<p>because it&#8217;s kind of the same thing.</p>



<p>So that means if we grab argv,</p>



<p>then we&#8217;ll be grabbing a pointer.</p>



<p>If we then dereference that pointer,</p>



<p>it would tell us where another pointer is.</p>



<p>And then that other pointer would point to a string</p>



<p>representing the first argument.</p>



<p>first argument so we&#8217;ll explain that more as i do this loop i&#8217;m going to i&#8217;m going to show you</p>



<p>some code that actually loops through all the arguments anyway the thing about where to find</p>



<p>that argv is it&#8217;s just the next pointer or i guess it&#8217;s the it&#8217;s the next item that the stack holds</p>



<p>so remember i told you that rsp when this when this function comes in rsp is a pointer to wherever</p>



<p>we&#8217;re we&#8217;re looking what memory location the top of the stack is so if we dereference that we get</p>



<p>So if we dereference that, we get argc.</p>



<p>Well, we just need to go find the next previous item in the stack</p>



<p>and then dereference that so that we can get the first pointer or argv,</p>



<p>you know, the first pointer to a pointer,</p>



<p>or the first double pointer in your array of pointer to pointers.</p>



<p>Why am I doing plus eight?</p>



<p>This might actually make sense as is,</p>



<p>sense as is but it&#8217;s important to understand that when you add stuff to the stack you&#8217;re actually</p>



<p>decreasing the memory locations uh that you are pointing to so like if i if i increase a stack</p>



<p>this is not a stack video there&#8217;s other videos that i have for that um if we add something to</p>



<p>the top of a stack you imagine the stack growing visually in a in a vertical direction like it&#8217;s</p>



<p>growing up right but inside of the computer the memory locations are actually going down</p>



<p>If we&#8217;re looking at the stack pointer and we&#8217;re adding eight, what it&#8217;s saying is that</p>



<p>we&#8217;re looking back into memory that was already added for the stack.</p>



<p>So that&#8217;s like if we grew the stack, that would be subtracting from the memory location.</p>



<p>So like looking further up in the stack would be subtracting from the memory location because</p>



<p>the stack grows upward in the abstract, but downward in memory locations.</p>



<p>So that means if we add eight, that means we&#8217;re going in the other direction.</p>



<p>that means we&#8217;re going in the other direction.</p>



<p>We&#8217;re looking downward into the stack.</p>



<p>So what that means is that the top two items on the stack</p>



<p>are first argc and then second, that first pointer,</p>



<p>you know, the argv argument.</p>



<p>And that&#8217;s how we access it with plus eight.</p>



<p>Why is it a plus eight instead of plus something else?</p>



<p>Because all pointers on 64-bit systems are 64-bit integers</p>



<p>and 64-bit integers are eight bytes.</p>



<p>If we&#8217;re talking about eight bytes, sorry,</p>



<p>if we&#8217;re talking about eight bits per byte.</p>



<p>So this is just how you access it.</p>



<p>What about this other thing going on here?</p>



<p>What&#8217;s this LEA instruction?</p>



<p>Usually you see a move instruction, right?</p>



<p>So we basically want to look at the stack pointer,</p>



<p>but look one level lower.</p>



<p>And each item on the stack is also going to be eight bytes.</p>



<p>We want to look one item lower on the stack,</p>



<p>and then we want to dereference it.</p>



<p>but we don&#8217;t want to dereference and store the dereferenced value we still want to grab the</p>



<p>actual pointer the thing is I shouldn&#8217;t have said dereference in the first place these are dereferencing</p>



<p>brackets right so like up here on line 46 when you put brackets around rsp it dereferences whatever</p>



<p>value rsp holds so therefore when you put brackets around this you kind of expect that you&#8217;re going</p>



<p>to be dereferencing right but we don&#8217;t want to dereference we just want the original address that</p>



<p>showing us. So the problem with this is that when we say RSP plus eight,</p>



<p>we can&#8217;t actually do a mathematical formula unless we&#8217;re inside of brackets, which means we</p>



<p>can&#8217;t do them in a move instruction without accidentally dereferencing and losing a pointer</p>



<p>to the actual array of pointers. If we dereferenced like this, we would just end up with,</p>



<p>you know, a pointer to one string. So the way around this is instead of using the move instruction,</p>



<p>instead of using the move instruction we use the lea instruction the lea instruction allows us to</p>



<p>put a formula inside of brackets so that the assembler won&#8217;t get confused but then it won&#8217;t</p>



<p>put the dereferenced value into r13 it&#8217;ll just put the actual value of whatever we see</p>



<p>with that formula meaning it&#8217;ll give us the memory location of the item sitting one under the top of</p>



<p>this at this point r13 is now the memory location of the of the item sitting one underneath the top</p>



<p>of the stack and then we can de-reference that later in order to look at all of our arguments</p>



<p>so the next part of our code is just going to print an intro message so there&#8217;s like this intro</p>



<p>string up here hello and um i&#8217;m basically going to call a custom function that i made to just sort of</p>



<p>Printing. Don&#8217;t worry about this code right here.</p>



<p>This is not the point of the video.</p>



<p>I just made a custom function that just kind of helped me print.</p>



<p>Don&#8217;t worry about that.</p>



<p>The real meat of this video is that we&#8217;re going to loop through all arguments</p>



<p>and just print every single argument that the user provided.</p>



<p>So you can imagine this is maybe the top of a while loop here.</p>



<p>Notice how, oh, I forgot to replace main with start with start.</p>



<p>I had another version of this program that used main.</p>



<p>pretend we have this is called the main loop instead of the the main functions</p>



<p>loop anyway so main loop initialize first thing I&#8217;m going to do is I&#8217;m going</p>



<p>to initialize the loop by saying we&#8217;re looking at index 0 okay no problem so</p>



<p>we&#8217;re looking at index 0 we want to do that we want to look at all the</p>



<p>arguments you know we want to look at index 0 and index 1 and index 2 and we</p>



<p>just want to keep going until we&#8217;re out of arguments remember that we also have</p>



<p>the number of arguments coming from argc now sitting in r12. So that means if we have a</p>



<p>counter that starts at zero with r14 and then we know how many arguments there are in r12,</p>



<p>we should be able to know when the loop stops. So just a reminder that, you know, the loops here,</p>



<p>the arrays here are zero-based indexed or zero-index based, which means, for example,</p>



<p>that&#8217;s three arguments but the array of arguments is going to have indexes zero and one and two</p>



<p>which means the last valid index is going to be two or the size minus one think about that if you</p>



<p>have five arguments the last index that is valid is going to be four size minus one because it&#8217;s</p>



<p>zero based so we can use that logic to figure out if we&#8217;re done so we start off with an index of zero</p>



<p>here and then at the top of the loop i&#8217;m just going to quickly ask are we done how do we know</p>



<p>are we done how do we know if we&#8217;re done we compare the current index we&#8217;re</p>



<p>looking at with the count and we say if the current index we&#8217;re looking at is</p>



<p>greater than or equal to the count then we know we&#8217;re already done we don&#8217;t need</p>



<p>to look at any more indexes why am I saying is is the index greater than or</p>



<p>equal to the count because remember if we have five arguments then the last</p>



<p>valid index is four therefore if we find ourselves looking at index five we know</p>



<p>already done five is equal to or greater than five, but four is not equal to or greater</p>



<p>than five.</p>



<p>So that&#8217;s why I&#8217;m using this logic here.</p>



<p>I&#8217;m saying if we&#8217;re done, then jump to the done label.</p>



<p>Basically what&#8217;s the done label?</p>



<p>It&#8217;s main loop done.</p>



<p>All it really does is it says goodbye and then it just exits the program with a system</p>



<p>call.</p>



<p>Again, I have other videos that explain system calls and all that stuff.</p>



<p>So if we&#8217;re done, we, uh, we jumped down to the done area, but if not, then execute</p>



<p>to the done area but if not then execution will fall through to this next loop or sorry this next</p>



<p>label which is not necessarily we don&#8217;t really need a label there but i i like to put i like to</p>



<p>put it there just to help myself remember this is what it looks like in terms of a while loop you</p>



<p>know i have like here&#8217;s the top of the while loop here&#8217;s the little comparison part of the while loop</p>



<p>here&#8217;s the body like the opening braces of the while loop and then here&#8217;s sort of like the</p>



<p>closing braces i just like to do that but anyway after we decided that we are not done and we drop</p>



<p>done and we drop through to the next actual instruction we&#8217;re just going to</p>



<p>print the next argument so what&#8217;s the next argument we will dereference r13</p>



<p>remember up here we took the second item sitting like you know one under the top</p>



<p>of the stack and we just stuck it into r13 so this is r13 is now the address of</p>



<p>the second item on the stack if we de-reference it then that&#8217;s going to give</p>



<p>then that&#8217;s going to give us a pointer to our um sorry it&#8217;s going to give us a pointer to the first</p>



<p>item to the first string i should say to the first string uh the f this the string of the first</p>



<p>argument so if we dereference you know let me say this one more time just to make sure that i&#8217;m</p>



<p>item, you know, the item right under the top of the stack, that&#8217;s going to be a pointer</p>



<p>to the first argument string.</p>



<p>So if we just simply dereference R13, then we&#8217;re basically telling RDI, here&#8217;s a pointer</p>



<p>to the string that we want to print.</p>



<p>Okay, so then I just call my helper function to print that string to standard output, no</p>



<p>problem.</p>



<p>And then we have to figure out like, how do we increment to go to the next string?</p>



<p>might have been unclear earlier because gcc does it in a different way but on the stack</p>



<p>every item underneath the the top of the stack is a is another pointer it&#8217;s part of the original argv</p>



<p>array it&#8217;s another pointer to a different string so basically if i increase r14 here then i&#8217;m</p>



<p>increasing the index counter that&#8217;ll help us eventually terminate but notice how here i&#8217;m</p>



<p>Remember R13 is the second item, you know, the item right under the top of the stack.</p>



<p>And if I dereference that, then I now have a pointer to the first argument string.</p>



<p>If I want to go one lower into the stack, then I just add eight to that register&#8217;s value.</p>



<p>Because again, remember, the stack grows downward in memory.</p>



<p>So if I increase the value, then I&#8217;m sort of like going through previous items that were put into the stack.</p>



<p>So imagine the stack here, it&#8217;s got like,</p>



<p>you know, argc sitting on top</p>



<p>and then underneath argc,</p>



<p>it&#8217;s got a pointer to the first argument.</p>



<p>And then under that in the stack,</p>



<p>it&#8217;s got a pointer to the second argument.</p>



<p>And then under that in the stack,</p>



<p>it&#8217;s got a pointer to the third argument.</p>



<p>Then under that, it&#8217;s got a pointer</p>



<p>to the next argument and so forth.</p>



<p>So every time we wanna go to the next arguments pointer,</p>



<p>the next arguments string pointer,</p>



<p>we just add eight to that R13 register,</p>



<p>which was originally just pointing at the first string.</p>



<p>so again why eight because pointers are 64-bit integers therefore they are eight bytes so I&#8217;m</p>



<p>just literally increasing the index by one and then I&#8217;m moving that register r13 to point to</p>



<p>the next pointer that way next time I dereference it up here on line 70 I&#8217;ll be dereferencing the</p>



<p>next string and these strings don&#8217;t need to be contiguous they could be located anywhere it&#8217;s</p>



<p>that is contiguous but it&#8217;s just full of pointers to other strings or sorry it&#8217;s full of if we de</p>



<p>reference the stack pointer at any point then we will get the address of a string so then we will</p>



<p>get a pointer to a string this double pointer stuff sometimes i get tongue tongue tired okay so uh</p>



<p>when we&#8217;re done printing and incrementing then we just jump to the top of the loop</p>



<p>And then finally, when we&#8217;re done, well, we&#8217;re done.</p>



<p>Let me draw this out for you, because I think the way I&#8217;m explaining it might be</p>



<p>a little bit unclear, so I just want to make sure that I&#8217;m being totally clear on</p>



<p>this, okay, so I&#8217;ve got my little annotator here and you can imagine here&#8217;s a stack</p>



<p>and it visually grows up.</p>



<p>We can imagine that every time we add an item to the stack, it grows up.</p>



<p>Like if you wanted to take a five, stick it on top of the stack.</p>



<p>Well, then it would end up on the top of the stack, right?</p>



<p>No problem.</p>



<p>Right? No problem.</p>



<p>Oh, maybe this needs to be bigger because of my pen size.</p>



<p>So let&#8217;s do it like that.</p>



<p>What the heck happened?</p>



<p>I lost the whole desktop.</p>



<p>There we go.</p>



<p>So we have this and this and this and this.</p>



<p>And so there&#8217;s probably some kind of a value sitting on top of the stack.</p>



<p>When we started the program,</p>



<p>RSP is a register that just has the memory location of the top of the stack,</p>



<p>you know, the most recently added item.</p>



<p>you know the most recently added item we know that this was actually arg c so if we dereferenced rsp</p>



<p>that&#8217;ll go to wherever rx is and get it so we can basically say that rx is sitting on the top of</p>



<p>the stack so you know we dereference our rsp which holds a memory location to this place in memory</p>



<p>we just had an integer stored an eight byte integer okay cool so then the next lowest i guess</p>



<p>the next highest item or the next lower item is arg v at index zero which is the same thing as</p>



<p>just saying arg v remember a pointer to the array is really a pointer to the first item</p>



<p>the next lower item is arg v at index one and so forth so we can just keep doing this we can keep</p>



<p>keep doing this we can keep going lower and lower and lower on the stack by adding eight to r13</p>



<p>because remember r13 originally pointed uh you know to this item right here i don&#8217;t know i&#8217;ll</p>



<p>say like r13 maybe starts off pointing uh to the first argument because that&#8217;s the way we have it</p>



<p>set up so every time the loop iterates if we dereference r13 then we&#8217;re getting a pointer to</p>



<p>when we add eight bytes to r13 we&#8217;re really just moving it down to the next pointer that we can</p>



<p>dereference hopefully that was a little bit more clear than what i said before or maybe you&#8217;re just</p>



<p>a visual learner um you know it&#8217;s a good idea to try to explain things in many different ways</p>



<p>but long story short we&#8217;re going down down down further in the stack to get more arguments and</p>



<p>we&#8217;re just printing them and so now that we&#8217;ve explained it all we should be able to just run</p>



<p>clear and make run and under the hood let me just show you real fast what I&#8217;ve got inside of my make</p>



<p>file under the hood when we do make run you don&#8217;t need to know about make files I have a video that</p>



<p>explains all of this don&#8217;t worry but well I mean you don&#8217;t need to know it to understand this video</p>



<p>but you probably do need to know it notice how when I call the executable which in this case is</p>



<p>I give it some arguments.</p>



<p>I give it just some strings.</p>



<p>First arg, second arg, third arg, fourth arg.</p>



<p>So that&#8217;s the same thing as if I typed echo first arg,</p>



<p>second arg, third arg, fourth arg, right?</p>



<p>That&#8217;s what echo is doing.</p>



<p>So in the make file, I&#8217;m just giving it those arguments.</p>



<p>And then our program now is looping</p>



<p>through all of the arguments.</p>



<p>It knows when to stop because we grabbed argc</p>



<p>and it knows where those strings are located</p>



<p>are located because the pointers to those strings are just sitting on the stack so if we deref twice</p>



<p>we&#8217;re derefing we&#8217;re doing a double dereference a pointer to a pointer</p>



<p>at least we could say r13 is a pointer to a pointer but the actual values sitting inside</p>



<p>the stack are just pointers to strings um so let&#8217;s just do this in a slightly different way</p>



<p>clear and then I&#8217;ll say main because that&#8217;s the name of the program that I</p>



<p>compiled you can imagine this is could be named something else and I&#8217;ll just put</p>



<p>hello you are super cool now so I&#8217;m giving it one two three four five</p>



<p>arguments in addition to main so it should print six things main hello you</p>



<p>are super cool now so notice how it prints main hello you are super cool now</p>



<p>Alright, that&#8217;s pretty much everything that I wanted to show you for the pure assembly</p>



<p>version of Grappin Command Line Arguments.</p>



<p>Thank you so much for listening.</p>



<p>It&#8217;s a little late for me.</p>



<p>The sun&#8217;s starting to come up.</p>



<p>I&#8217;ve got to go and eat a bunch of cookies.</p>



<p>So I hope you learned some stuff and I hope you had a little bit of fun.</p>



<p>Thank you so much for watching this video.</p>



<p>I will see you in the next one.</p>



<p>Happy studying and coding and all that stuff.</p>



<p>Hey everybody!</p>



<p>Thanks for watching this video again from the bottom of my heart.</p>



<p>I really appreciate it.</p>



<p>I do hope you did learn something and have some fun.</p>



<p>If you could do me a please, a small little favor,</p>



<p>could you please subscribe and follow this channel or these videos</p>



<p>or whatever it is you do on the current social media website</p>



<p>that you&#8217;re looking at right now.</p>



<p>It would really mean the world to me and it&#8217;ll help make more videos</p>



<p>and grow this community.</p>



<p>So we&#8217;ll be able to do more videos, longer videos, better videos,</p>



<p>or just i&#8217;ll be able to keep making videos in general so please do do me a kindness and uh and</p>



<p>subscribe you know sometimes i&#8217;m sleeping in the middle of the night and i just wake up because i</p>



<p>know somebody subscribed or followed it just wakes me up and i get filled with joy that&#8217;s exactly what</p>



<p>happens every single time so you could do it as a nice favor to me or you could you control me if</p>



<p>you want to just wake me up in the middle of the night just subscribe and then i&#8217;ll i&#8217;ll just wake</p>



<p>up i promise that&#8217;s what will happen also uh if you look at the middle of the screen right now you</p>



<p>of the screen right now you should see a qr code which you can scan in order to go to the website</p>



<p>which i think is also named somewhere at the bottom of this video and it&#8217;ll take you to my</p>



<p>main website where you can just kind of like see all the videos i published and the services and</p>



<p>tutorials and things that i offer and all that good stuff and uh if you have a suggestion for uh</p>



<p>clarifications or errata or just future videos that you want to see please leave a comment or</p>



<p>up what&#8217;s going on you know just send me a comment whatever i also wake up for those in the middle of</p>



<p>the night i get i wake up in a cold sweat and i&#8217;m like it would really it really mean the world to</p>



<p>me i would really appreciate it so again thank you so much for watching this video and um enjoy the</p>



<p>cool music as as i fade into the darkness which is coming for us all</p>



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/master-command-line-arguments-in-x86-64-assembly-with-yasm/">Master Command Line Arguments in x86-64 Assembly with YASM</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/master-command-line-arguments-in-x86-64-assembly-with-yasm/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Compile, Link, Execute: A Beginner’s Guide to Linux Programming with C++, C, and Assembly</title>
		<link>https://www.NeuralLantern.com/compile-link-execute-a-beginners-guide-to-linux-programming-with-c-c-and-assembly/</link>
					<comments>https://www.NeuralLantern.com/compile-link-execute-a-beginners-guide-to-linux-programming-with-c-c-and-assembly/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Wed, 28 May 2025 03:37:23 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[bash scripting]]></category>
		<category><![CDATA[beginner coding]]></category>
		<category><![CDATA[C programming]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[compile C++]]></category>
		<category><![CDATA[execute Linux]]></category>
		<category><![CDATA[G++ tutorial]]></category>
		<category><![CDATA[link object files]]></category>
		<category><![CDATA[Linux compile script]]></category>
		<category><![CDATA[Linux development]]></category>
		<category><![CDATA[Linux programming]]></category>
		<category><![CDATA[name mangling]]></category>
		<category><![CDATA[object files]]></category>
		<category><![CDATA[programming basics]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=135</guid>

					<description><![CDATA[<p>Learn to compile, link &#038; execute C++, C, and assembly on Linux! Beginner-friendly guide with bash scripting tips. #Linux #Programming</p>
<p>The post <a href="https://www.NeuralLantern.com/compile-link-execute-a-beginners-guide-to-linux-programming-with-c-c-and-assembly/">Compile, Link, Execute: A Beginner’s Guide to Linux Programming with C++, C, and Assembly</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Compile, Link, Execute: A Beginner&#039;s Guide to Linux Programming with C++, C, and Assembly" width="1380" height="776" src="https://www.youtube.com/embed/z9iuTrptPM0?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p>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</p>



<p>Introduction to Compiling, Linking, and Executing 00:00:00<br>Overview of the Process 00:00:05<br>Writing a Simple C++ Program 00:00:28<br>Compiling with G++ 00:02:01<br>Understanding Executable Files 00:03:04<br>Executing a Program 00:04:21<br>Handling Execute Permissions 00:05:13<br>Creating a Compile Script 00:07:00<br>Using Shebang for Scripts 00:08:43<br>Specifying Output Files 00:10:53<br>Breaking Down Compilation Steps 00:12:26<br>Compiling to Object Files 00:13:17<br>Linking Object Files 00:15:24<br>Handling Multiple Source Files 00:18:39<br>Mixing C and C++ with Name Mangling 00:22:52<br>Enhancing Script with Error Handling 00:24:29<br>Adding Assembly Source File 00:31:16<br>Using Bash Variables for Flags 00:41:53<br>Organizing Compiler Flags 00:44:41<br>Final Script Enhancements 00:46:36<br>Conclusion and Call to Action 00:47:35</p>



<p>Thanks for watching!</p>



<p>Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>
</ul>



<p>Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



<li>Leaving a comment or suggestion</li>



<li>Subscribing to our Blog</li>



<li>Watching the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p>Hey there! Let&#8217;s talk about compiling, linking and executing.</p>



<p>This video is going to focus on Linux, but I think it&#8217;ll be useful for all other operating systems that you might be working with at home.</p>



<p>I&#8217;m just going to basically explain like what does it mean to compile?</p>



<p>What does it mean to link a program?</p>



<p>What does it mean to execute a program?</p>



<p>If you don&#8217;t know that, then I think this video might be for you.</p>



<p>Okay, so for starters, imagine you have written a program.</p>



<p>I&#8217;m going to write the program here real fast.</p>



<p>I&#8217;m going to say, let&#8217;s do an empty file.</p>



<p>We&#8217;ll call it, let&#8217;s see, I got my answer key up here.</p>



<p>We&#8217;ll call it first.cpp.</p>



<p>First.cpp.</p>



<p>So I&#8217;m just like making a first piece of source code and I&#8217;m editing it in my little editor.</p>



<p>Notice how I&#8217;m not calling this the main.cpp program because a lot of beginners, they always</p>



<p>do main.</p>



<p>I want you to know that you don&#8217;t actually need a source code file called main.cpp.</p>



<p>Even if you&#8217;re writing a C++ program, all you really need is just one instance of the function main.</p>



<p>Beyond that also, you only need an instance of main if you&#8217;re writing a hybrid program</p>



<p>or some kind of program that is in pure C++ or that has the GCC libraries.</p>



<p>GCC libraries call on the main function as your entry point.</p>



<p>But if you&#8217;re writing pure assembly, the entry point is actually underscore start as a label.</p>



<p>We&#8217;re not really going to do this is not an assembly tutorial.</p>



<p>I just wanted you to know real fast.</p>



<p>Okay.</p>



<p>So I&#8217;m going to do like IO stream and then I&#8217;m going to say</p>



<p>STDC out and I&#8217;m just going to print, you know, hello from first.</p>



<p>Cpp.</p>



<p>STD.</p>



<p>And L.</p>



<p>Okay.</p>



<p>So we kind of have like a hello world program that&#8217;ll just</p>



<p>that&#8217;ll just basically print a message and do nothing else how do we compile</p>



<p>this program and execute it you know what does that actually even mean okay</p>



<p>so for starters the first thing that you probably do when you&#8217;re compiling a</p>



<p>program you&#8217;ve probably been told to do something like this let me get my little</p>



<p>command line open here you&#8217;ve probably been told to do something like this</p>



<p>let&#8217;s compile with GCC or G++ and we&#8217;ll say the standard for C++ that we&#8217;re</p>



<p>There are a bunch of other standards you can use.</p>



<p>We can turn on all warnings.</p>



<p>We can have the compiler be pedantic in terms of the warnings that it gives us.</p>



<p>You know, these two things at the end don&#8217;t really matter that much.</p>



<p>It&#8217;s really all about G++ and then STD C++ 23 to get it compiled.</p>



<p>And then we&#8217;ll give an input file.</p>



<p>We&#8217;ll say burst.cpp.</p>



<p>And then I think probably usually this is where most of you stop.</p>



<p>you stop we just kind of like specify a couple flags specify the input file then</p>



<p>hit enter and it compiles if we list the contents of the directory now this might</p>



<p>be familiar you&#8217;ll probably see something called a.out a.out notice how</p>



<p>the permissions look a little bit different for that for that file it&#8217;s</p>



<p>kind of printed in green in Ubuntu and if you look at the left side right here</p>



<p>you can see that it&#8217;s got a bunch of X&#8217;s in the permissions area that just means</p>



<p>That just means it has execute permission.</p>



<p>The compiler does that automatically for you,</p>



<p>but essentially this is like just, you know,</p>



<p>an executable binary file.</p>



<p>It&#8217;s not a text file.</p>



<p>It&#8217;s just something that the computer is meant to run.</p>



<p>We can look inside of it real fast.</p>



<p>There&#8217;s a program called XXD on Linux,</p>



<p>and it&#8217;ll sort of spill the contents of that file.</p>



<p>So if I say XXD and then I go a.out,</p>



<p>it just spills like a whole lot of like binary nonsense,</p>



<p>right?</p>



<p>you can see at the very beginning elf that&#8217;s uh i think that&#8217;s executable linking format</p>



<p>for linux and then there&#8217;s like a bunch of stuff and it just doesn&#8217;t make sense this is not text</p>



<p>that you can read here&#8217;s like some sort of a shared argument that it&#8217;s using and then some</p>



<p>register information i don&#8217;t even know what most of this is somewhere in here if you look</p>



<p>carefully you&#8217;ll probably see that message where is that oh yeah hello from c first.cpp so it&#8217;s</p>



<p>saying that the message we typed in the program is actually embedded in the executable.</p>



<p>That makes sense.</p>



<p>So hopefully I&#8217;ve proved to you that this is just an executable program.</p>



<p>How do you execute the program?</p>



<p>Well, usually you&#8217;ll have to type, you know, the full path to the program that you want to execute</p>



<p>because your custom program is not in the system library paths.</p>



<p>So your system&#8217;s not going to know where to find it automatically,</p>



<p>even if it&#8217;s in the current folder.</p>



<p>So you would usually do something like, you know, the full path to your executable,</p>



<p>path to your executable like a.out. But there&#8217;s a shortcut we can use in Linux. The dot directory</p>



<p>usually just means or it always means the current directory. So if we say dot and then a slash,</p>



<p>that&#8217;s the same thing as typing out the full path to the current directory. Then it&#8217;s really easy to</p>



<p>just type the name of the file that we compiled. Okay, so a.out. We executed it now. So that&#8217;s what</p>



<p>it means to execute. We just compiled without a lot of options. We&#8217;re going to try to do that better</p>



<p>We&#8217;re going to try to do that better in a second.</p>



<p>And we know that you need execute permissions.</p>



<p>Let me show you what happens if we take away execute permissions.</p>



<p>I&#8217;m going to use a command called chamad, like change the mode.</p>



<p>I&#8217;m going to say take away execute permissions from the a.out program.</p>



<p>Then if I list the directory again, notice how there are a bunch of X&#8217;s missing now</p>



<p>on the left side of that a.out permissions area.</p>



<p>Notice how it&#8217;s not in green anymore.</p>



<p>Depending on your system, it might not be green or gray or whatever.</p>



<p>be green or gray or whatever but just look at the permissions so now if i do a dot out</p>



<p>it&#8217;ll say permission denied maybe you&#8217;ve seen this before especially if you&#8217;re syncing with</p>



<p>the cloud that doesn&#8217;t carry execute permissions from one machine to another which happens to me</p>



<p>all the time we can reverse this with just the chamad you know plus x execute permissions</p>



<p>and then we can just execute it again the funny thing about execute permissions is</p>



<p>you can actually execute a bash program which is basically a text file without</p>



<p>without needing to execute a binary program so this is not a bash video I&#8217;ll</p>



<p>make other videos in the future on my channels for for bash but for now just</p>



<p>know that something you compile is not the only type of thing that you can</p>



<p>actually execute in fact in Linux under the hood a lot of system tools are just</p>



<p>written in bash or Python or something else that looks like a human readable</p>



<p>like a human readable language and they are not necessarily all compiled languages, you</p>



<p>know, like C++.</p>



<p>Okay, so now that we kind of know that we&#8217;re compiling a program, let&#8217;s be a little bit</p>



<p>better about specifying the name of the output file.</p>



<p>So I just want you to see one more flag here and while I&#8217;m doing that, I&#8217;m going to create</p>



<p>a script that will compile our program for us.</p>



<p>nano and I&#8217;ll just name a file I&#8217;ll name it compile and it&#8217;ll be our compiler program okay</p>



<p>so what we did before is it was I think G++ and then we named the standard is C++ 23 and then we</p>



<p>said wall wall just means give me all warnings which is good to help your you know to help you</p>



<p>write better code same thing for pedantic just helps you write better code it gives you more</p>



<p>but you know it&#8217;s pretty good. Okay so this is where we stopped last time so</p>



<p>I&#8217;m going to say now let me remove a dot out now instead of typing the command by</p>



<p>ourselves we can run the compile script. The language I&#8217;ve just written was was</p>



<p>well I haven&#8217;t really written in any particular language but if we use a</p>



<p>shell interpreter called bash which is actually what&#8217;s running in the terminal</p>



<p>right now it&#8217;ll interpret any command line that we give it as just something</p>



<p>line that we give it as just something that we want it to execute to the system. So if I type</p>



<p>bash to run the bash interpreter and I just specify the name of the script, it should compile the</p>



<p>program automatically by executing that one line that I put inside of it. See now I&#8217;ve got the a.out</p>



<p>again. If you rewind the video you&#8217;ll see the a.out wasn&#8217;t there when I originally ran it.</p>



<p>One more time just to prove it to you. bash compile. Now you can see that it&#8217;s back. Okay</p>



<p>Okay, so let&#8217;s make this compile program a little bit better instead of typing bash every single time</p>



<p>I think I just want to try to execute the compile program directly. I</p>



<p>Can do that pretty easily. There&#8217;s something in</p>



<p>In our script that we can put that will designate what interpreter we should use to run the program</p>



<p>So right now it&#8217;s just like some text and it doesn&#8217;t really mean anything</p>



<p>there&#8217;s something called the shebang which just means hash bang and</p>



<p>just provide the path to some interpreter. I&#8217;m going to type shebang and then a path</p>



<p>to the bash interpreter. If you don&#8217;t know where bash is on your system, you can go which bash.</p>



<p>And I guess mine is a user bin bash. I think I can get away with bin bash. Let me see.</p>



<p>Let me just type it out to make sure bin bash. Yeah. Okay. So if I type exit, I&#8217;m actually in</p>



<p>another version of bash, which is inside of bash. If I do exit, I should just kind of stay where I</p>



<p>Okay, so bin bash will work, user bin bash will also work.</p>



<p>I&#8217;m going to go back to editing this compiler program.</p>



<p>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,</p>



<p>all I have to do is add execute permissions on it, and it should work.</p>



<p>So let me first remove a dot out.</p>



<p>And then let me try first to execute compile.</p>



<p>It should not work because I don&#8217;t have execute permissions.</p>



<p>Remember we talked about that before.</p>



<p>permissions. Remember we talked about that before? So if I do that it says</p>



<p>permission denied. Oh no! So I&#8217;m going to give it permission to execute. I&#8217;m going</p>



<p>to say change the mode plus X on compile. If I list the directory again notice how</p>



<p>it&#8217;s green notice how there&#8217;s a bunch of X&#8217;s everywhere. So now I should be able</p>



<p>to just say compile and then it seems to have worked and notice how it has an</p>



<p>because it&#8217;s a pain in the butt to type out all the compile commands every single time,</p>



<p>especially if you have a complicated compile command or many files that you want to compile</p>



<p>in the same shot. So, you know, of course, in another video, I&#8217;m going to talk about make files,</p>



<p>build systems, proper build systems are a lot better than just making a script. But for now,</p>



<p>let&#8217;s just make a script because I just want you to understand, you know, compiling, linking,</p>



<p>some of the surrounding ideas. Okay, so I&#8217;m going to, let&#8217;s see, I&#8217;m going to go nano compile.</p>



<p>And the next upgrade that I want to make is I want to specify the output file because it&#8217;s</p>



<p>kind of a bad idea to not specify the output file and just hope that the compiler gives you</p>



<p>the file that you wanted. I don&#8217;t know. So I&#8217;m going to do dash O main, and that&#8217;s going to give</p>



<p>So if I remove a.out just to keep my area clean, I can now do compile.</p>



<p>And if I list again, notice how I&#8217;ve got main.</p>



<p>So I can execute main and it&#8217;s the same program that we already had.</p>



<p>In fact, maybe sometimes just as a shortcut since this is a script,</p>



<p>what if I&#8217;m always hitting ls-la, I always want to list the folder after I compile.</p>



<p>I could just put that into the script.</p>



<p>The more commands you put into the script, the more convenient things can get, unless</p>



<p>I guess it gets too messy.</p>



<p>So I&#8217;m going to do, I&#8217;m going to list first, and I&#8217;m going to remove main, just to prove</p>



<p>to you that we&#8217;re doing this from scratch, and then I&#8217;ll do compile.</p>



<p>Notice how it compiles and then lists everything for me.</p>



<p>Seems like it didn&#8217;t actually turn the compile green.</p>



<p>That&#8217;s probably another flag that we have to stick in there.</p>



<p>Let me see if I can do that real fast.</p>



<p>If not, I won&#8217;t waste time.</p>



<p>This is not an LS video.</p>



<p>probably color or colors is what I got to do. Let me just do that one more time. Yeah, okay. So</p>



<p>that&#8217;s just a flag on LS. That&#8217;s not what this video is about. So now we&#8217;re compiling directly</p>



<p>to main, but here&#8217;s the problem though. We&#8217;re actually kind of sort of skipping over some</p>



<p>steps that we could be aware of. When we compile our program, we&#8217;re actually taking our source code</p>



<p>and we&#8217;re assembling it down to assembly code under the hood, and then we&#8217;re compiling it to</p>



<p>And then we&#8217;re compiling it to an object file, which is sort of just machine code.</p>



<p>It&#8217;s like a level lower than assembly.</p>



<p>And then as a last step, it&#8217;s getting linked into an executable named main.</p>



<p>So we&#8217;re skipping a ton of steps.</p>



<p>Really what&#8217;s supposed to be happening is every single source file.</p>



<p>Oh, whoops.</p>



<p>I&#8217;m on the wrong machine right now.</p>



<p>Okay.</p>



<p>Where&#8217;s that?</p>



<p>Oh, the machine that I&#8217;m recording on.</p>



<p>There we go.</p>



<p>CPP and we&#8217;re thinking that it just goes directly to main you know that works if you only have one source file or if you don&#8217;t want to have</p>



<p>fine-tuned control over your compiling</p>



<p>But I would like to do something a little bit more detailed. Let&#8217;s compile our</p>



<p>CPP source file into an object file. I&#8217;ll name it first.o and then we&#8217;ll link that into main</p>



<p>Why is that useful because later when we have multiple source files?</p>



<p>We might we might want to compile them all to object files first and then link them all together in one step into main</p>



<p>all together in one step into main this will be even more useful when you later realize that you</p>



<p>can use a build system that does kind of the same thing to save compilation time i won&#8217;t talk too</p>



<p>much about make files here but let me just tell you when you start using a build system</p>



<p>your compile time can be drastically reduced uh story i used to have a program a long time ago</p>



<p>that i wrote myself took like an hour to compile i would literally every little change i made i&#8217;d</p>



<p>have to get up and leave and go make a sandwich or something and then come back by the time it</p>



<p>back by the time it was finished compiling when i started using object files as intermediate steps</p>



<p>and upgraded to a build system i was using gnu make then my compile time went down to like a</p>



<p>minute each time so it was like so much more efficient anyway let&#8217;s do that let&#8217;s do that now</p>



<p>let&#8217;s let&#8217;s uh let&#8217;s nano our compile script and you just need another flag in here for the gcc</p>



<p>an object file and not actually produce a fully linked executable that you can execute that means</p>



<p>i should probably rename main to something else because main is supposed to be my executable at</p>



<p>least that&#8217;s what i&#8217;ve chosen i would like to have every object file be named similarly to the source</p>



<p>file it belongs to so i&#8217;m going to type the output now is going to be first.o and then the c flag</p>



<p>just says only compile don&#8217;t link this means of course that i&#8217;m not going to produce a program</p>



<p>a program that I can run. Let&#8217;s see, I&#8217;m gonna end up accidentally deleting the</p>



<p>compiler. Okay, but we can see what we&#8217;re doing. Okay, so compile and CPP for first.</p>



<p>So I&#8217;m gonna compile it and now notice how we have an object file and no main</p>



<p>and just the source file and its corresponding object file. So I can&#8217;t run</p>



<p>this program. I have to do the additional linking step now. So I&#8217;m gonna do</p>



<p>step now so I&#8217;m going to do compile and in my editor here let&#8217;s see where is my</p>



<p>command that I wanted to do for linking okay so I&#8217;m going to link with the the</p>



<p>GCC libraries I&#8217;m gonna use the G++ command I&#8217;m just gonna put a couple of</p>



<p>flags I&#8217;ll explain them real fast I&#8217;m gonna say that I want a 64-bit</p>



<p>executable which you probably actually don&#8217;t have to specify I&#8217;m going to</p>



<p>anyway and then I&#8217;m gonna tell it again that I want it to link up against the</p>



<p>23 libraries. This is important if multiple modules reference the C++ libraries. I&#8217;m not</p>



<p>even sure. I think I need it in both the compiling command and the linking command. I&#8217;m going to</p>



<p>leave it in both because it should work. Usually if you want to embed debugging symbols into your</p>



<p>program, you want to put a dash G there, which is a great idea. But we&#8217;re not going to talk about</p>



<p>debugging in this video. You can also specify the type of debugging symbols that you want.</p>



<p>symbols that you want I&#8217;m just gonna put that in here right now G dwarf 2 and</p>



<p>then there&#8217;s some new flags that you kind of need there&#8217;s one here called no</p>



<p>PI which I think is just an Ubuntu thing your program probably won&#8217;t execute if</p>



<p>you don&#8217;t do that just just put it in there and trust me there&#8217;s another one</p>



<p>that we have to put in now called no exec stack which basically prevents your</p>



<p>program from executing code sitting inside of the stack which is just sort</p>



<p>not get hacked hopefully it&#8217;s not bulletproof but you know it might help</p>



<p>and then i&#8217;m going to give it input files the input files for the linking</p>



<p>stage maybe i should do a comment linking stage</p>



<p>compile the c++ program or source or something</p>



<p>for the linking stage uh you want to you want to provide all of the object files</p>



<p>that you&#8217;re going to link together in this case we just have you know</p>



<p>file named first and then we can specify the output file as main if we didn&#8217;t</p>



<p>specify it would probably be a dot out like we saw before so if I got this</p>



<p>right let&#8217;s see if I did now this should compile the C++ source code file into</p>



<p>its corresponding object file and then scoop up the object file turn it into an</p>



<p>executable okay so my clear and then list and then I&#8217;m gonna remove the object</p>



<p>put star dot o which is a shell trick to basically say that that star dot o is</p>



<p>going to expand to really mean every file that ends in dot o so you know in</p>



<p>this case it&#8217;s actually just gonna end up being first dot o but it&#8217;s a trick you</p>



<p>can use to grab multiple object files or files of a certain type all in one</p>



<p>command so I&#8217;m gonna do that and then I&#8217;m gonna list again and then I&#8217;m gonna</p>



<p>compile and you can see that the compile command it created first dot o the</p>



<p>the object file, and it also created the executable main.</p>



<p>So if I execute main, the same thing should happen.</p>



<p>So great. We&#8217;re ready to move on to another step.</p>



<p>Let me remove star dot O.</p>



<p>I got to be so careful because I&#8217;m going to totally ruin this video by erasing the wrong thing.</p>



<p>Okay. Because this is, I should be in a Git repository, but I&#8217;m trying to be fast here.</p>



<p>Okay. So let&#8217;s nano compile.</p>



<p>Let&#8217;s create a second source file actually.</p>



<p>Okay. So let me get out of this.</p>



<p>Let me get out of this.</p>



<p>I want to create a second source file.</p>



<p>I&#8217;m going to call it second.c.</p>



<p>It&#8217;s going to be a pure C program.</p>



<p>And because we don&#8217;t have C++ libraries in C source files,</p>



<p>I&#8217;m just going to say, you know,</p>



<p>standard input output dot O for the header.</p>



<p>I&#8217;m going to make a function called void second probably.</p>



<p>And it doesn&#8217;t do anything except just sort of print</p>



<p>a little message.</p>



<p>So I&#8217;m going to say hello from second.</p>



<p>And then do a new line.</p>



<p>because we don&#8217;t have endels in c all right i think that&#8217;s probably going to work so then</p>



<p>let&#8217;s uh edit our compiling script and we&#8217;ll just add another line here to compile the c source</p>



<p>okay so i&#8217;m going to go gcc because it&#8217;s not c plus plus anymore and um i&#8217;m going to do all</p>



<p>warnings and pedantic again uh you don&#8217;t really need even need to worry about that and i&#8217;m going</p>



<p>need even need to worry about that and i&#8217;m going to give it an input of second.c because that&#8217;s the</p>



<p>file we just worked on and i&#8217;m going to tell it i only want you to compile with the dash c flag if i</p>



<p>forgot that on this one then i&#8217;ll probably end up with an object file a main and then an a.out that&#8217;s</p>



<p>only based on the c source code and that probably wouldn&#8217;t work because the second source code</p>



<p>doesn&#8217;t have a main function inside of it so um let&#8217;s see we&#8217;ll do output and it&#8217;s going to be</p>



<p>and then I think that should be okay you know something that I just realized I</p>



<p>should mention too is when you&#8217;re making hybrid programs like this or or</p>



<p>programs where there are just many many source files like I said before you</p>



<p>don&#8217;t need to have a source code named main.cpp you can name all your source</p>



<p>codes anything that you want the one thing that you really do need is a piece</p>



<p>of source code oh I&#8217;m editing inside the terminal suddenly sorry I got</p>



<p>Sorry, I got sidetracked.</p>



<p>I wanted to edit all this stuff inside of Genie because it&#8217;s a little bit better.</p>



<p>Maybe it doesn&#8217;t matter.</p>



<p>So notice how we have the main function inside of our first.cpp program,</p>



<p>and then there&#8217;s no main function inside of our other source codes.</p>



<p>For your program that uses the GCC libraries, you need exactly one instance of main.</p>



<p>so that means um you know you have to have at least one and if you have more than one it&#8217;s</p>



<p>probably not going to link so if i put a main inside of here probably not going to work</p>



<p>this also kind of means you can arrange your source code in a funny way you could</p>



<p>have a bunch of different pieces of source code that you could consider maybe libraries maybe</p>



<p>there are a bunch of classes or something that will help your program and you can release multiple</p>



<p>different executable files by just having different source code files each with their own</p>



<p>mains and linked separately against the object files that contain your library or classes or</p>



<p>whatever that&#8217;s just a little side tip but um anyway let&#8217;s see if i actually did this right</p>



<p>we&#8217;re linking this and i&#8217;m going to see if it works i get stuck inside of the terminal i start</p>



<p>thinking i&#8217;m going to edit everything inside there okay so it compiled well let me let me</p>



<p>remove the main and then just clear it again in the list okay so now I&#8217;m gonna</p>



<p>run the compiler and after the compiler ran everything seemed to be okay it</p>



<p>compiled first into its object file and it also compiled second into its object</p>



<p>file if I run the main executable notice how it only prints hello from first dot</p>



<p>CPP well what about the second one that we just added well what we need to do is</p>



<p>improve our linking stage notice how in the linking stage right here we just</p>



<p>stage right here we just only link first.o so we should also include the other object file that we</p>



<p>want i&#8217;m going to glob that in a second but uh let me show you compile now if we run main</p>



<p>what did i do wrong hold on let&#8217;s see here second.c oh i forgot to actually call the second</p>



<p>function so that&#8217;s cool just uh wrote a bunch of stuff for no reason at all let me uh let me have</p>



<p>have the main function call on the second function we&#8217;re going to encounter an issue here too the</p>



<p>second function is written in c and the first function uh its its source code is in c plus plus</p>



<p>and the thing is c plus plus uses something called name mangling i won&#8217;t go over it too much in this</p>



<p>video but it uses name mangling so you can overload functions you can have functions with the same</p>



<p>name but like different signatures right so it&#8217;ll go in there and it&#8217;ll mangle the name of a function</p>



<p>the name of a function according to its overload prototype so that we can actually call overloaded</p>



<p>functions but we don&#8217;t really have that in C so that means if we try to call a C function</p>



<p>from C++ we&#8217;re probably going to end up calling the wrong name because there&#8217;s the expectation</p>



<p>that the other module has name mangling enabled but it&#8217;s not because it&#8217;s C so I just have</p>



<p>to do this little block extern C and just name all the stuff that is not going to be</p>



<p>name mangled i&#8217;ll just provide the prototypes of all the functions that i want to be able to call</p>



<p>so just void second if you look here again we just have void second and now when um when the c plus</p>



<p>plus module calls on second it won&#8217;t expect name mangling so it should work so we&#8217;re calling it</p>



<p>and then we linked it now everything i think should be okay i&#8217;m going to compile one more time</p>



<p>and then i&#8217;m going to run the main program and now you see that we have both calls in there</p>



<p>Awesome. Let&#8217;s give this script a little bit more output though. This is kind of</p>



<p>precarious. What if something failed inside of there? Maybe you don&#8217;t want the script to continue</p>



<p>if something fails and maybe you kind of want to know where the script is at while it&#8217;s compiling.</p>



<p>So I&#8217;m going to upgrade it just a little bit. So compile the source. I&#8217;m just going to say echo</p>



<p>compiling the, you know, compiling burst.cbp. And then here I&#8217;ll just do another echo and I&#8217;ll say</p>



<p>and then here I&#8217;ll say echo linking the executable or the binary aka the binary</p>



<p>I don&#8217;t even know if that&#8217;s the proper term I just like to call these things binaries because</p>



<p>they&#8217;re not in text anyway so if we do another I&#8217;m going to do I&#8217;m going to do a compound</p>



<p>command line here I&#8217;m going to go clear and compile so that I don&#8217;t have to continue to</p>



<p>continue to clear every single time. Now notice how it prints what it&#8217;s doing. So if it kind of</p>



<p>failed somewhere along the way, or if it took a long time, you&#8217;d kind of know what&#8217;s going on.</p>



<p>Now let&#8217;s use a little bash trick, because remember this is a bash script. It&#8217;s not just</p>



<p>going to only execute actual commands. I&#8217;m going to say or exit with the return code of the previous</p>



<p>We&#8217;re going to try to execute G++.</p>



<p>If G++ fails for some reason, then the whole program or the whole script is going to exit</p>



<p>and it&#8217;s going to exit with the return code mentioned here.</p>



<p>I think it&#8217;s not going to print it.</p>



<p>I&#8217;ll show you how to print it in a second, but we can actually make a function called</p>



<p>die up here in a second, which might be better.</p>



<p>So I&#8217;m going to do the same thing over here.</p>



<p>I&#8217;m going to say or exit with the return code of the GCC command and then here or exit with</p>



<p>And then here or exit with the return code of the linking command.</p>



<p>We&#8217;ll do it one more time here.</p>



<p>Notice how it seems to work.</p>



<p>So now let&#8217;s make the first C++ program not actually compile.</p>



<p>I&#8217;ll put a bunch of Zs there.</p>



<p>And so if we try to compile it again, it should just quit right after it tries to compile the C++ program.</p>



<p>So that saves you some time and helps draw your attention to what went wrong.</p>



<p>The C++ compiler is saying hey, there&#8217;s like a type there. It doesn&#8217;t show the exit code though. So let&#8217;s make a function in bash</p>



<p>That will die and print an exit code. So I&#8217;m going to say do die or maybe just die by itself something like that</p>



<p>Functions whoops. I&#8217;m in the wrong language</p>



<p>Let me go back to the compiler</p>



<p>make a function in bash</p>



<p>Called die and it&#8217;ll take one argument and the argument will just be the exit code</p>



<p>that we want it to alert us of.</p>



<p>So I&#8217;m going to say echo,</p>



<p>I&#8217;m going to say we are dying now.</p>



<p>Previous exits code was,</p>



<p>maybe I&#8217;ll do a little variable here,</p>



<p>I&#8217;ll do a local variable.</p>



<p>Exit code is going to be the first incoming argument.</p>



<p>This is a bash program, not C++.</p>



<p>And then here I can just say exit code.</p>



<p>Okay.</p>



<p>So then after writing all that,</p>



<p>then after writing all that I can say or die and then just you know so I&#8217;m calling</p>



<p>a function and the first argument I&#8217;m giving it is the actual exit code of the</p>



<p>thing that died so we&#8217;re just kind of like using indirection to print a pretty</p>



<p>pretty message to get a little bit more information so we&#8217;ll try that one more</p>



<p>time notice how it says we are dying now previous exit code was one so that just</p>



<p>that just tells us that gcc exited with the exit code of one not super useful but it&#8217;s nice to see</p>



<p>could be something else entirely like i don&#8217;t know maybe uh maybe we misname the first source</p>



<p>code file let&#8217;s see if the code changes i think it probably will nope maybe this exit code was one</p>



<p>no such file or directory all right exit code am i doing something wrong i guess i&#8217;m all right</p>



<p>Alright, okay, so let&#8217;s just move on.</p>



<p>I wanted it to be fancier.</p>



<p>We could also upgrade a little bit if we wanted to.</p>



<p>Instead of echoing, I could make a function called log here,</p>



<p>and I could just say that the log command just echoes,</p>



<p>and maybe we&#8217;ll put a cute label, and we&#8217;ll say, you know,</p>



<p>my super cool program compiler, or how about super cool compiler?</p>



<p>And then just do the $1, because that&#8217;s the first incoming argument that you get.</p>



<p>the first incoming argument that you get now I can type log instead of echo so</p>



<p>that&#8217;s nice and why would you want to do this well now it&#8217;s gonna go inside of a</p>



<p>function which means later on you can start adding extra commands like logger</p>



<p>a log to the system daemon oh no so this would write to your system log in</p>



<p>addition to printing something you could do anything you want you know it&#8217;s just</p>



<p>like a regular program adding more power by sticking everything inside of</p>



<p>functions I&#8217;m gonna put log here and let&#8217;s see if it works</p>



<p>let&#8217;s see if it works pile okay nice notice how we have a little message with a little header which</p>



<p>seems pretty cool maybe instead of echoing here let&#8217;s make a complaint function let&#8217;s do a</p>



<p>complain and same thing as the echo function but maybe I think there&#8217;s a flag where you can tell</p>



<p>echo to print to standard error instead of standard output but I&#8217;m just going to do it the easy way</p>



<p>it the easy way I&#8217;m going to say one goes to two so that the the first pipe standard output is going</p>



<p>to be redirected into the second pipe standard error so now I can use complain instead of um</p>



<p>echo down here complain before I die oh I forgot to actually</p>



<p>or die I forgot to actually exit so that means it was oh yeah it&#8217;s still compiling everything</p>



<p>Let me fix that.</p>



<p>So in the actual die function, hopefully you were at home cringing along with me.</p>



<p>Let&#8217;s see, let&#8217;s exit with the actual exit code that was specified just to make things</p>



<p>a little bit more automatable.</p>



<p>Yeah, now it stops right away.</p>



<p>And the complainer is like, you know, pretty cool.</p>



<p>Let me fix the first source code real fast.</p>



<p>And we&#8217;ll say that the second source code is broken.</p>



<p>We&#8217;ll put a bunch of Z&#8217;s there.</p>



<p>So now the first one compiles, the second one, it breaks.</p>



<p>And we&#8217;re starting to, you know, get a hang for a compiler script that&#8217;s a lot more sophisticated</p>



<p>than just typing things on the command line, right?</p>



<p>You can take this as far as you want.</p>



<p>The next step above this is using a proper build engine.</p>



<p>So let&#8217;s see.</p>



<p>Well, let&#8217;s do another source code.</p>



<p>an assembly source code. So we&#8217;ll say third dot ASM and it&#8217;s just going to be Yasm. This is not</p>



<p>a Yasm or assembly tutorial. I&#8217;m just showing you how to, you know, build a, you know, compile,</p>



<p>link and execute. We&#8217;ll make a data section so I can just stick some defines in here real fast.</p>



<p>I&#8217;ll say system write is going to be equal to a one and then the file descriptor for std out</p>



<p>Do you ever feel like red jumps out at you on the screen?</p>



<p>Does that mean I have eye problems?</p>



<p>I am kind of old now.</p>



<p>I don&#8217;t know.</p>



<p>Hello from third dot ASM and I&#8217;ll just put a little new line there.</p>



<p>So I don&#8217;t have to do it elsewhere.</p>



<p>And then I&#8217;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.</p>



<p>And then I&#8217;ll do a text section, which is where your code goes.</p>



<p>which is where your code goes and you know in hybrid programs if I have this</p>



<p>third function which is marked as a which is marked using a label and then</p>



<p>in return statement I still have to mark a third as being accessible outside</p>



<p>this module so I&#8217;m going to do the global statement and then I&#8217;m just going</p>



<p>to do really quickly I&#8217;m going to move something into RAX it&#8217;s going to be</p>



<p>system right so basically I would like to write something somewhere to some</p>



<p>some file descriptor its first argument wants where I want to write it so I&#8217;m</p>



<p>gonna write it to the standard output instead of an actual file and then I&#8217;m</p>



<p>gonna move RSI the next argument is going to be the actual message or a</p>



<p>pointer to the message that I defined above and you know check out my other</p>



<p>videos if you want to learn assembly that&#8217;s this is not this is not an</p>



<p>assembly learning video I&#8217;m just kind of doing it real fast in case you&#8217;re</p>



<p>probably unless I ruined it somehow let&#8217;s start compiling or assembling the</p>



<p>assembly program so just one more we&#8217;ll say assemble the assembly source or</p>



<p>module or whatever you want to call it we&#8217;re going to use the ASM to do that</p>



<p>G dwarf 2 is the debugging symbols not necessary in this video the format is I</p>



<p>want a 64-bit executable that&#8217;s how you do it in Yasm and I&#8217;m going to give it</p>



<p>and I&#8217;m going to give it an input of just the third assembly source file.</p>



<p>And I want it to output the third object file.</p>



<p>I got to remind myself to glob line 36 after this or die with the exit code.</p>



<p>Okay. So now this might work.</p>



<p>Oh, I got to put, I got to put third dot O in the linking stage.</p>



<p>I&#8217;m going to do clear and I&#8217;m going to go here and compile.</p>



<p>and compile.</p>



<p>Unrecognized debug format, dward.</p>



<p>Why didn&#8217;t you stop me?</p>



<p>It&#8217;s g dwarf.</p>



<p>Let&#8217;s see, what did I do wrong?</p>



<p>g dwarf, oh, dwarf dash two.</p>



<p>Oh, it&#8217;s not even g, it&#8217;s just dwarf two.</p>



<p>Sorry.</p>



<p>And I got to put a dash, I think, for EASM.</p>



<p>Yeah, okay.</p>



<p>Let&#8217;s see if that works.</p>



<p>What did I do wrong?</p>



<p>Oh no no, Yasm doesn&#8217;t need the dash, but GCC wants the dash.</p>



<p>Okay, I had it right the first time.</p>



<p>Doop doop doop, okay, so it probably linked.</p>



<p>Let me run it and see what happened.</p>



<p>Hello from the second, oh, we still have to actually call it from the C++ module.</p>



<p>So now we&#8217;ll do the same thing.</p>



<p>We&#8217;ll say that the assembly module, it&#8217;s not going to use name mangling.</p>



<p>use name mangling there&#8217;s no overloads so we&#8217;re going to put it into the extern C</p>



<p>block and we&#8217;ll just say you know void third how do we know that&#8217;s that it&#8217;s a</p>



<p>void function with no arguments well you know that&#8217;s just a convenience that</p>



<p>happens under the hood with C and C++ in assembly it&#8217;s just a label that you&#8217;re</p>



<p>jumping to and returning from void means return type all that means is I&#8217;m</p>



<p>trying my best not to make this a C++ or an assembly tutorial but all that</p>



<p>All that means is there is no return value getting returned.</p>



<p>That would be the RAX register if I wanted to move something in there.</p>



<p>And then similarly, if I wanted to catch arguments, I&#8217;d be grabbing from other registers.</p>



<p>So I&#8217;m not grabbing any register, any arguments from registers, and I&#8217;m not sending a value back with RAX or XMM0 or anything.</p>



<p>So that just means it&#8217;s void and no arguments.</p>



<p>So let&#8217;s do it again.</p>



<p>Compile it and then run main.</p>



<p>then run main hello from second oh i sure put in the name mingling thing but i forgot to call it</p>



<p>let me do that third so now one more time compile and then run it</p>



<p>what&#8217;s happening in here</p>



<p>third i guess i should probably open this up now third asm so global third and then third oh</p>



<p>important part of a system call you got to put the keyword or the instruction system call in there</p>



<p>one more time i hope you&#8217;re laughing and cringing at home trying to make this more fun maybe i don&#8217;t</p>



<p>know okay so now it&#8217;s all it&#8217;s all working notice how we have a bunch of different modules in</p>



<p>different languages we&#8217;re compiling them all to object files first and we&#8217;re uh we&#8217;re linking</p>



<p>them all into one executable and we&#8217;re doing this in a really really easy way by using a script</p>



<p>script instead of typing all the commands out manually. Pretty convenient, right? Let me give</p>



<p>you another upgrade real fast here. So this is kind of irritating. I have like all of these object</p>



<p>files that I have to specify. What if I forget to specify some? Since this line 36 is just going to</p>



<p>be something that goes directly, you know, to the shell as a command line. This is not like bash</p>



<p>wildcards I can say star dot o meaning I&#8217;m going to give every object file to</p>



<p>the linker as an input file and that should just work automatically keep in</p>



<p>mind if you want to use an advanced strategy like I mentioned earlier where</p>



<p>you have multiple different object files and they&#8217;ve each got their own main</p>



<p>functions because they&#8217;re each going to be their own separate executables then</p>



<p>globbing all object files probably not going to work you might want to organize</p>



<p>files or stick the object files for executables in like their own folders or</p>



<p>something like that we&#8217;re not going to go that far but I just want you to know</p>



<p>so let&#8217;s do that again real fast here we&#8217;ll say clear and compile and I&#8217;ll</p>



<p>just try to like run main assuming that the compiler works yes so it worked</p>



<p>everything compiled by the way this and end that means only execute the next</p>



<p>So if I, you know, clear is obviously going to succeed and then compile, it might succeed</p>



<p>or not.</p>



<p>Then I&#8217;m going to try to run main.</p>



<p>Let me actually, let me actually do that real fast.</p>



<p>Let me show you what I&#8217;m talking about.</p>



<p>I&#8217;m going to remove main.</p>



<p>You&#8217;re okay.</p>



<p>So then I&#8217;m going to do clear and compile and main.</p>



<p>So obviously if everything works, then everything works.</p>



<p>But if I do that again, where something is not actually going to compile, like maybe</p>



<p>the second the c source program is not going to compile by putting some z&#8217;s in there then the</p>



<p>dot main or yeah that part shouldn&#8217;t get executed so it won&#8217;t try to execute our program</p>



<p>notice how it just stops so that&#8217;s convenient if i do it again though with a different command</p>



<p>if i chain instead of between the commands instead of putting and end to say one must execute before</p>



<p>semicolon then they will all execute no matter whether whether they succeeded or not maybe in</p>



<p>certain things you&#8217;re doing you actually want that behavior but in this case it&#8217;s going to be bad</p>



<p>watch what happens it fails to compile and then at the end it also tries to run a program that</p>



<p>doesn&#8217;t exist because it never got linked because the script stopped so i&#8217;m just letting you know</p>



<p>you have lots of different options when you&#8217;re writing these scripts we do it again and then let</p>



<p>And I think, well, maybe there&#8217;s one other thing I want you to know.</p>



<p>Sometimes these lines get really, really long, right?</p>



<p>And it&#8217;s messy and irritating.</p>



<p>At least for me.</p>



<p>Maybe if it goes off the side of the screen for you, it&#8217;s like, oh, that&#8217;s awful.</p>



<p>You can actually just separate a command across multiple lines with a backslash.</p>



<p>Or sorry, I think that is called a forward slash.</p>



<p>It&#8217;s the drive slash.</p>



<p>I don&#8217;t remember forward slash backslash.</p>



<p>I just say web slash and drive slash.</p>



<p>So I&#8217;m going to do G++ and do a little slash.</p>



<p>Then I can put the whole rest of the thing on the next line.</p>



<p>If I wanted to, I could start separating these out by category.</p>



<p>So maybe I&#8217;m like, oh, I don&#8217;t know.</p>



<p>Maybe M64 on its own line.</p>



<p>And then I&#8217;ll keep these other two on their own line.</p>



<p>And maybe these two on their own line.</p>



<p>You know, it&#8217;s up to you how you organize this.</p>



<p>And then I&#8217;ll say, give me all the input files on its own line.</p>



<p>And then the output file on another line.</p>



<p>on another line you could even put the or bars for the die part on its own line I</p>



<p>guess I&#8217;ll just try that and now let&#8217;s just make sure that this still compiles</p>



<p>let me remove star dot o and remove main it&#8217;s not there okay so clear and then</p>



<p>list and then two showed up somewhere oh my mistake I probably should have caught</p>



<p>some file got saved as a two I should have noticed okay so let&#8217;s do clear and</p>



<p>compile and I just run main okay so now everything works notice how even though</p>



<p>everything was on a different line it all just seemed to work cool what about</p>



<p>something here on line 38 we&#8217;re using the C++23 standard but we&#8217;re also doing</p>



<p>that up here and we&#8217;re also using the all warnings flag up here and down here</p>



<p>and pedantic also and even the linker like what if I want to put all warnings</p>



<p>into the linker we could make variables in bash and by the way this is totally</p>



<p>starting to become a job for for mark for make files but for now we&#8217;re just</p>



<p>stay in bash so I can make a variable in bash I can say maybe GCC flags name it something and I&#8217;ll</p>



<p>just put equals whatever and I&#8217;ll just type the stuff that I know is going to appear in every</p>



<p>single invocation of G++ or GCC so I&#8217;m going to go wall because I want to put that everywhere</p>



<p>and I&#8217;m going to say C++23 because I&#8217;m going to put that everywhere and probably pedantic I&#8217;m</p>



<p>I&#8217;m not going to put the dash C flag everywhere because notice how I&#8217;m only putting that on the compiler commands when I just want to make an object file.</p>



<p>But when I&#8217;m linking, there&#8217;s no dash C because that means you got to actually link.</p>



<p>So let&#8217;s see.</p>



<p>We got G dwarf two, G dwarf two.</p>



<p>Maybe I could put G dwarf two there and see if it still works.</p>



<p>So I want debugging symbols and I want them to be in dwarf dash two format, which honestly is the default format.</p>



<p>which honestly is the default format if you just put dash G.</p>



<p>Let&#8217;s see if I can get away with that for now.</p>



<p>So I&#8217;m going to go GCC flags.</p>



<p>And here I can get rid of C++23 and wall and pedantic.</p>



<p>And I&#8217;ll just insert instead GCC flags.</p>



<p>And if you want to be extra careful,</p>



<p>you can put these little braces around the variable name of that.</p>



<p>Probably should be fine.</p>



<p>But because I don&#8217;t want to type that again,</p>



<p>I&#8217;m just going to copy paste it into the other command.</p>



<p>So wall pedantic, just paste that.</p>



<p>Just paste that and then I&#8217;m going to leave it alone for Yasm because Yasm is different than GCC.</p>



<p>And then here I&#8217;m going to do another line where I just kind of put the flags.</p>



<p>That means I can get rid of those two flags.</p>



<p>And then wall and pedantic are going to show up.</p>



<p>No pi is kind of a linker only thing.</p>



<p>So is the no exec stack.</p>



<p>I think probably that will be okay.</p>



<p>Let&#8217;s see if I got that right or wrong.</p>



<p>Oh no.</p>



<p>Unrecognized command line option.</p>



<p>Wall pedantic GCC.</p>



<p>Oh, I guess because I quoted it.</p>



<p>I shouldn&#8217;t have done that.</p>



<p>Because I quoted it, the command line thought that that was all one argument.</p>



<p>I should have remembered.</p>



<p>So I&#8217;m going to unquote it right now.</p>



<p>This should help the command line understand that all of the things inside of that variable,</p>



<p>the GCC flags, are separate arguments instead of just one argument.</p>



<p>Let me try one more time.</p>



<p>Yay, it seems to work.</p>



<p>Okay, we got a warning here.</p>



<p>Command line option C++23 is valid for object,</p>



<p>but not for C.</p>



<p>Okay, so basically I made the mistake</p>



<p>of trying to send a C++ object,</p>



<p>which is this right here, or sorry,</p>



<p>a C++ argument, which is this right here,</p>



<p>into a C compiling command.</p>



<p>Not great.</p>



<p>So what if I did this?</p>



<p>I&#8217;ll do C flags, and it will contain only flags for C,</p>



<p>for C and then I&#8217;ll have another one for C++ flags</p>



<p>which will only contain things that are for C++ compiling so the regular C compiling can take</p>



<p>wall and pedantic okay and then it can take the G dwarf 2 okay the C++ flags it can take everything</p>



<p>except for um sorry the C++ flags can actually take all of the C flags and then additionally the</p>



<p>and then additionally the the c++ 23 flag so i could embed a variable inside of itself i could</p>



<p>say c flags or sorry i could embed a variable inside of another variable so that i can just</p>



<p>steal all of the flags from the c flags variable stick them also in the c++ flags variable and</p>



<p>then just add one more at the end of that hopefully that works i think it probably will</p>



<p>here and then use the C flags here just for compiling I could probably name the</p>



<p>variable better put the word compile in there or something but I don&#8217;t really</p>



<p>want to I think some people put CC to mean like C compiling but I don&#8217;t know I</p>



<p>just want to do it this way so we have that and then we&#8217;re gonna do CPP flags</p>



<p>like this we could also organize the variables differently so that the we</p>



<p>We could have some linker flags, which basically would probably have all of this stuff minus dash C.</p>



<p>And then we could have linking flags.</p>



<p>No, we can have linking flags that has all that stuff minus the C.</p>



<p>And then we&#8217;ll have like some compiler flags that has the linking flags plus the dash C.</p>



<p>But I don&#8217;t really want to spend a bunch of time on this now.</p>



<p>For now, we&#8217;ll just say we have C flags and CPP flags.</p>



<p>And we&#8217;ll just manually add that dash C.</p>



<p>But of course, keep in mind, you can organize these things any way you want.</p>



<p>and organize these things any way you want.</p>



<p>So I&#8217;m gonna do,</p>



<p>well, this might actually work.</p>



<p>I&#8217;m getting distracted.</p>



<p>I think it works.</p>



<p>Okay.</p>



<p>Let me just double check by removing all the object files.</p>



<p>There&#8217;s a two in there still.</p>



<p>Let&#8217;s debug that if it happens again,</p>



<p>then I&#8217;m gonna remove main.</p>



<p>Okay, then I&#8217;m just gonna list everything real fast.</p>



<p>Okay, let&#8217;s see.</p>



<p>Is there gonna be a two in there now?</p>



<p>Is there going to be a two in there now?</p>



<p>No, is that only when it fails?</p>



<p>Only when something fails to compile?</p>



<p>I mean, it&#8217;s working, so that&#8217;s good news.</p>



<p>You know what?</p>



<p>Leave a comment somewhere letting me know</p>



<p>where you think I went wrong with this two,</p>



<p>and maybe I&#8217;ll release another video</p>



<p>or something in the future,</p>



<p>at some distant point in the future.</p>



<p>But yeah, anyway, I hope you learned a lot of stuff</p>



<p>in this video and you had a little bit of fun too.</p>



<p>had a little bit of fun too. This has been, you know, a little intro for compiling, linking</p>



<p>and executing in Linux, but also probably on a bunch of other systems if you have these</p>



<p>same tools, and you&#8217;re working inside of bash. Thank you so much for watching this video.</p>



<p>I&#8217;ll see you in the next one.</p>



<p>of my heart i really appreciate it i do hope you did learn something and have some fun if you could</p>



<p>do me a please a small little favor could you please subscribe and follow this channel or these</p>



<p>videos or whatever it is you do on the current social media website that you&#8217;re looking at right</p>



<p>now it would really mean the world to me and it&#8217;ll help make more videos and grow this community so</p>



<p>we&#8217;ll be able to do more videos longer videos better videos or just i&#8217;ll be able to keep making</p>



<p>Do me a kindness and and subscribe. You know sometimes I&#8217;m sleeping in the middle of the night</p>



<p>And I just wake up because I know somebody subscribed or followed it just wakes me up and I get filled with joy</p>



<p>That&#8217;s exactly what happens every single time</p>



<p>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</p>



<p>And I just subscribe and then I&#8217;ll just wake up. I promise that&#8217;s what will happen</p>



<p>Also, uh, if you look at the middle of the screen right now</p>



<p>You should see a QR code which you can scan in order to go to the website</p>



<p>go to the website which i think is also named somewhere at the bottom of this video and it&#8217;ll</p>



<p>take you to my main website where you can just kind of like see all the videos i published and</p>



<p>the services and tutorials and things that i offer and all that good stuff and uh</p>



<p>if you have a suggestion for uh uh clarifications or errata or just future videos that you want to</p>



<p>see please leave a comment or if you just want to say hey what&#8217;s up what&#8217;s going on you know just</p>



<p>wake up for those in the middle of the night i get i wake up in a cold sweat and i&#8217;m like</p>



<p>it would really it really mean the world to me i would really appreciate it so again thank you so</p>



<p>much for watching this video and um enjoy the cool music as as i fade into the darkness which is</p>



<p>coming for us all</p>



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/compile-link-execute-a-beginners-guide-to-linux-programming-with-c-c-and-assembly/">Compile, Link, Execute: A Beginner’s Guide to Linux Programming with C++, C, and Assembly</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/compile-link-execute-a-beginners-guide-to-linux-programming-with-c-c-and-assembly/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
