<?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>Talk Binary &#187; terminal</title>
	<atom:link href="http://talkbinary.com/tag/terminal/feed/" rel="self" type="application/rss+xml" />
	<link>http://talkbinary.com</link>
	<description>Programming Resources, Technology, Computers</description>
	<lastBuildDate>Wed, 15 Feb 2012 16:01:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>C++ Input and Output ( cin, cout )</title>
		<link>http://talkbinary.com/programming/c/c-input-and-output-cin-cout/</link>
		<comments>http://talkbinary.com/programming/c/c-input-and-output-cin-cout/#comments</comments>
		<pubDate>Thu, 05 Aug 2010 07:54:20 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ cin]]></category>
		<category><![CDATA[C++ cout]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[output]]></category>
		<category><![CDATA[standard input]]></category>
		<category><![CDATA[standard output]]></category>
		<category><![CDATA[terminal]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=3022</guid>
		<description><![CDATA[In our previous tutorial, we learned about the C++ String datatype. Until know, our C++ programs haven&#8217;t been that interactive. In this tutorial we are going to fix that. We are going to talk a little more in depth about gathering input from the user and displaying the output to the terminal. C++ Standard Output<a class="moretag" href="http://talkbinary.com/programming/c/c-input-and-output-cin-cout/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<p>In our previous tutorial, we learned about the <a href="http://talkbinary.com/programming/c/c-string/">C++ String</a> datatype. Until know, our C++ programs haven&#8217;t been that interactive. In this tutorial we are going to fix that. We are going to talk a little more in depth about gathering input from the user and displaying the output to the terminal.</p>
<h3 id="section-1">C++ Standard Output</h3>
<p>If you recall, we have been using the <em>cout</em> object to display information on to the screen. Below, I&#8217;ll describe various ways to output strings, the contents of variables, expressions, and more.</p>
<h4 id="section-2">Basic output with endl</h4>
<p>Below is a simple example of printing out the string &#8220;Hello, World!&#8221; to the terminal accompanied by a newline.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// We first print out Hello World </span>
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Hello, World!&quot;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// We then add the following to create a new line</span>
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span></pre></td></tr></table></div>

<p><center><div id="attachment_3028" class="wp-caption alignnone" style="width: 310px"><a href="http://talkbinary.com/wp-content/uploads/2010/08/gvimandterminal.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/08/gvimandterminal-300x143.jpg" alt="" title="gvimandterminal" width="300" height="143" class="size-medium wp-image-3028" /></a><p class="wp-caption-text">The previous example is demonstrated above.</p></div></center></p>
<h4 id="section-3">Basic output with variables</h4>
<p>Now, we are going to display the contents of a variable. Notice how we can output more than one item with a single <em>cout</em> object.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> num <span style="color: #000080;">=</span> <span style="color: #0000dd;">5</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> num <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span></pre></td></tr></table></div>

<h4 id="section-4">Basic output with math expressions</h4>
<p>We can also output arithmetic expressions. This by the way, is something you will hardly ever use but its good to know anyways since you&#8217;ll always want to save the result of an expression in a variable.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #0000dd;">5</span> <span style="color: #000040;">*</span> <span style="color: #0000dd;">4</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">3</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span></pre></td></tr></table></div>

<h4 id="section-5">Basic output with boolean expressions</h4>
<p>Finally, we will demonstrate a simple example with a boolean expression. This will not print out <em>true</em> or <em>false</em>. It will print out either a <em>1</em> for true, or <em>0</em> for false. You also need the parenthesis, else the < operator will give you an error.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span> <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span>  <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span></pre></td></tr></table></div>

<h3 id="section-6">C++ Standard Input</h3>
<p>For standard output we used the object <em>cout</em> and for standard input, we will use the object <em>cin</em>. Before we start with examples, there is are a couple of things you must know. </p>
<blockquote><ul>
<li>User input is usually done through the keyboard and the terminal. Your program will halt at each user input and keep executing when the user enters a value and then presses enter</li>
<li>The <em>cin</em> object will read in the type of object you tell it to. Therefore, if you wish to have a char, it will read a char. Same with an integer, float, and a string. If you mix things up, you will receive undesired behavior</li>
<li>The user might provide you with invalid values and therefore require you to do error checking. We will leave this for a later topic.</li>
</ul>
</blockquote>
<h4 id="section-7">Reading in a value</h4>
<p>Below is an example of reading in a single and multiple values. Try it by changing the object type.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// We declare and initialize the variable num to -1</span>
<span style="color: #0000ff;">int</span> num <span style="color: #000080;">=</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// We prompt the user for input</span>
<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> num<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// We declare three strings</span>
string a, b, c<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// We read three strings from input</span>
<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> a <span style="color: #000080;">&gt;&gt;</span> b <span style="color: #000080;">&gt;&gt;</span> c<span style="color: #008080;">;</span></pre></td></tr></table></div>

<h3 id="section-8">Going further with input and output</h3>
<p>Now that you understand the basics of input and output why not do the following?</p>
<ol>
<li>Prompt the user for his First Name, Last Name, Age, City, and State in the following fashion.</p>
<pre lang="">
Enter your first name:
Enter your last name:
Enter your age:
Enter your city:
Enter your state:</pre>
<p><br/><br />
Then, output the results in the following fashion. </p>
<pre lang="">
Hello <strong>firstname lastname</strong>!
I see you are <strong>age</strong> years old and live in <strong>city</strong>, <strong>state</strong>.</pre>
</li>
<li>Now try declaring a variable without initializing it. Then output it. What happened?</li>
<li>Try reading in an int value, but give it a string. Output the variable and what happens?</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-input-and-output-cin-cout/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to add Open in Terminal support To Nautilus in Ubuntu</title>
		<link>http://talkbinary.com/linux/how-to-add-open-in-terminal-support-to-nautilus-in-ubuntu/</link>
		<comments>http://talkbinary.com/linux/how-to-add-open-in-terminal-support-to-nautilus-in-ubuntu/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 02:30:49 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[nautilus]]></category>
		<category><![CDATA[open in terminal]]></category>
		<category><![CDATA[synaptic packet manager]]></category>
		<category><![CDATA[terminal]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[windows explorer]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=2348</guid>
		<description><![CDATA[Wouldn&#8217;t it be great to open up Nautlius (equivalent to Windows-Explorer in Ubuntu) and be able to open up your terminal in that specific folder by simply right clicking and choosing an Open in Terminal option? Unfortunately this option isn&#8217;t set by default. Luckily there are two ways to install this option. Installing through the<a class="moretag" href="http://talkbinary.com/linux/how-to-add-open-in-terminal-support-to-nautilus-in-ubuntu/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<p><a href="http://talkbinary.com/wp-content/uploads/2010/03/openinterminal1.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/03/openinterminal1.jpg" alt="" title="openinterminal" width="300" height="269" class="alignleft size-full wp-image-2366" /></a><br/><br/><br/>Wouldn&#8217;t it be great to open up Nautlius (equivalent to Windows-Explorer in Ubuntu) and be able to open up your terminal in that specific folder by simply right clicking and choosing an <strong>Open in Terminal</strong> option? Unfortunately this option isn&#8217;t set by default. Luckily there are two ways to install this option.</p>
<ol>
<li>Installing through the terminal (easiest)</li>
<li>Installing through the Synaptic Package Manager</li>
</ol>
<p><br/><span id="more-2348"></span></br><br/></p>
<h2 id="section-1">Installing Open in Terminal support to Nautilus in Ubuntu</h2>
<p>The easiest method is to install it within the terminal window. </p>
<blockquote><h3 id="section-2">Method 1: Installing Open in Terminal Support through the Terminal</h3>
<p>Simply enter this in your terminal. </p>
<p><strong>sudo apt-get install nautilus-open-terminal</strong></p>
<p>Enter your password if prompted, and then restart Nautlius. You can do so using the following through the terminal as well.</p>
<p><strong>killall nautlius</strong></p>
<p><center><a href="http://talkbinary.com/wp-content/uploads/2010/03/open-terminal21.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/03/open-terminal21.jpg" alt="" title="open-terminal2" width="475" height="167" class="alignnone size-full wp-image-2363" /></a></center></p>
<p>Once this is done, you should see this option within Nautilus. See above? I simply opened the current directory within the terminal. Didn&#8217;t that save some time?
</p></blockquote>
<blockquote><h3 id="section-3">Method 2: Installing Open in Terminal Support through Synaptic Packet Manager</h3>
<p>1. Open the Synaptic Packet Manager </p>
<p><strong>System -> Administration -> Synaptic Package Manager </strong></p>
<p><center><br />
<a href="http://talkbinary.com/wp-content/uploads/2010/03/open-terminal.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/03/open-terminal.jpg" alt="" title="open-terminal" width="450" height="147" class="aligncenter size-full wp-image-2359" /></a></center><br />
<br/><br />
2. You can do a quick search for <strong>nautilus-open-terminal</strong> and mark it for installation by right clicking on the entry. </p>
<p>3. Apply the changes, then close Synaptic Package Manager, log off and then back on.</p>
<p>After logging in, you should see the option available when you right click in Nautilus.
</p></blockquote>
<p><a href="http://www.watchingthenet.com/add-open-command-prompt-here-functionality-to-nautilus-in-ubuntu.html">Add Open Command Prompt Here Functionality To Nautilus In Ubuntu</a> via <a href="http://www.watchingthenet.com">Watching The Net</a></p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/linux/how-to-add-open-in-terminal-support-to-nautilus-in-ubuntu/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Command Line Arguments</title>
		<link>http://talkbinary.com/programming/c/command-line-arguments/</link>
		<comments>http://talkbinary.com/programming/c/command-line-arguments/#comments</comments>
		<pubDate>Sun, 03 Aug 2008 06:54:07 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[argc]]></category>
		<category><![CDATA[argv]]></category>
		<category><![CDATA[command line arguments]]></category>
		<category><![CDATA[terminal]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=514</guid>
		<description><![CDATA[Command Line Arguments allow the user to run the program thruogh the terminal as well as pass in arguments into your program in one line. Most common use of command line arguments, are to pass in &#8220;strings&#8221; that represent file names from which you are going to read data from. Format of Command Line Arguments<a class="moretag" href="http://talkbinary.com/programming/c/command-line-arguments/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<p>Command Line Arguments allow the user to run the program thruogh the terminal as well as pass in arguments into your program in one line. Most common use of command line arguments, are to pass in &#8220;strings&#8221; that represent file names from which you are going to read data from.</p>
<h3 id="section-1">Format of Command Line Arguments</h3>
<p>This is how you would write a simple program without command line arguments.</p>
<pre lang="c++">#include  <iostream>
using namespace std;  

double square(int number) {
     return number*number; }  

int main() {
     int number;
     cin &gt;&gt; number;
     cout &lt;&lt; square(number) &lt;&lt; endl;
     return 0;
}</pre>
<p>To run this program you&#8217;d do the following in the terminal. ($ denotes your input)</p>
<pre lang="c++">$./a.out
$10        //Your input
 100      //The output</pre>
<h3 id="section-2">Using Command Line Arguments</h3>
<p>This is how you would use command line arguments.</p>
<pre lang="c++">#include <iostream>
 using namespace std;  

double square(int number) {
     return number*number; }  

int main(int argc, char* argv[]) {
      int number = atoi(argv[1]);      //Convert our c-string to int using the atoi function
      cout &lt;&lt; square(number) &lt;&lt; endl;
      return 0;
} </pre>
<p>To run this, you&#8217;d simply do the following in the terminal.</p>
<pre lang="c++">$./a.out 10 //One line in the terminal
100 //Output</pre>
<h3 id="section-3">How does command line arguments work?</h3>
<pre lang="c++">int main(int argc, char* argv[])</pre>
<p>The <strong>int argc</strong> parameter denotes the number of arguments passed in.<br />
The <strong>char* argv[]</strong> parameter denotes a char pointer to a c-string.</p>
<p>So, typing the following:</p>
<pre lang="c++">$./a.out 10 45</pre>
<p>Would produce the following:</p>
<p><center><img class="alignnone size-full wp-image-518" title="commandlinearguments" src="http://talkbinary.com/wp-content/uploads/2008/08/commandlinearguments1.jpg" alt="" width="252" height="112" /></center></p>
<p>In other words, the arguments are passed in as c-strings reason for why we converted our c-string to an int using the <strong>atoi</strong> function.</p>
<h3 id="section-4">Things to do with Command Line Arguments</h3>
<p>You may pass in any type of arguments. Try the following challenges to learn more.<br />
<strong>Challenge 1: </strong>Create a program using command line arguments where you pass in your name, and two numbers, adds the numbers together and displays them both in the terminal.<br />
<strong>Challenge 2: </strong>Create a program that accepts one int and returns the square of the number as shown above. The difference is that you need to error check your program. In other words, if they don&#8217;t pass in an int value or more than one int value, you should terminate the program. Hint: Use argc, and if statements.</pre>
</pre>
<p>Need more help? Go ask in the <a href="http://forums.talkbinary.com">forums</a> where our committed community will surely answer all your questions!</p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/command-line-arguments/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Basic Linux Terminal Commands</title>
		<link>http://talkbinary.com/linux/basic-linux-terminal-commands/</link>
		<comments>http://talkbinary.com/linux/basic-linux-terminal-commands/#comments</comments>
		<pubDate>Thu, 05 Jun 2008 23:04:07 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[terminal]]></category>
		<category><![CDATA[terminal commands]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=106</guid>
		<description><![CDATA[If you are new to Linux and it&#8217;s terminal, you might be wondering how to move around? I&#8217;ll show you how with some basic terminal commands. Below I&#8217;ll list a few examples with comments denoted by # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18<a class="moretag" href="http://talkbinary.com/linux/basic-linux-terminal-commands/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<p>If you are new to Linux and it&#8217;s terminal, you might be wondering how to move around? I&#8217;ll show you how with some basic terminal commands.<br />
<br/><br />
<center><img src="http://talkbinary.com/wp-content/uploads/2008/06/terminalcommands1.jpg" alt="" title="terminalcommands1" width="500" height="168" class="alignnone size-full wp-image-108" /></center></p>
<p><br/>Below I&#8217;ll list a few examples with comments denoted by #<span id="more-106"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#Displays contents in folder</span>
<span style="color: #c20cb9; font-weight: bold;">ls</span> 
&nbsp;
<span style="color: #666666; font-style: italic;"># Changes Directory </span>
<span style="color: #7a0874; font-weight: bold;">cd</span> FOLDERNAME
&nbsp;
<span style="color: #666666; font-style: italic;"># Go up one directory</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> ..
&nbsp;
<span style="color: #666666; font-style: italic;"># Go to your home directory</span>
<span style="color: #7a0874; font-weight: bold;">cd</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Make directory</span>
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> FOLDERNAME
&nbsp;
<span style="color: #666666; font-style: italic;"># Remove a file</span>
<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">--</span> FILENAME
&nbsp;
<span style="color: #666666; font-style: italic;"># Copy a file</span>
<span style="color: #c20cb9; font-weight: bold;">cp</span> ORIGINALFILE NEWFILE
&nbsp;
<span style="color: #666666; font-style: italic;"># Move a file</span>
<span style="color: #c20cb9; font-weight: bold;">mv</span> SOURCE DESTINATION
&nbsp;
<span style="color: #666666; font-style: italic;"># Install Program in Ubuntu and other Linux Distros</span>
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> PROGRAMNAME
&nbsp;
<span style="color: #666666; font-style: italic;"># To run a program you usually specify the name, </span>
<span style="color: #666666; font-style: italic;"># if text editor, its usually name of program and </span>
<span style="color: #666666; font-style: italic;"># text file to create, if no file is specified, the text editor still opens.</span>
firefox
emacs hello_world.cpp
&nbsp;
<span style="color: #666666; font-style: italic;"># Compile a program using g++ </span>
<span style="color: #c20cb9; font-weight: bold;">g++</span> hello_world.cpp
&nbsp;
<span style="color: #666666; font-style: italic;"># Execute program </span>
.<span style="color: #000000; font-weight: bold;">/</span>PROGRAMNAME</pre></td></tr></table></div>

<p><br/>An example of finding help for a certain command is the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">man</span> <span style="color: #c20cb9; font-weight: bold;">mkdir</span></pre></td></tr></table></div>

<p><br/>The <b>man</b> command will give you the manual page for a given command.<br />
<br/></p>
<h3 id="section-1">Example using the terminal</h3>
<p>Let&#8217;s say we want to view the contents of a folder, create a folder called Example, create a text file using emacs, compile it, run it, and go back to our home directory. To do this, we would simply do the following.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span>
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> C++
<span style="color: #7a0874; font-weight: bold;">cd</span> C++
emacs hello_world.cpp
<span style="color: #c20cb9; font-weight: bold;">g++</span> hello_world.cpp
.<span style="color: #000000; font-weight: bold;">/</span>a.out 
<span style="color: #7a0874; font-weight: bold;">cd</span></pre></td></tr></table></div>

<p><br/>Yes there are numerous ways to achieve this, its just a matter of how you want to navigate the terminal.<br />
<br/></p>
<h3 id="section-2">What now?</h3>
<p><br/>If you feel another basic command needs to be here or need more help, simply drop in a line in the comments box! I&#8217;ll be sure to put it down.<br />
<br/>Expect advanced terminal commands in the near future.</p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/linux/basic-linux-terminal-commands/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to write and compile C++ program in Linux</title>
		<link>http://talkbinary.com/programming/c/how-to-write-and-compile-c-program-in-linux/</link>
		<comments>http://talkbinary.com/programming/c/how-to-write-and-compile-c-program-in-linux/#comments</comments>
		<pubDate>Thu, 29 May 2008 23:50:48 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[compile]]></category>
		<category><![CDATA[hello world]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[terminal]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=68</guid>
		<description><![CDATA[Here is a step by step detailed guide on showing you how to write and compile a C++ program in Linux. For demonstration purposes, I&#8217;ll show you how to write and compile Hello World which is typically the first program that many students learn when introduced to programming. How to write and compile Hello World<a class="moretag" href="http://talkbinary.com/programming/c/how-to-write-and-compile-c-program-in-linux/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<p>Here is a step by step detailed guide on showing you how to write and compile a C++ program in Linux. For demonstration purposes, I&#8217;ll show you how to write and compile Hello World which is typically the first program that many students learn when introduced to programming.<br />
<br/></p>
<h2 id="section-1">How to write and compile Hello World in C++</h2>
<p>First, let&#8217;s locate and open up our handy terminal. The terminal is the console on where you will initially learn how to create folders in order to create the programs that we will later compile. Depending on your OS your Terminal or Console might be opened in various ways. The image below demonstrates how to open it in Ubuntu.<br />
<br/><br />
<center><a href='http://talkbinary.com/wp-content/uploads/2008/05/terminal.jpg'><img src="http://talkbinary.com/wp-content/uploads/2008/05/terminal-300x225.jpg" alt="terminal c++ hello world" title="terminal" width="300" height="225" class="alignnone size-medium wp-image-72" /></a></center><span id="more-68"></span><br />
<br/></p>
<blockquote><h4 id="section-2">Installing the appropriate tools</h4>
<p>We might have to install the tools necessary to start writing, and compile our code. In order to do this we must specify some commands to install our tools. If you already have the programs installed, it will either upgrade them, or do nothing so either way there is no harm in trying this out.</p>
<p>The following commands simply give permission to install the packages we need.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">sudo apt<span style="color: #000040;">-</span>get install g<span style="color: #000040;">++</span> 
sudo apt<span style="color: #000040;">-</span>get install emacs</pre></div></div>

<p>If it asks you for a password, simply provide the password you log in with. And if it asks you for what version of emacs, simply type in emacs22 for now.</p>
<p><em>Other linux distributions may have different methods for installing these tools. If so, use their Package Managers to download them.</em>
</p></blockquote>
<h2 id="section-3">Creating a folder and creating the Hello World program file</h2>
<p>Now, let&#8217;s create a new folder and open a file in that folder to write our Hello World program. I&#8217;ll explain the commands shown on the screen in more detail below the image.<br />
<br/><br />
<center><a href='http://talkbinary.com/wp-content/uploads/2008/05/terminal1.jpg'><img src="http://talkbinary.com/wp-content/uploads/2008/05/terminal1-300x227.jpg" alt="terminal emacs hello world C++" title="terminal1" width="300" height="227" class="alignnone size-medium wp-image-69" /></a></center><br />
<br/></p>
<p>The following command lists the contents of the current directory.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span></pre></div></div>

<p>The following command makes a directory with the name FOLDERNAME.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">mkdir</span> FOLDERNAME</pre></div></div>

<p>The following command changes your current directory to FOLDERNAME.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> FOLDERNAME</pre></div></div>

<p>The following command opens up the program emacs with the file hello_world.cpp. If not created it will create it for you.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">emacs hello_world.cpp</pre></div></div>

<p><br/></p>
<h2 id="section-4">Writing Hello World</h2>
<p>With emacs open, simply click on the editor and it should turn blank. Write the following code for your Hello World program. When done, simply hit save. (It will save in the folder you called emacs hello_world.cpp)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
&nbsp;
     <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Hello World!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
     <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><br/><br />
<center><a href='http://talkbinary.com/wp-content/uploads/2008/05/helloworld.jpg'><img src="http://talkbinary.com/wp-content/uploads/2008/05/helloworld-300x226.jpg" alt="Hello World!" title="helloworld" width="300" height="226" class="alignnone size-medium wp-image-71" /></a></center><br />
<br/></p>
<p>Your editor should look like the image above. </p>
<h2 id="section-5">Compiling our Hello World Program</h2>
<p>Now that you have your program, let&#8217;s compile it so it can be written in machine language that can later be executed.<br />
<center><a href='http://talkbinary.com/wp-content/uploads/2008/05/compilehelloworld.jpg'><img src="http://talkbinary.com/wp-content/uploads/2008/05/compilehelloworld-300x229.jpg" alt="Hello World!" title="compilehelloworld" width="300" height="229" class="alignnone size-medium wp-image-70" /></a></center><br />
<br/><br />
The following command will compile your program and create an executable called <strong>a.out</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">g++</span> hello_world.cpp</pre></div></div>

<p>The following command will execute your program.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>a.out</pre></div></div>

<p><br/><br />
<br/>Congratulations! You are done! I tried making it as simple as possible. If you still have any questions, feel free to drop a comment or suggestion to make this tutorial better!<br />
<br/></p>
<h2 id="section-6">Useful resources</h2>
<p><a href="http://talkbinary.com/category/c/">C++ Guide</a> Your guide for programming in C++</a><br />
<br/></p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/how-to-write-and-compile-c-program-in-linux/feed/</wfw:commentRss>
		<slash:comments>54</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

Served from: talkbinary.com @ 2012-05-24 15:39:32 -->
