LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 05-22-2009, 07:55 AM   #1
Windows to Linux
Member
 
Registered: Sep 2008
Distribution: Ubuntu LTS
Posts: 57

Rep: Reputation: 16
Having trtouble with C++ hello world, where are headers


Can any one tell me where gcc c++ header files are? Can't compile the Hello World
 
Old 05-22-2009, 07:57 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Usually, somewhere under /usr/include. Can you tell us what the exact error messages are, please?
 
Old 05-22-2009, 08:18 AM   #3
soleilarw
Member
 
Registered: Apr 2009
Posts: 107

Rep: Reputation: 19
You have to make sure that you have the relevant development packages installed. Common libraries will be installed along with gcc, but other pieces like mysql will require you to install mysql-devel package to make the headers available. I'm sure that the compile error feedback gives enough information about the problem in your case.

Linux Archive

Last edited by soleilarw; 06-18-2009 at 04:02 AM.
 
Old 05-22-2009, 11:31 AM   #4
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
This sounds indeed like you're missing the development files. Under Ubuntu it's usually as simple as typing
Code:
sudo apt-get install build-essential
in a terminal, confirming and waiting for the installation to finish.
 
Old 05-23-2009, 10:53 AM   #5
Windows to Linux
Member
 
Registered: Sep 2008
Distribution: Ubuntu LTS
Posts: 57

Original Poster
Rep: Reputation: 16
I found the lib, iostream, and everything seems OK
here is what I'm trying to compile

#include <iostream>
int main()
{
cout << "Hello world!\n";
return 0;
}

and gcc says

HI.cpp: In function 'int main()':
HI.cpp:4: error: 'cout' was not decared in this scope

Last edited by Windows to Linux; 05-26-2009 at 07:49 AM.
 
Old 05-23-2009, 11:05 AM   #6
jrecortel
Member
 
Registered: Sep 2008
Location: Philippines
Distribution: slackware, FreeBSD
Posts: 123

Rep: Reputation: 19
Quote:
Originally Posted by Windows to Linux View Post
I found the lib, iostream, and everything seems OK
here is what I'm trying to compile

#include <iostream>
int main()
{
cout << "Hello world!\n"
return 0;
}

and gcc says

HI.cpp: In function 'int main()':
HI.cpp:4: error: 'cout' was not decared in this scope
try this:
Code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!\n"
return 0;
}
or
Code:
#include <iostream>
int main()
{
std::cout << "Hello world!\n"
return 0;
}

Last edited by jrecortel; 05-23-2009 at 11:09 AM. Reason: alternative
 
Old 05-23-2009, 11:11 AM   #7
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Here's a bit of an explanation: link
 
Old 05-23-2009, 11:25 AM   #8
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
You're also missing a semi-colon on the line containing cout.
 
Old 05-23-2009, 08:04 PM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
If you are still having problems then it could be that you are using gcc rather than g++ to compile your code.

Code:
#include <iostream>
int main()
{
std::cout << "Hello world!\n";
return 0;
}
And then assuming that your code is saved in a file called Hi.cpp try from the prompt
Code:
$ g++ -o Hi Hi.cpp
$ ./Hi
 
Old 05-25-2009, 11:14 AM   #10
Windows to Linux
Member
 
Registered: Sep 2008
Distribution: Ubuntu LTS
Posts: 57

Original Poster
Rep: Reputation: 16
g++ is GCC, which can compile c++ code.
 
Old 05-25-2009, 12:02 PM   #11
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Quote:
Originally Posted by Windows to Linux View Post
and gcc says
Not to speak for graemef, but the thought that you were using the c compiler crossed my mind because of the above statement. gcc is a link to the c compiler and g++ links to the c++ compiler, thus the confusion. But you would have gotten a lot of errors if you had been using gcc. Try it.
 
Old 05-25-2009, 09:41 PM   #12
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
If you compile your program using plain old gcc it will look at the extension of the source code to determine the language and for *.cpp it will (by default) use c++. However it will not link in the C++ library and so you will need to explicitly link the library in. Whereas g++ forces the use of the C++ compiler on the source code and links in the C++ standard library.
 
Old 05-26-2009, 07:46 AM   #13
Windows to Linux
Member
 
Registered: Sep 2008
Distribution: Ubuntu LTS
Posts: 57

Original Poster
Rep: Reputation: 16
Thanks for the help, using g++ worked
 
  


Reply



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
What are headers? okos Linux - Newbie 6 08-10-2008 04:31 PM
2.6.22.6 headers saimike Debian 11 09-11-2007 05:50 PM
headers stabu Linux - Newbie 3 06-16-2005 04:47 PM
world writeable files will not stay world writeable antken Mandriva 1 03-02-2004 05:04 PM
what are headers? mep]-[isto Linux - Newbie 7 12-14-2003 05:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:28 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration