I want to test C++ on Ubuntu 9.04.
So, follow this post from www.ubuntugeek.com. This post said that………
If you are a developer you need C and C++ Compiler for your development work. In Ubuntu you can install the build-essential for C and C++ compilers.
Install C and C++ Compilers in Ubuntu
sudo aptitude install build-essential
This will install all the required packages for C and C++ compilers
Testing C and C++ Programs
-
Compiling Your first C Programs
Now you need to open first.c file
sudo gedit first.c
add the following lines save and exit the file
#include <stdio.h>
int main()
{
printf(“Hello, World!\n”);
return 0;
}
Firstly compile the code using the following command
cc -c first.c
that would produce an object file you may need to add to the library.
then create an executable using the following command
cc -o first first.c
Now run this executable using the following command
./first
Output should show as follows
Hello, world
-
Compiling your first C++ program
If you want to run c++ program follow this procedure
g++ is the compiler that you must use.
you should use a .cpp file extension rather than a .c one
You need to create a file
sudo gedit first.cpp
add the following lines save and exit the file
#include <iostream>
int main()
{
std::cout << “Hello, World!” << std::endl;
return 0;
}
Run your C++ Program using the following command
g++ first.cpp -o test
./test
Output should show as follows
Hello World!
And I test on my Toshiba laptop.
thanwinnaing@thanwinnaing-laptop:~$ sudo apt-get install build-essential
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following extra packages will be installed:
dpkg-dev g++ g++-4.3 libstdc++6-4.3-dev patch
Suggested packages:
debian-keyring g++-multilib g++-4.3-multilib gcc-4.3-doc libstdc++6-4.3-dbg
libstdc++6-4.3-doc diff-doc
The following NEW packages will be installed:
build-essential dpkg-dev g++ g++-4.3 libstdc++6-4.3-dev patch
0 upgraded, 6 newly installed, 0 to remove and 0 not upgraded.
Need to get 6270kB of archives.
After this operation, 21.4MB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://ftp.tcc.edu.tw jaunty/main libstdc++6-4.3-dev 4.3.3-5ubuntu4 [1356kB]
Get:2 http://ftp.tcc.edu.tw jaunty/main g++-4.3 4.3.3-5ubuntu4 [4162kB]
Get:3 http://ftp.tcc.edu.tw jaunty/main g++ 4:4.3.3-1ubuntu1 [1438B]
Get:4 http://ftp.tcc.edu.tw jaunty/main patch 2.5.9-5 [100kB]
Get:5 http://ftp.tcc.edu.tw jaunty/main dpkg-dev 1.14.24ubuntu1 [643kB]
Get:6 http://ftp.tcc.edu.tw jaunty/main build-essential 11.4 [7172B]
Fetched 6211kB in 9min 57s (10.4kB/s)
Selecting previously deselected package libstdc++6-4.3-dev.
(Reading database … 103569 files and directories currently installed.)
Unpacking libstdc++6-4.3-dev (from …/libstdc++6-4.3-dev_4.3.3-5ubuntu4_i386.deb) …
Selecting previously deselected package g++-4.3.
Unpacking g++-4.3 (from …/g++-4.3_4.3.3-5ubuntu4_i386.deb) …
Selecting previously deselected package g++.
Unpacking g++ (from …/g++_4%3a4.3.3-1ubuntu1_i386.deb) …
Selecting previously deselected package patch.
Unpacking patch (from …/patch_2.5.9-5_i386.deb) …
Selecting previously deselected package dpkg-dev.
Unpacking dpkg-dev (from …/dpkg-dev_1.14.24ubuntu1_all.deb) …
Selecting previously deselected package build-essential.
Unpacking build-essential (from …/build-essential_11.4_i386.deb) …
Processing triggers for man-db …
Setting up patch (2.5.9-5) …
Setting up dpkg-dev (1.14.24ubuntu1) …
Setting up libstdc++6-4.3-dev (4.3.3-5ubuntu4) …
Setting up g++-4.3 (4.3.3-5ubuntu4) …
Setting up g++ (4:4.3.3-1ubuntu1) …
Setting up build-essential (11.4) …
thanwinnaing@thanwinnaing-laptop:~$
But, I test with this code only for C++ which is the same above.
#include<iostream>
using namespace std;
int main()
{
cout << “Hello, World!” << endl;
return 0;
}