Monday, December 13, 2010

How to run a C++ program in linux

Install g++ (Ubuntu) or gcc-c++ (RedHat or fedora) package using 
apt-get or yum.
sudo apt-get install g++
or 
yum install gcc-c++
Write a C++ program using your favorite editor.
emacs hello_world.cpp
or
vi hello_world.cpp
or
gedit hello_world.cpp
 #include <iostream>  
  using namespace std; 
  int main() { 
 
      cout << "Hello World!\n";
         
      return 0;
  }
The following command will compile your program 
and create an executable called a.out
   g++ hello_world.cpp
The following command will execute your program.
  ./a.out
Congratulations! You are done!
 

3 comments:

  1. how to run a grahpics program of c++ in linux

    ReplyDelete
  2. if u change the output file from a.out then use
    g++ filename.cpp -o newname

    now u'll get a compiled file named newname
    u can execute is by running the following command
    ./newname

    ReplyDelete
  3. thnx nikita...4 a nice trick.. :))

    ReplyDelete