<?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>askyb.com &#187; Linux</title>
	<atom:link href="http://www.askyb.com/category/linux/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.askyb.com</link>
	<description>Programming Tips and Tuorials</description>
	<lastBuildDate>Wed, 12 Jun 2013 08:07:47 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>C++ PostgreSQL Example in Linux</title>
		<link>http://www.askyb.com/cpp/c-postgresql-example-in-linux/</link>
		<comments>http://www.askyb.com/cpp/c-postgresql-example-in-linux/#comments</comments>
		<pubDate>Thu, 26 Apr 2012 05:37:03 +0000</pubDate>
		<dc:creator>askyb</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[G++]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[PostgreSQL]]></category>

		<guid isPermaLink="false">http://www.askyb.com/?p=682</guid>
		<description><![CDATA[In this tutorial, i&#8217;ll demonstrate an example on how to connect to PostgreSQL database using gcc in linux. Also, i&#8217;ll demonstrate some basic SQL execution in the C++ example so that it give better understading to you how all of these are working. If you are looking a tutorial for windows platform, you could visit...]]></description>
				<content:encoded><![CDATA[In this tutorial, i&#8217;ll demonstrate an example on how to connect to PostgreSQL database using gcc in linux. Also, i&#8217;ll demonstrate some basic SQL execution in the C++ example so that it give better understading to you how all of these are working.

If you are looking a tutorial for windows platform,  you could visit the following link which demonstrate the tutorial for windows platform. <a href="http://www.askyb.com/cpp/c-postgresql-example/" title="C++ PostgreSQL Example in Windows" target="_blank">C++ PostgreSQL Example in Windows</a>
<span id="more-682"></span>
To begin this tutorial, let&#8217;s make sure that you have the PostgreSQL install into your linux box. I&#8217;m using Ubuntu linux in this demo. Use the following command to download &#038; intstall PostgreSQL if it&#8217;s not exists in your linux box.

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$ </span><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get install</span> postgresql</div></div>

Let&#8217;s turn on the psql command console with the following command:

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$ </span><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #660033;">-u</span> postgres psql template1</div></div>

Let&#8217;s create a password for this by using the following command in psql console:

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;"># </span>\password postgres</div></div>

You will need to key in you password now. I use password &#8220;test123&#8243; in this demo.

Let&#8217;s create the database now by using the following command in psql console. Our database name in this demo called &#8220;testdb&#8221;.

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;"># </span>create database testdb;</div></div>

<a href="http://www.askyb.com/wp-content/uploads/2012/04/26042012_1.png"><img class="aligncenter size-full wp-image-706" title="c++ postgresql example in linux" src="http://www.askyb.com/wp-content/uploads/2012/04/26042012_1.png" alt="" width="466" height="210" /></a>

At this point, we&#8217;ve the PostgreSQL database created and we can now writing some C++ code to establish connection to this database. We will start the coding part in our cpp file now. I called the cpp file &#8211; &#8220;PSQLTest.cpp&#8221;. Make sure that you include the following header accordingly. libpq-fe.h must be included.

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#include &lt;iostream&gt;</span><br />
<span style="color: #339900;">#include &quot;libpq-fe.h&quot;</span><br />
<br />
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span></div></td></tr></tbody></table></div>

Now we start our very first functon named CloseConn(). This function will make sure that we close the database connection accordingly.

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff0000; font-style: italic;">/* Close connection to database */</span><br />
<span style="color: #0000ff;">void</span> CloseConn<span style="color: #008000;">&#40;</span>PGconn <span style="color: #000040;">*</span>conn<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; PQfinish<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000dd;">getchar</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>

Next, we create a function named ConnectDB(). This function will establish a connection to PostgreSQL server. Please be sure that you provide the correct parameters in PostgreSQL(). Prior to this example, i have setup a database called testdb with user password of test123 during the installation. You might need to modify it accordingly in order to make sure the compilation success.

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff0000; font-style: italic;">/* Establish connection to database */</span><br />
PGconn <span style="color: #000040;">*</span>ConnectDB<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; PGconn <span style="color: #000040;">*</span>conn <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// Make a connection to the database</span><br />
&nbsp; conn <span style="color: #000080;">=</span> PQconnectdb<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;user=postgres password=test123 dbname=testdb hostaddr=127.0.0.1 port=5432&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// Check to see that the backend connection was successfully made</span><br />
&nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>PQstatus<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> CONNECTION_OK<span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Connection to database failed.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; CloseConn<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Connection to database - OK<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">return</span> conn<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>

Next, we create a function named CreateEmployeeTable(). This function will create an employee table in our testdb database.

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff0000; font-style: italic;">/* Create employee table */</span><br />
<span style="color: #0000ff;">void</span> CreateEmployeeTable<span style="color: #008000;">&#40;</span>PGconn <span style="color: #000040;">*</span>conn<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #666666;">// Execute with sql statement</span><br />
&nbsp; PGresult <span style="color: #000040;">*</span>res <span style="color: #000080;">=</span> PQexec<span style="color: #008000;">&#40;</span>conn, <span style="color: #FF0000;">&quot;CREATE TABLE employee (Fname char(30), Lname char(30))&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp;<br />
&nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>PQresultStatus<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> PGRES_COMMAND_OK<span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Create employee table failed<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; PQclear<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; CloseConn<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Create employee table - OK<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// Clear result</span><br />
&nbsp; PQclear<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>

Next, we create a function named InsertEmployeeRec(). This function will take 2 parameters, fname and lname in char pointer type, to form a SQL statement. It then will be executed in order to store the record into the employee table.

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;height:200px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff0000; font-style: italic;">/* Append SQL statement and insert record into employee table */</span><br />
<span style="color: #0000ff;">void</span> InsertEmployeeRec<span style="color: #008000;">&#40;</span>PGconn <span style="color: #000040;">*</span>conn, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span> fname, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span> lname<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #666666;">// Append the SQL statment</span><br />
&nbsp; std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> sSQL<span style="color: #008080;">;</span><br />
&nbsp; sSQL.<span style="color: #007788;">append</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;INSERT INTO employee VALUES ('&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; sSQL.<span style="color: #007788;">append</span><span style="color: #008000;">&#40;</span>fname<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; sSQL.<span style="color: #007788;">append</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;', '&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; sSQL.<span style="color: #007788;">append</span><span style="color: #008000;">&#40;</span>lname<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; sSQL.<span style="color: #007788;">append</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;')&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp;<br />
&nbsp; <span style="color: #666666;">// Execute with sql statement</span><br />
&nbsp; PGresult <span style="color: #000040;">*</span>res <span style="color: #000080;">=</span> PQexec<span style="color: #008000;">&#40;</span>conn, sSQL.<span style="color: #007788;">c_str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>PQresultStatus<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> PGRES_COMMAND_OK<span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Insert employee record failed<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; PQclear<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; CloseConn<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Insert employee record - OK<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// Clear result</span><br />
&nbsp; PQclear<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>

Next, we create a function named FetchEmployeeRec(). This function will fetch all the record in employee table and display it on the console windows.

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;height:200px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff0000; font-style: italic;">/* Fetch employee record and display it on screen */</span><br />
<span style="color: #0000ff;">void</span> FetchEmployeeRec<span style="color: #008000;">&#40;</span>PGconn <span style="color: #000040;">*</span>conn<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #666666;">// Will hold the number of field in employee table</span><br />
&nbsp; <span style="color: #0000ff;">int</span> nFields<span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// Start a transaction block</span><br />
&nbsp; PGresult <span style="color: #000040;">*</span>res &nbsp;<span style="color: #000080;">=</span> PQexec<span style="color: #008000;">&#40;</span>conn, <span style="color: #FF0000;">&quot;BEGIN&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>PQresultStatus<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> PGRES_COMMAND_OK<span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;BEGIN command failed<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; PQclear<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; CloseConn<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #666666;">// Clear result</span><br />
&nbsp; PQclear<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// Fetch rows from employee table</span><br />
&nbsp; res <span style="color: #000080;">=</span> PQexec<span style="color: #008000;">&#40;</span>conn, <span style="color: #FF0000;">&quot;DECLARE emprec CURSOR FOR select * from employee&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>PQresultStatus<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> PGRES_COMMAND_OK<span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;DECLARE CURSOR failed<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; PQclear<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; CloseConn<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #666666;">// Clear result</span><br />
&nbsp; PQclear<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; res <span style="color: #000080;">=</span> PQexec<span style="color: #008000;">&#40;</span>conn, <span style="color: #FF0000;">&quot;FETCH ALL in emprec&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>PQresultStatus<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> PGRES_TUPLES_OK<span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;FETCH ALL failed<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; PQclear<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; CloseConn<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #666666;">// Get the field name</span><br />
&nbsp; nFields <span style="color: #000080;">=</span> PQnfields<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// Prepare the header with employee table field name</span><br />
&nbsp; <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>Fetch employee record:&quot;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>********************************************************************<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> nFields<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;%-30s&quot;</span>, PQfname<span style="color: #008000;">&#40;</span>res, i<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>********************************************************************<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// Next, print out the employee record for each row</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> PQntuples<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> j <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> j <span style="color: #000080;">&lt;</span> nFields<span style="color: #008080;">;</span> j<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;%-30s&quot;</span>, PQgetvalue<span style="color: #008000;">&#40;</span>res, i, j<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp;<br />
&nbsp; &nbsp; PQclear<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// Close the emprec</span><br />
&nbsp; &nbsp; res <span style="color: #000080;">=</span> PQexec<span style="color: #008000;">&#40;</span>conn, <span style="color: #FF0000;">&quot;CLOSE emprec&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; PQclear<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// End the transaction</span><br />
&nbsp; &nbsp; res <span style="color: #000080;">=</span> PQexec<span style="color: #008000;">&#40;</span>conn, <span style="color: #FF0000;">&quot;END&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// Clear result</span><br />
&nbsp; &nbsp; PQclear<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>

Next, we create a function named RemoveAllEmployeeRec(). This function will remove all record in employee table.

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff0000; font-style: italic;">/* Erase all record in employee table */</span><br />
<span style="color: #0000ff;">void</span> RemoveAllEmployeeRec<span style="color: #008000;">&#40;</span>PGconn <span style="color: #000040;">*</span>conn<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #666666;">// Execute with sql statement</span><br />
&nbsp; PGresult <span style="color: #000040;">*</span>res <span style="color: #000080;">=</span> PQexec<span style="color: #008000;">&#40;</span>conn, <span style="color: #FF0000;">&quot;DELETE FROM employee&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>PQresultStatus<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> PGRES_COMMAND_OK<span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Delete employees record failed.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; PQclear<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; CloseConn<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>Delete employees record - OK<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// Clear result</span><br />
&nbsp; PQclear<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>

Next, we create a function named DropEmployeeTable(). This function will drop or remove the employee from the testdb database.

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff0000; font-style: italic;">/* Drop employee table from the database*/</span><br />
<span style="color: #0000ff;">void</span> DropEmployeeTable<span style="color: #008000;">&#40;</span>PGconn <span style="color: #000040;">*</span>conn<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #666666;">// Execute with sql statement</span><br />
&nbsp; PGresult <span style="color: #000040;">*</span>res <span style="color: #000080;">=</span> PQexec<span style="color: #008000;">&#40;</span>conn, <span style="color: #FF0000;">&quot;DROP TABLE employee&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>PQresultStatus<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> PGRES_COMMAND_OK<span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Drop employee table failed.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; PQclear<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; CloseConn<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Drop employee table - OK<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// Clear result</span><br />
&nbsp; PQclear<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>

Finally, we update the main entry point function so that it call all the functions that we have created to demonstrate what we intend to show in this example.

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;height:200px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; PGconn <span style="color: #000040;">*</span>conn <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span><br />
&nbsp; <br />
&nbsp; conn <span style="color: #000080;">=</span> ConnectDB<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>conn <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; CreateEmployeeTable<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; InsertEmployeeRec<span style="color: #008000;">&#40;</span>conn, <span style="color: #FF0000;">&quot;Mario&quot;</span>, <span style="color: #FF0000;">&quot;Hewardt&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; InsertEmployeeRec<span style="color: #008000;">&#40;</span>conn, <span style="color: #FF0000;">&quot;Daniel&quot;</span>, <span style="color: #FF0000;">&quot;Pravat&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; FetchEmployeeRec<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>Press ENTER to remove all records &amp; table.....<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">getchar</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; RemoveAllEmployeeRec<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; DropEmployeeTable<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; CloseConn<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>

Try to compile and run the application now. You should see the following screenshot. <strong>REMEMBER</strong>, in order to make sure that the compilation can be done successfully, you must tell the gcc compiler where to find postgres include file. Also, you must include postgres library into your c++ project as well. All this can be done by supplying the following argument in the gcc command.

If you are new to C++ compilation in Linux, please visit to my previous post which should guide you on how to compile C++ in Linux. <a href="http://www.askyb.com/cpp/compiling-c-code-by-using-g-in-ubuntu/" title="Compiling C++ code by using g++ in Linux" target="_blank">Compiling C++ code by using g++ in Ubuntu</a>

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">g++</span> PSQLTest.cpp <span style="color: #660033;">-I</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>include<span style="color: #000000; font-weight: bold;">/</span>postgresql<span style="color: #000000; font-weight: bold;">/</span>include <span style="color: #660033;">-l</span> pq <span style="color: #660033;">-o</span> PSQLTest<br />
$ .<span style="color: #000000; font-weight: bold;">/</span>PSQLTest</div></div>

<a href="http://www.askyb.com/wp-content/uploads/2012/04/26042012_3.png"><img class="aligncenter size-full wp-image-704" title="c++ postgresql example in linux" src="http://www.askyb.com/wp-content/uploads/2012/04/26042012_3.png" alt="" width="426" height="279" /></a>

If you try to break the C++ program before it remove and drop the employee table. Enter to psql command console and you should be able to read you table data as the following screenshot.

<a href="http://www.askyb.com/wp-content/uploads/2012/04/26042012_2.png"><img class="aligncenter size-full wp-image-705" title="c++ postgresql example in linux" src="http://www.askyb.com/wp-content/uploads/2012/04/26042012_2.png" alt="" width="597" height="249" /></a>

Download sample source code: <a href='http://www.askyb.com/wp-content/uploads/2012/04/PSQLTest.zip'>PSQLTest.zip</a>]]></content:encoded>
			<wfw:commentRss>http://www.askyb.com/cpp/c-postgresql-example-in-linux/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Connecting Ubuntu Server to Dropbox Account</title>
		<link>http://www.askyb.com/linux/connecting-ubuntu-server-to-dropbox-account/</link>
		<comments>http://www.askyb.com/linux/connecting-ubuntu-server-to-dropbox-account/#comments</comments>
		<pubDate>Tue, 03 Apr 2012 07:58:34 +0000</pubDate>
		<dc:creator>askyb</dc:creator>
				<category><![CDATA[Dropbox]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[dropbox]]></category>

		<guid isPermaLink="false">http://www.askyb.com/?p=656</guid>
		<description><![CDATA[I was looking for a solution to share some of my files in my Ubuntu server to others and while thinking of &#8220;cloud computing&#8221; at that time, I am thinking if i can try to share my files by linking my box to my existing dropbox account or not. Thus, i give a try and...]]></description>
				<content:encoded><![CDATA[<img src="https://www.dropbox.com/static/images/logo210.png" alt="dropbox" align="left" style="padding: 5px 5px 0px 5px;"/> I was looking for a solution to share some of my files in my Ubuntu server to others and while thinking of &#8220;cloud computing&#8221; at that time, I am thinking if i can try to share my files by linking my box to my existing dropbox account or not. Thus, i give a try and it seem it works. Here are the steps how to do it.
<span id="more-656"></span>
1. Download and extract the dropbox client with the following command:

For 32-bit:

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$ </span><span style="color: #c20cb9; font-weight: bold;">wget</span> <span style="color: #660033;">-O</span> dropbox.tar.gz <span style="color: #ff0000;">&quot;http://www.dropbox.com/download?plat=lnx.x86&quot;</span></div></div>

For 64-bit:

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$ </span><span style="color: #c20cb9; font-weight: bold;">wget</span> <span style="color: #660033;">-O</span> dropbox.tar.gz <span style="color: #ff0000;">&quot;http://www.dropbox.com/download?plat=lnx.x86_64&quot;</span></div></div>

2. Extract the file

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xzvf</span> dropbox.tar.gz</div></div>

3. Run the dropbox client deamon wiht the following command:

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$ </span>~<span style="color: #000000; font-weight: bold;">/</span>.dropbox-dist<span style="color: #000000; font-weight: bold;">/</span>dropboxd</div></div>

4. Since the server is not link to any dropbox account yet, so you will see the following message keep showing every few second:

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">This client is not linked to any account...<br />
Please visit https:<span style="color: #000000; font-weight: bold;">//</span>www.dropbox.com<span style="color: #000000; font-weight: bold;">/</span>cli_link?<span style="color: #007800;">host_id</span>=ed2986f99681f0a7b0bf1cd36c79d3e9<span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #007800;">cl</span>=en_US to <span style="color: #c20cb9; font-weight: bold;">link</span> this machine.<br />
This client is not linked to any account...<br />
Please visit https:<span style="color: #000000; font-weight: bold;">//</span>www.dropbox.com<span style="color: #000000; font-weight: bold;">/</span>cli_link?<span style="color: #007800;">host_id</span>=ed2986f99681f0a7b0bf1cd36c79d3e9<span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #007800;">cl</span>=en_US to <span style="color: #c20cb9; font-weight: bold;">link</span> this machine.</div></div>

5. You may now copy &amp; paste the link to any browser by using any computer so that it will start to link this machine to your dropbox account. You will be asking to provide your username &amp; password in order to link this to your dropbox account.

6. Once it&#8217;s successful, you will see the the following message in your Ubuntu linux machine:

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$ </span>Client successfully linked, Welcome ASKYB<span style="color: #000000; font-weight: bold;">!</span></div></div>

<a href="http://www.askyb.com/wp-content/uploads/2012/04/Ubuntu-Server-to-Dropbox-Account.png"><img class="aligncenter size-medium wp-image-668" title="Ubuntu Server to Dropbox Account" src="http://www.askyb.com/wp-content/uploads/2012/04/Ubuntu-Server-to-Dropbox-Account-300x48.png" alt="Ubuntu Server to Dropbox Account" width="300" height="48" /></a>

7. Now you may press CTRL + C to terminate the deamon process now.

8. You can manually start the service by running the following command:

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$ </span>~<span style="color: #000000; font-weight: bold;">/</span>.dropbox-dist<span style="color: #000000; font-weight: bold;">/</span>dropbox</div></div>

9. You may start to snyc and copy any file to your dropbox folder now.]]></content:encoded>
			<wfw:commentRss>http://www.askyb.com/linux/connecting-ubuntu-server-to-dropbox-account/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ SQLite Example in Linux</title>
		<link>http://www.askyb.com/cpp/c-sqlite-example-in-linux/</link>
		<comments>http://www.askyb.com/cpp/c-sqlite-example-in-linux/#comments</comments>
		<pubDate>Tue, 03 Apr 2012 02:56:28 +0000</pubDate>
		<dc:creator>askyb</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[G++]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[SQLite]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://www.askyb.com/?p=627</guid>
		<description><![CDATA[This example will demonstrate how you can write a C++ program to utilize SQLite as your program database file in Linux environment. I am using Ubuntu in this demonstration. If you are new to C++ &#38; SQLite in Ubuntu, it is advisable that you read the following article so that you have better understanding what...]]></description>
				<content:encoded><![CDATA[This example will demonstrate how you can write a C++ program to utilize SQLite as your program database file in Linux environment. I am using Ubuntu in this demonstration.

If you are new to C++ &amp; SQLite in Ubuntu, it is advisable that you read the following article so that you have better understanding what to do before you can compile your c++ program and utilize SQLite in your system.
<span id="more-627"></span>
<a title="Compiling C++ code by using g++ in Ubuntu" href="http://www.askyb.com/cpp/compiling-c-code-by-using-g-in-ubuntu/" target="_blank">Compiling C++ code by using g++ in Ubuntu</a>
<a title="Installing SQLite in Ubuntu" href="http://www.askyb.com/sqlite/installing-sqlite-in-ubuntu/" target="_blank">Installing SQLite in Ubuntu</a>

To start the demo, let&#8217;s create a cpp file now and I rename it as SqliteSample.cpp. Also, include the the following header file from sqlite to our demo program.

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#include &lt;sqlite3.h&gt;</span></div></div>

We now create a new callback function. The callback function is triggered once there is data ready to display. This usually happen when the select statement being executed.

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">// This is the callback function to display the select data in the table</span><br />
<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">int</span> callback<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span>NotUsed, <span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">**</span>argv, <span style="color: #0000ff;">char</span> <span style="color: #000040;">**</span>szColName<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> argc<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> szColName<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> argv<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>

In the main function, right after the database being open successfully and before it&#8217;s close, we need to insert the following codes into the main function so that our mission in this demonstration can be accomplished.

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;height:200px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; sqlite3 <span style="color: #000040;">*</span>db<span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>szErrMsg <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// open database</span><br />
&nbsp; <span style="color: #0000ff;">int</span> rc <span style="color: #000080;">=</span> sqlite3_open<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;askyb.db&quot;</span>, <span style="color: #000040;">&amp;</span>db<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>rc<span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Can't open database<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Open database successfully<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span> <br />
<br />
&nbsp; <span style="color: #666666;">// prepare our sql statements</span><br />
&nbsp; <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>pSQL<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">6</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
&nbsp; pSQL<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;CREATE TABLE Employee(Firstname varchar(30), Lastname varchar(30), Age smallint)&quot;</span><span style="color: #008080;">;</span><br />
&nbsp; pSQL<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;INSERT INTO Employee(Firstname, Lastname, Age) VALUES ('Woody', 'Alan', 45)&quot;</span><span style="color: #008080;">;</span><br />
&nbsp; pSQL<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;INSERT INTO Employee(Firstname, Lastname, Age) VALUES ('Micheal', 'Bay', 38)&quot;</span><span style="color: #008080;">;</span><br />
&nbsp; pSQL<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;SELECT * FROM Employee&quot;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; <span style="color: #666666;">// execute sql</span><br />
&nbsp; <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">4</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; rc <span style="color: #000080;">=</span> sqlite3_exec<span style="color: #008000;">&#40;</span>db, pSQL<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>, callback, <span style="color: #0000dd;">0</span>, <span style="color: #000040;">&amp;</span>szErrMsg<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>rc <span style="color: #000040;">!</span><span style="color: #000080;">=</span> SQLITE_OK<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;SQL Error: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> szErrMsg <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; sqlite3_free<span style="color: #008000;">&#40;</span>szErrMsg<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span> &nbsp; <br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #666666;">// close database</span><br />
&nbsp; <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>db<span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; sqlite3_close<span style="color: #008000;">&#40;</span>db<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>

To compile this program, we will need to link the sqlite3 library along with the program as following:

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$ </span><span style="color: #c20cb9; font-weight: bold;">g++</span> SqliteSample.cpp <span style="color: #660033;">-l</span> sqlite3 <span style="color: #660033;">-o</span> SqliteSample</div></div>

If you compiled and run the C++ program now, you should see a similar screenshot as following.

<a href="http://www.askyb.com/wp-content/uploads/2012/04/c++-example-in-ubuntu_pic1.png"><img class="aligncenter size-medium wp-image-643" title="c++ example in ubuntu" src="http://www.askyb.com/wp-content/uploads/2012/04/c++-example-in-ubuntu_pic1-300x90.png" alt="c++ example in ubuntu" width="300" height="90" /></a>

Open the askyb.db file from Sqlite console will show you the data as following:

<a href="http://www.askyb.com/wp-content/uploads/2012/04/c++-example-in-ubuntu_pic2.png"><img class="aligncenter size-full wp-image-644" title="c++ example in ubuntu_pic2" src="http://www.askyb.com/wp-content/uploads/2012/04/c++-example-in-ubuntu_pic2.png" alt="c++ example in ubuntu" width="416" height="168" /></a>

Download sample source code: <a href="http://www.askyb.com/wp-content/uploads/2012/04/SqliteSampleLinux.zip">SQLiteSample.cpp</a>]]></content:encoded>
			<wfw:commentRss>http://www.askyb.com/cpp/c-sqlite-example-in-linux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Installing SQLite in Ubuntu</title>
		<link>http://www.askyb.com/sqlite/installing-sqlite-in-ubuntu/</link>
		<comments>http://www.askyb.com/sqlite/installing-sqlite-in-ubuntu/#comments</comments>
		<pubDate>Mon, 02 Apr 2012 14:01:45 +0000</pubDate>
		<dc:creator>askyb</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[SQLite]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://www.askyb.com/?p=615</guid>
		<description><![CDATA[Installing SQLite in Ubuntu is pretty simple and all you have to do is using the following command to download and install the package. $ sudo apt-get install sqlite3 libsqlite3-dev Once it&#8217;s downloaded and installed, type the following command to create a SQLite database file. I called the database file &#8212; askyb.db. You should see...]]></description>
				<content:encoded><![CDATA[Installing SQLite in Ubuntu is pretty simple and all you have to do is using the following command to download and install the package.
<span id="more-615"></span>

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$ </span><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get install</span> sqlite3 libsqlite3-dev</div></div>

Once it&#8217;s downloaded and installed, type the following command to create a SQLite database file. I called the database file &#8212; askyb.db. You should see the following screenshot:

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$ </span>sqlite3 askybdb.db</div></div>

<a href="http://www.askyb.com/wp-content/uploads/2012/04/install-sqlite-in-ubuntu.png"><img class="aligncenter size-full wp-image-619" title="install sqlite in ubuntu" src="http://www.askyb.com/wp-content/uploads/2012/04/install-sqlite-in-ubuntu.png" alt="install sqlite in ubuntu" width="478" height="178" /></a>

Use the ls command to list the file created and you should obsered that askyb.db was created in the directory.]]></content:encoded>
			<wfw:commentRss>http://www.askyb.com/sqlite/installing-sqlite-in-ubuntu/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Compiling C++ code by using g++ in Ubuntu</title>
		<link>http://www.askyb.com/cpp/compiling-c-code-by-using-g-in-ubuntu/</link>
		<comments>http://www.askyb.com/cpp/compiling-c-code-by-using-g-in-ubuntu/#comments</comments>
		<pubDate>Mon, 02 Apr 2012 08:26:29 +0000</pubDate>
		<dc:creator>askyb</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[G++]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[Compile]]></category>

		<guid isPermaLink="false">http://www.askyb.com/?p=581</guid>
		<description><![CDATA[This article aim to share a little bit of knowledge on how to compile c++ code by using gcc compiler. In case you never experience how to compile c++ code in linux environment, this article will guide you through install of gcc and compile your very first C++ program in Ubuntu. Here it go the...]]></description>
				<content:encoded><![CDATA[<img src="http://gcc.gnu.org/img/gccegg-65.png" alt="gcc" align="left" style="padding: 3px 3px 0px 3px;"/>This article aim to share a little bit of knowledge on how to compile c++ code by using gcc compiler. In case you never experience how to compile c++ code in linux environment, this article will guide you through install of gcc and compile your very first C++ program in Ubuntu. Here it go the steps:
<span id="more-581"></span>
1. Install Ubuntu build-essential package so that it install gcc compiler into the system. Use the following command to download the package.

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$ </span><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get install</span> build-essential</div></div>

2. Create the C++ file. Let&#8217;s call the c++ file &#8211; &#8220;HelloWorld.cpp&#8221;. I am using vi to create the following C++ file. You may use your preference editor.

<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#include &lt;iostream&gt;</span><br />
<br />
using namespace std<span style="color: #339933;">;</span><br />
<br />
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;cout <span style="color: #339933;">&lt;&lt;</span> <span style="color: #ff0000;">&quot;Hello World<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

3. Once the cpp file is saved and it&#8217;s ready to compile now by using g++. Use the following command to compile our very first cpp file now.

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$ </span><span style="color: #c20cb9; font-weight: bold;">g++</span> HelloWorld.cpp <span style="color: #660033;">-o</span> HelloWorld</div></div>

4. An executable HelloWorld is produced and ready to be executed by using the following command.

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$ </span>.<span style="color: #000000; font-weight: bold;">/</span>HelloWorld</div></div>

5. The output of the executable program is displayed as follow:

<a href="http://www.askyb.com/wp-content/uploads/2012/04/g++-compiling.png"><img class="aligncenter size-full wp-image-597" title="Compiling C++ code by using g++ in Ubuntu" src="http://www.askyb.com/wp-content/uploads/2012/04/g++-compiling.png" alt="Compiling C++ code by using g++ in Ubuntu" width="449" height="162" /></a>]]></content:encoded>
			<wfw:commentRss>http://www.askyb.com/cpp/compiling-c-code-by-using-g-in-ubuntu/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
