Monday, 27 May 2013

Instructions to compile and execute C/C++ program in linux




Step 1 : To compile any C/C++ program, C/C++ compiler like gcc or g++ are required, if you do not have  
              one you can download it by just typing below mentioned command in the terminal,

                                                  For C :      sudo  apt-get  install  gcc
                                                 For C++ :   sudo  apt-get  install  g++


Step 2 : Having downloaded C/C++ compiler, compile your program as

                                                  For C :       gcc  myprogram.c
                                                 For C++ :   g++  myprogram.cpp


Step 3
: Now since your program compiled successfully, a file by the name a.out being generated,                          
              which is your executable file. In order to execute this type
           
                                                                            ./a.out
             

As like me, I hope that you too don't like the name of your executable file to be a.out. In order to          
             give your own name for the executable file, type the following command in the terminal

                                              gcc  -o  <the name you wish to give>  <filename>



             ex:- gcc  -o  jamesbond  myprogram.c

             Now your executable file is jamesbond rather than a.out which can be executed as    

                                                                        ./jamesbond                     
             
             Note : use .c extension to store C files and .cpp for C++ files


Leave your doubts or suggestions in comments section..

No comments:

Post a Comment