Wednesday, 16 October 2013

How to Install C and C++ Compilers in Ubuntu and testing your first C and C++ Program

Ubuntu c & C++ compiler for developers on c & C++. here in ubuntu you can easily build the things for c & c++ compilers.

Install C and C++ Compilers in Ubuntu.


Install C and C++ Compilers in Ubuntu.

Image credit

 
sudo aptitude install build-essential

This will install all the basic packages required for Compiling Your C Programs.

Now you have to open First.c file.
sudo gedit first.c

Add The following line to test your c compiler is working or not and save the file and exit.
#include <stdio.h>
int main()
{
printf ("hello ,worl\n")
return 0:
}

To compile the code you have to use following code.
cc -c first.c

That would produce an object file you may need to add to the library.

Then create an executable using the following command.
cc -o first first.c

Run the executable file using the following command.
./first

Out put will return as

Hello, world

Compiling your first C++ program.


Are you really wanted to test C++ program

For this program to execute we use g++ compiler.
You give the file extension as .cpp.
You have to create a file with First.cpp.
sudo gedit first.cpp

Add the following lines save and exit the file .
#include <iostream>
int main()
{
std::cout<< "Hello world!" << std::endl:
return 0;
}

Run your C++ Program using the following command.
g++ first.cpp -o test

 
./test

Output should show as follows

Hello World!

No comments:

Post a Comment