Saturday, May 1, 2010

Compile 1st C++ Program in Ubuntu

Having installed Ubuntu for quite a long time, and I finally have learnt how to program C++ in this Unix environment. 

I type my C++ program using gedit that is available under Ubuntu.
I tried this code first hand and it does not worked.

$ gcc -o  hw1 hw1.cpp

I found out the reason is that gcc is used for only C program files and not C++. The following command is actually worked out.

$ g++ -o  hw1 hw1.cpp

This -o basically used to suggest name for the executable file that g++ would create. In case if you typed something like the following:

$ g++ hw1.cpp

You would be having a a.out in the same directory as the source C file. This is the default name of the executable that g++ creates. This would create problems when you compile many programs in one directory. So you override this with the -o option followed by the name of the executable.

Would create an executable by the name hw1 for your source code named hw1.cpp.

Running the executable that you created is as simple as typing the following at the prompt.

$ ./hw1
$ ./a.out

Or whatever you named your executable. 


Post a Comment