How to write and compile C++ programs

Started by sukishan, Jul 10, 2009, 09:49 PM

Previous topic - Next topic

sukishan

Complie and Write C++

In order to run a program and see it doing wonderful things, you should first write the program. The program can be written in any text editor, such as vi and emacs in Unix environment and using command prompt in DOS. There are also several Integrated Development Environment (IDE) packages available which provide a complete programming environment for C++ in which you can write, compile, run, and debug your program.

C++ programs are saved with extensions .C, .cc, .cpp, .cxx depending on the platform you are working upon.

Once you have saved a program, next stage is compiling it using a compiler which converts your C++ program into the object code which the computer can understand.


Compile using g++ compiler

If you are using UNIX environment and using g++ compiler, then compiling your C++ program is simple. After you have saved the program file, simply type the command

g++ program.cc –o program

where program is the name of the program. For example the name of our first program is first, then save it using extension "first.cc". To compile, type

g++ first.cc –o first

To run the program, type first or ./first at the command prompt and your program will run.
A good beginning makes a good ending