LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-26-2009, 04:20 AM   #1
shubham chakraborty
LQ Newbie
 
Registered: Nov 2009
Posts: 2

Rep: Reputation: 0
c++ program not running in ubuntu


I am trying to run a simple program in ubuntu. GCC is installed. But still the following errors are being flashed .

the following is my simple program:
I typed the below given program in text editor and saved it in \Desktop\TC
as hello1.C

Code:
#include<iostream.h>
int main()
{
cout << "Hello, world" << "\n";
return 0;
}
then i opened the terminal and went to TC. there i gave the following command.

Code:
$ g++ hello1.C -o hello
but i got the following messages

Code:
hello1.C:1:21: error: iostream.h: No such file or directory
hello1.C: In function ‘int main()’:
hello1.C:4: error: ‘cout’ was not declared in this scope
hello1.C:4: error: ‘\n’ was not declared in this scope
what does all this mean?? and why is this error here???

please help

i am using UBUNTU 9.04

Last edited by XavierP; 11-26-2009 at 06:12 AM.
 
Old 11-26-2009, 05:38 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
You need to use "iostream" rather than "iostream.h". I believe this is part of the newer C++ standard (ANSI C++?), but I'm not sure. Some distros provide the older headers (i.e. the ones with the ".h") for backwards compatibility, but it seems that Ubuntu isn't one of them. Also, you'll need a "using namespace std;" under your include directives and before main(), i.e.

Code:
#include <iostream>
using namespace std;

int main()
{
  // Code here
}
This is because cout is declared in the "std" namespace, so you need to specify that you want to use that namespace.
 
1 members found this post helpful.
Old 11-26-2009, 06:12 AM   #3
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Lubuntu
Posts: 19,174
Blog Entries: 4

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 11-26-2009, 07:05 AM   #4
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,051

Rep: Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100
Quote:
Originally Posted by shubham chakraborty View Post
I am trying to run a simple program in ubuntu. GCC is installed. But still the following errors are being flashed .
I hope you use Synaptic for package management. It is easier than other front ends to apt. In Synaptic find the packages named libstdc++6-?.?-dev with various numbers for the ?.?. You should have one of those installed. If you do already, I've mis diagnosed the problem. If you don't, you need to install one. I'm not certain which one. Assuming you already have libstdc++6 installed, look at the version number of that and select the matching version of libstdc++6-?.?-dev

For example, on my Mepis system I have version 4.3.2-1.1 of libstdc++6 installed and the same version of libstdc++6-4.3-dev installed. There is libstdc++6-4.1-dev and libstdc++6-4.2-dev available, not installed. But of course neither of them has a version number starting with 4.3

Quote:
Originally Posted by Nylex View Post
You need to use "iostream" rather than "iostream.h". I believe this is part of the newer C++ standard (ANSI C++?), but I'm not sure. Some distros provide the older headers (i.e. the ones with the ".h") for backwards compatibility, but it seems that Ubuntu isn't one of them.
I think all of that is incorrect.

iostream.h is part of the package libstdc++-dev in Ubuntu. I doubt any distribution would split up the std C++ headers the way you describe. I think the backwards compatibility ones are always included.

Since the OP can't find iostream.h, I assume he is missing all those headers (current ones as well as backwards compatibility ones).

I think there is some higher level package group that would include libstdc++-dev as well as other packages the OP will need. But I don't know how to look up which groups include a package. Installing the required packages one at a time as you hit error messages will get the job done. Installing the group would be easier if you knew its name.

Quote:
Also, you'll need a "using namespace std;" under your include directives and before main(), i.e.

Code:
#include <iostream>
using namespace std;

int main()
{
  // Code here
}
This is because cout is declared in the "std" namespace, so you need to specify that you want to use that namespace.
That is another difference between iostream.h and iostream. If you use iostream.h, you don't need the using namespace std line.

I'm not particularly recommending using iostream.h instead of iostream. I just don't think that is the OP's current problem.

Last edited by johnsfine; 11-26-2009 at 07:21 AM.
 
1 members found this post helpful.
Old 11-27-2009, 01:42 AM   #5
srinathduraisamy
LQ Newbie
 
Registered: Nov 2009
Posts: 9

Rep: Reputation: 4
To make it simple

install the following for your kubuntu

apt-get install build-essential

and as mentioned use <iostream> instead of <iostream.h> and using namespace std, otherwise it will give you a warning, but it will compile.
 
Old 11-28-2009, 11:49 AM   #6
shubham chakraborty
LQ Newbie
 
Registered: Nov 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by Nylex View Post
You need to use "iostream" rather than "iostream.h". I believe this is part of the newer C++ standard (ANSI C++?), but I'm not sure. Some distros provide the older headers (i.e. the ones with the ".h") for backwards compatibility, but it seems that Ubuntu isn't one of them. Also, you'll need a "using namespace std;" under your include directives and before main(), i.e.

Code:
#include <iostream>
using namespace std;

int main()
{
  // Code here
}
This is because cout is declared in the "std" namespace, so you need to specify that you want to use that namespace.

Ya u r absolutely correct..... that is where the problemwas.... i am habitual of using the older version of Turbo C++ IDE in windows where i use the <iostream.h> and i never used the
using namespace std stuff..... so i was getting the errors.....
thanks a lot.... now the programs are back to the running mode....
 
Old 11-28-2009, 12:15 PM   #7
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,223

Rep: Reputation: 167Reputation: 167
Quote:
Originally Posted by shubham chakraborty View Post
Ya u r absolutely correct..... that is where the problemwas.... i am habitual of using the older version of Turbo C++ IDE in windows where i use the <iostream.h> and i never used the
using namespace std stuff..... so i was getting the errors.....
thanks a lot.... now the programs are back to the running mode....
You could always do this:
Code:
#ifdef __TURBOC__
#include <iostream.h>
#define EXIT_SUCCESS 0
#else
#include <iostream> //Who ever thought of dropping the file extension :|
using namespace std;
#endif
int main(){
cout<<"Hello world!" <<endl;
return EXIT_SUCCESS;
}
Portability is good!

Last edited by smeezekitty; 11-28-2009 at 12:18 PM.
 
1 members found this post helpful.
Old 11-30-2009, 03:48 AM   #8
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by smeezekitty View Post
You could always do this:
Code:
#ifdef __TURBOC__
#include <iostream.h>
#define EXIT_SUCCESS 0
#else
#include <iostream> //Who ever thought of dropping the file extension :|
using namespace std;
#endif
int main(){
cout<<"Hello world!" <<endl;
return EXIT_SUCCESS;
}
Portability is good!
Yeah, but any compiler should accept <iostream> and "using namespace std;", so you might as well just use that and not confuse yourself when you look back on your code.
 
1 members found this post helpful.
Old 11-30-2009, 10:45 AM   #9
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,223

Rep: Reputation: 167Reputation: 167
Quote:
Originally Posted by JohnGraham View Post
Yeah, but any compiler should accept <iostream> and "using namespace std;", so you might as well just use that and not confuse yourself when you look back on your code.
even turbo C++?
Code:
 Compiling HELLOCPP.CPP:
•Error HELLOCPP.CPP 1: Unable to open include file 'IOSTREAM'
 Error HELLOCPP.CPP 2: Declaration syntax error
 Error HELLOCPP.CPP 4: Undefined symbol 'cout'
 Error HELLOCPP.CPP 4: Undefined symbol 'endl'
 
Old 12-01-2009, 03:52 AM   #10
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by smeezekitty View Post
even turbo C++?
Code:
 Compiling HELLOCPP.CPP:
•Error HELLOCPP.CPP 1: Unable to open include file 'IOSTREAM'
 Error HELLOCPP.CPP 2: Declaration syntax error
 Error HELLOCPP.CPP 4: Undefined symbol 'cout'
 Error HELLOCPP.CPP 4: Undefined symbol 'endl'
Well... obviously not, at least not with the version you're using

I stand corrected, then. The headers without the .h extension are indeed the "newer" headers, not to be confused with the old ones. There may be something to be said about a compiler that doesn't support them, but if you want to use that compiler, that's your choice.
 
  


Reply

Tags
cc++, compiler, error, program


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems Compiling/Running a Program on Ubuntu 8.10 Akinjide Linux - Software 1 09-24-2009 02:22 PM
Running program from terminal with & does nothing, program stops DittoAlex Linux - General 1 10-05-2007 01:35 AM
Running a program in Ubuntu LeythDC5 Ubuntu 19 07-12-2006 01:35 PM
Running a program in Ubuntu LeythDC5 Linux - Software 2 07-12-2006 09:44 AM


All times are GMT -5. The time now is 02:16 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration