Sunday, March 21, 2010

Using C/C++ In Linux

Linux is the most preferred OS among programmers. It's good to know how to write and compile C/C++ programs in linux. There are a plenty of compilers available for C/C++. Most of the begining students will be using Windows based IDE. There most of them use TURBO C for compiling. In linux also we are having IDE's for C/C++. But for basic programming other than application launching, its enough to write your programes in a text editor and then compile. Even though there are many text editors most common are Vi editor and Vim editor.I prefer to use Vim.

First open the terminal.For Ubuntu users goto Applications->Accessories->terminal.
First create a file for writing the program as follows.

Syntax : vim filename.c

if you are writting c++ program change the extention to .cpp. Now the editor screen comes. Press " i " to start writing. Now you can write your c/c++ programs in it. When you finished writting the pograms press escape key and type " :wq " for saving your program and to quit from the editor to the terminal.

Now we have to compile this program. Here the compiler used is GCC( GNU Compiler collection) for C and G++ for C++. To complie a C program

Type "gcc filename.c"

for C++

"g++ filename.cpp"

This will list all the syntax errors you have made. If there are no errors, an executable object file will be created with name "a.out". Now to execute this type

" ./a.out "

Now every thing is over and you will get the required output. A sample Hello world program in c and c++ are given below.

C Program

Type vim hello.c

type " i " to start writing program.

#include
main()
{
printf("Hello World");
}

Now press ESC and type


:wq


and then type


gcc hello.c


./a.out


C++ Program


Type


vim hello.cpp


type "i" to insert the program


#include

main()

{

using namespace std;

cout<<"Hello World";

}


Now press ESC and type

:wq

then type

g++ hello.cpp

./a.out

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | coupon codes