PDA

View Full Version : How to write and run c/c++ programs on ubuntu...



dilipkumarthota
May 1st, 2011, 11:45 AM
Hii Uforum,
can any one tel me how to write and execute c/c++ code on ubuntu.. and please do specify the software to be dowloaded...
thanking you,,:P

tb13
May 2nd, 2011, 04:24 PM
Sure thing,

1) First of all you are going to want to download and install the C and C++ compilers. For this you need to enter this code into the terminal:

sudo aptitude install build-essential

2) For C Programming to write your c program enter the following:

sudo gedit first.c

then enter this:

#include <stdio.h>

int main()
{
printf("Hello World\n");
return 0
}

3) Next you need to compile your C Code that you have just entered. To do this type in the following command:

cc -c first.c

This will create an object file, to create an executable file type in:

cc -o first.c

4) To run the program finally type:

./first

tb13
May 2nd, 2011, 04:29 PM
Also, for C++,

sudo gedit first.cpp

#include <iostream>

int main()
{
cout << "Hello World!" << endl;
return 0;
}

Then a slight difference in the compiling type:

g++ first.cpp -o programname

then:

./programname

ApOgEEs
May 28th, 2011, 12:41 AM
My step by step instructions.

1. Install the 'build-essential' package.

$ sudo apt-get install build-essential

2. Write your first C program.

gedit myhello.c
paste this code into it, and save


#include <stdio.h>

main()
{
printf("Hello Ubuntu Lover!\n");
}


3. Compile your first C program


$ gcc myhello.c -o myhello


4. Run your first C program


$ ./myhello
Hello Ubuntu Lover!


5. Write your first C++ program

gedit myhello.cpp
paste this code into it, and save

#include <iostream>
using namespace std;

int main()
{
cout << "Hello Ubuntu Lover in C++" << endl;
return 0;
}

6. Compile your first C++ program

g++ myhello.cpp -o myhellocpp

7. Run your first C++ program

$ ./myhellocpp
Hello Ubuntu Lover in C++


Good Luck!

rpmp
May 28th, 2011, 12:51 AM
look for C/C++ tutorials to learn to write programs and to compile use CODEblocks ide then when you finish writing the program (in code blocks) hit Build then Build and run and with that you compile it and executed it.

Petrolea
May 28th, 2011, 12:51 PM
Also, for C++,

sudo gedit first.cpp

#include <iostream>

int main()
{
cout << "Hello World!" << endl;
return 0;
}

Then a slight difference in the compiling type:

g++ first.cpp -o programname

then:

./programname

Why would you want to run gedit with 'sudo'? And even if you would have to, it's still recomended to run it with gksu.