How to Install C and C++ Compilers in Ubuntu and testing your first C and C++ Program
Sponsored Link
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!
working for small programs but giving error for programs of object oriented programming although programs are running in turbo C/C++ without giving any error….
this is a very useful web site to improve knolege aabout ubuntu
g++ is not working. error cot<< is not in scope.
i get these errors. why?
sitt.cpp: In function ‘int main()’:
sitt.cpp:4: error: expected primary-expression before ‘|’ token
that’s great
It says
”
The program ‘g++’ can be found in the following packages:
* g++
* pentium-builder
Try: sudo apt-get install
g++: command not found
“
This is a good start….!!! thanks !! keep rocking!!!
I am using Ubuntu 9.10 Karmic Koala that I have downloaded from internet. When i try to install g++ compiler I got the following message:
The program ‘g++’ can be found in the following packages:
* g++
* pentium-builder
Try: sudo apt-get install
g++: command not found
I can not install this compiler, I have already tried the following commands:
sudo apt-get install g++
sudo apt-get install pentium-builder
sudo apt-get install build-essentials
and none of them worked, it always appears a message with something like, package not found.
Anybody can help me.
Note down the steps, the first step is to install updates through;
Update Manager;
Now type in the Terminal;
sudo apt-get install build-essential
NOTE: you spelt it as build-essentials, here s is not required in the end of the word build-essential.
It will automatically install g++
what the meaning of this problem… pleasa help me… huhuhu
what should i do..??
[email protected]:~$ sudo gedit first.cpp
[email protected]:~$ g++ first.cpp -o test
The program ‘g++’ can be found in the following packages:
* g++
* pentium-builder
Try: sudo apt-get install
bash: g++: command not found
[email protected]:~$ sudo apt-get install g++
E: Could not get lock /var/lib/dpkg/lock – open (11 Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?
[email protected]:~$
an other program or service is useing the update directory.
you can only have one program use the directory at once
so exit all other update/package managers/ installers
then try again
i need c,c++ commands work in ubuntu ,so plz send me a list of commands.and what are the probelms occur and how to retify that,please send me that.
For all those who are not having working internet connection and want to install g++ on their computer,there is a way.
First put your ubuntu linux live cd in the cd-rom and close it.
Go to the synaptic package manager, and click add the cd-rom from one of the classical menus on the top. After u click that button, synaptic will add all the files of the cd and will ask you. Do you want to add another cd-rom. click no.
Now having the live cd in the cd-rom, go to synaptic package manager, click the search button and type g++ in it.
when g++ appears in the list, tick mark it and click the apply button. Now everything should go smoothly. Now go to terminal and click g++. you should get no input files. This means you have installed c++ compiler in your computer. Now compile programs as usual.
If the result is positive, reply me.
i will also tell how to install java,oracle,lamp(linux apache mysql php) and flex in your ubuntu linux.
Hi,
I am build essential is not getting installed.I am getting the error
“E: Write error – write (28 No space left on device)”
Can you please help me with this?
Thanks
kp,
What a brilliant idea.
You earn kudos.
Please guide installing java, oracle etc. also
Thanks
I am facing problems while compiling cpp files.
I have installed the build-essential packages but I am getting error messages while compiling the program
fact.cpp:1:21: error: iostream.h: No such file or directory
fact.cpp:2:18: error: conio.h: No such file or directory
fact.cpp:3: error: ‘::main’ must return ‘int’
fact.cpp: In function ‘int main()’:
fact.cpp:8: error: ‘clrscr’ was not declared in this scope
fact.cpp:9: error: ‘cout’ was not declared in this scope
fact.cpp:10: error: ‘cin’ was not declared in this scope
fact.cpp:13: error: ‘getch’ was not declared in this scope
I dont know what to do please help me
If it gives a “g++ Can be found in …” message,you DIDNT follow the directions. It says aptitude not apt-get, I have KK and that worked. Prasanth: iostream.h should just be iostream.
Prasanth: you need stdio.h for getch().
quite useful description…
thank you..
Thanks 🙂 This has come in handy. I will favourite this.
We need techs to help us finalize our operating system.
Ok thanks guys can u tell me how include header file in it.
Header file is prepared personally by me
like (file.h)
ok guys, working hard on the autoexec.bat file to install all necessary hardware and compiling ubuntu with UNIX Kernal. Don’t want to reinvent the wheel here. Anybody have it done and wants to makes some $$$?
1.cpp:2:21: error: iostream.h: No such file or directory
1.cpp:31: error: return type specification for destructor invalid
1.cpp: In member function ‘void Stack::Top()’:
1.cpp:67: error: ‘cout’ was not declared in this scope
1.cpp:67: error: ‘endl’ was not declared in this scope
1.cpp:71: error: ‘cout’ was not declared in this scope
1.cpp:71: error: ‘endl’ was not declared in this scope
1.cpp: In member function ‘void Stack::Insert()’:
1.cpp:81: error: ‘cout’ was not declared in this scope
1.cpp:81: error: ‘endl’ was not declared in this scope
1.cpp:85: error: ‘cout’ was not declared in this scope
1.cpp:85: error: ‘endl’ was not declared in this scope
1.cpp:86: error: ‘cin’ was not declared in this scope
1.cpp: In member function ‘void Stack::Delete()’:
1.cpp:96: error: ‘cout’ was not declared in this scope
1.cpp:96: error: ‘endl’ was not declared in this scope
1.cpp:100: error: ‘cout’ was not declared in this scope
1.cpp:100: error: ‘endl’ was not declared in this scope
1.cpp: In member function ‘void Stack::Display()’:
1.cpp:111: error: ‘cout’ was not declared in this scope
1.cpp:111: error: ‘endl’ was not declared in this scope
1.cpp:114: error: ‘cout’ was not declared in this scope
1.cpp:114: error: ‘endl’ was not declared in this scope
1.cpp:119: error: ‘cout’ was not declared in this scope
1.cpp: At global scope:
1.cpp:122: error: ‘::main’ must return ‘int’
1.cpp: In function ‘int main()’:
1.cpp:128: error: ‘cout’ was not declared in this scope
1.cpp:128: error: ‘endl’ was not declared in this scope
1.cpp:130: error: ‘cin’ was not declared in this scope
“sudo gedit first.c” to open an editor!!
why are you opening an editor with root privilages and worse, put it into a tutorial for beginners.
didnt this work??
$> gedit first.c
Please dont misunderstand me, I do appreciate your putting a blog to help beginners but this is a glaring blunder and a very bad way to tell a beginner to start.
@venkatesh
you are trying to compile cpp file. In cpp, the standard libraries like iostream dont have a .h extension (unlike c). Try
#include
I installed g++ for compiling c++ program in ubuntu 9.10, but we get the following errors…..
program is
#include
main()
{
int i;
cout<>i;
cout<<"\n number is"<<i;
}
after compile
p1.cpp:1:21: error: iostream.h: No such file or directory
p1.cpp: In function ‘int main()’:
p1.cpp:5: error: ‘cout’ was not declared in this scope
p1.cpp:6: error: ‘cin’ was not declared in this scope
where program name is p1.cpp
please replay me……
1. install gcc/g++
$>sudo apt-get install g++
$>sudo sudo aptitude install build-essential
2. write code using vim or gedit
/*——————-coba.cpp—————*/
#include
int main(){
int a,b;
a=10;
b=a*8;
cout<<"Belajar C++ : "<<b<g++ -c coba.cpp
coba.cpp: In function ‘int main()’:
coba.cpp:8: error: ‘cout’ was not declared in this scope
coba.cpp:8: error: ‘endl’ was not declared in this scope
4. i’m try to using this “using namespace std;” to correct that problem, like this
/*——————-coba.cpp—————*/
#include
#include
using namespace std;
int main(){
int a,b;
a=10;
b=a*8;
cout<<"Belajar C++ : "<<b<g++ -c coba.cpp
6. build
$>g++ coba.cpp -o coba
$>./coba
Belajar C++ : 80
7. aha it’s work…
Have fun… {blepotan banget bahasa inggris-e anu genah ora teyeng koh xixixi)
I have installed Ubuntu 10.4 Lynx,
Please help me how to install Anjuta
Okay, I noticed one of the common problems. For those with the error “cout not declared”; it’s because you need to declare it (obviously). So after:
#include
go ahead and write:
using namespace std;
This basically tells the compiler to use basic library functions, “std”, from the standard library. Blah blah, there
Thanks!!! My first c/c++ program in ubuntu!!!! I want to learn more (only in linux). Please, give me more articles or tutorials in c/c++ for linux, i have downloaded an compiled Code:Blocks.
See you later!!