Saturday, May 28, 2011

writing a C/C++ program in ubuntu

Gosh!! this took me a hell lot of searches. Nowhere I could find the proper way to do it. Not even the linux gcc tutorial explained it satisfactorily. After days of searches, I luckily landed on the physics forum where a guy ('' neilay from India \n") had answered beautifully. In case you are very new to linux, this might help you.

Make sure you have the necessary gcc files installed.
To install required packages for c and c++ compilers
in the terminal window, type: sudo apt-get install build-essential

To Program in Ubuntu, follow the following steps:
1. Click on Places on menu bar > go to Home Folder.
2. A new window appears.
3. Here rightclick on your mouse button > Create Document > click on Empty File.
4. Select the file > press F2 > rename it as monika.c
5. Double click on monika.c
(steps 1 to 5 is equivalent to writing: gedit monika.c in the terminal window)
6. There you type the simplest 

C Code:
int main()
printf("my first c program \n");
return 0;

the C++ code:
using namespace std;

int main()
cout<<("my first c++ program \n");
return 0;

7. Save and minimize everything.
8. Click on Applications on the menu bar > Accessories > click on Terminal.
9. A new window appears and since my username is “monika”, monika@ubuntu: ~ appears on its title bar as default – this will be different in your case.
10. To compile a C code: type: gcc monika.c and press the Enter key.

      To compile a C++ code: type: g++ monika.c and press the Enter key.
11. Wait and if your code is correct no error will be found and simply monika@ubuntu: ~ $ will be
appeared and indicates you can execute this compiled file.
12. Now type: ./a.out and press the Enter key and see how nicely your file is executed!
13. This procedure must work with both Ubuntu and Fedora. If it is not working, then definitely
your OS has got problem with it.

14. That's all about Ubuntu C Programming.

Happy programming!

No comments:

Post a Comment