Compiling and Running C/C++ programs in Ubuntu 10.10

Wednesday, October 20, 2010

 Today I wish to tell about how to compile and  run C/C++ programs in ubuntu 10.10.If you are a developer of C/C++ you need a compiler for the development of the programs.Ubuntu 10.10 has the compiler inbuilt with the operating system.Incase if the compiler is missing you download the package and able to install.
To download -Open the terminal and type the following.
                           sudo aptitude install build-essential
this will install the required packages of C/C++ compilers.

How to compile and run the C program
1.From the apllications open the terminal and type sudo gedit first.c
   This will open a new file type the C program and save it
  for eg. #include<stdio.h>
               int main()
               {
                printf("Hello,world\n");
                 return 0;
                 }
2.for compiling type cc -c first.c
  that will create an object file and add to the library.
3.then create an executable file using the following command
       cc -o first first.c
4.Then run the Program by typing 
     ./first
5.Output should be asfollows
        Hello,world

How to compile the C++ program.
If you want to run a C++ program you must need a g++compiler.
1.create a file using sudo gedit first.cpp
   eg.#include<iostream.h>
       int main() 
         {
          std:: cout << "Hello world!" <<std::endl;
          return 0;
           }

Run your C++ program using the following command
g++first.cpp -o test
then 
./first
Output will be as follows
Hello, world!


Tags:Ubuntu  compiling C & C++ in ubuntu 



 
           



 

0 comments:

Post a Comment