Install C and C++ Compilers in Ubuntu
sudo aptitude install build-essential
This will install all the required packages for C and C++ compilers
Testing C and C++ Programs
Compiling Your first C Programs
Now you need to open first.c file
sudo gedit first.c
add the following lines save and exit the file
Firstly compile the code using the following command
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
Now run this executable using the following command
./first
Output should show as follows
Hello, world
Compiling your first C++ program
If you want to run c++ program follow this procedure
g++ is the compiler that you must use.
you should use a .cpp file extension rather than a .c one
You need to create a file
sudo gedit first.cpp
add the following lines save and exit the file
Run your C++ Program using the following command
g++ first.cpp -o test
./test
Output should show as follows
Hello World!





@ Pral
Hey in unix system there are no drive concept (like c:\,d:\ etc). all are these declare as a file system. you can find them at /media/(file system name). you want to paste some .h file? but there are no need for this because ubuntu have preinstall these library files.
[Reply]
Thanks for all.
[Reply]
It is useful to install manpages in addition to the compilers. You can do this for example using the following command:
sudo apt-get install build-essential manpages-dev
…or if you have already installed the build-essential package:
sudo apt-get install manpages-dev
[Reply]
Thanks for all
[Reply]
STEPS TO COMPILE C AND C++ ON UBUNTU
For compilation of c code:
1.Open : applications>accessories>terminal
2.Type the following command in the terminal
gedit name of ur file.c
// I HAVE GIVE MY FILE NAME AS main.c
For me i will type gedit main.c inside terminal
3.Write ur code inside the gedit editor:
#include
#include
int main(int argc, char** argv)
{
printf(“NAMAH SHIVAY\n”);
return 0;
}
4.save the code by pressing ctrl+s
5.In the terminal itself goto :File>Open terminal and do as follows in the newly opened terminal
6.Compile the code using the following command :
cc -c main.c
7.This would produce an object file now you may need to add to the library.
Then create an executable using the following command :
cc -o main main.c
8.Now run this executable using the following command :
./main
FOR ANY DOUBTS MAIL ME
dheeraj17joshi@gmail.com
[Reply]
sujay Reply:
November 22nd, 2011 at 4:03 pm
thank u dude working fine
[Reply]
sudo aptitude install build-essential
worked like a charm. thanks!
[Reply]
sudo aptitude install build-essential will not work any more.you should use sudo apt-get install build-essential
[Reply]
THanks
[Reply]
“sudo aptitude install build-essential
This will install all the required packages for C and C++ compilers”
For this do u write that code in the terminal to download the compiler
and if so the terminal says that
the sudo:aptitude: command not found
plzz help ty
[Reply]
jirO Reply:
December 28th, 2011 at 5:12 am
try,
sudo apt-get install build-essential
[Reply]
Guys i had a new idea install wine & get borland c 5.02.then install it with wine. it is most compatible for ubuntu 10.04 & ubuntu 11.04. if you use ubuntu 10.10 you must install borland c 4.5(or)turbo c 4.5.
[Reply]
guys i think it will works for other versions of ubuntu too
[Reply]
Hey Guys, thanks for the help! This worked for me and I ran my C++ program. Once you create the editor file in Ubuntu, how would open the file back up?
[Reply]
Super helpful. Thanks!
At patricia; Just open up (sudo gedit yourfile.cpp) and keep editing. the built (compiled) version of your code is a different animal all together from the cpp file that you are editing.
[Reply]
thanks
[Reply]
try this code:
#include
void main()
{
printf(“hello world!”);
getchar();
}
is the same but you dont have to write “return 0;”
[Reply]
Hey, when I run the program, I typed as follow:
./test
it shows the error message ” bash: ./MyFirstPointer: No such file or directory”
Help me. What I need to do? Thanks a lot!
[Reply]
AH Reply:
October 24th, 2011 at 5:17 pm
sorry, I typed as
./MyFirstPointer
bash: ./MyFirstPointer: No such file or directory
it’s what I got!
[Reply]
That really helpful
now I can develop apps using C and C++
Thanks
[Reply]
Hello-
HELP! Sometime soon, I would like to port some of my old C code projects to the Ubuntu environment, and compile them into executables there. Is there some documentation in one piece somewhere explaining the vagaries of the available standard compiler? Things like how to handle I/O devices other than console, floating point and other numerical precision options, interfacing to assembly language modules (and incidentally assemblers, preferably macro, that are available and compatible with the compiler). These and 100 other questions. Finding answers online might be possible for some things, but is tedious. Sorry my cyber-youth friends, but nothing beats a real book! A solid recommendation of such a book (perhaps one of the O’Reilly series) without a lot of generalized hand waving would be appreciated by this old timer, though Linux newbee.
[Reply]
The C++ code example shown above won’t compile with g++. To make it work, the vertical bar after the left brace in the fourth line must be deleted.
The “bar” may actually be a screen capture of the text cursor. Why were screen images used in the code examples in this article, instead of actual text?
[Reply]
Andrew P. Reply:
January 19th, 2012 at 1:20 am
Here’s the corrected code:
#include
int main()
{
std::cout << "Hello, world!" << std::endl;
return 0;
}
[Reply]
(The comment form sorely needs a “preview” feature so that one will know how it will look before posting.)
[Reply]
lot of thanks for helping in installing “build-essential”package now I can make run program in c and c++ from linux
thanx once again
[Reply]
Following is the code that I type in terminal.
I have made hello.c file under /home/rana/C/ using gedit. please help
rana@rana-FS-1050:~$ cc -c hello.c
cc: error: hello.c: No such file or directory
cc: fatal error: no input files
compilation terminated.
rana@rana-FS-1050:~$ cc -c /home/rana/C/hello.c
rana@rana-FS-1050:~$ ./hello
bash: ./hello: No such file or directory
[Reply]