LinuxQuestions.org
Visit Jeremy's Blog.
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 07-08-2003, 10:54 AM   #1
kbeaver
Member
 
Registered: Jul 2003
Location: Lakewood, CO
Distribution: SuSE 8.2
Posts: 41

Rep: Reputation: 15
Compiling a simple C++ program


I recently installed RedHat 9.0 (a native Windows user). I am trying to compile and run a simple C++ program. I have one cout statement. The compiler states it cannot find iostream.h. I have found the file on my box, but I cannot seem to tell the compiler where to find it. Please help
 
Old 07-08-2003, 10:58 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
how are you trying to compile it? with gcc or g++? if you're just using gcc, then it may well be trying to compile it as a C program, which would not provide it with access to the iostream headers
 
Old 07-08-2003, 11:00 AM   #3
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
iostream.h is depreciated you should use iostream instead, what command are you using to compile?

acid_kewpie: gcc can find all the headers that g++ can, in fact i think the only difference between the two is that g++ automatically links with libstdc++ and gcc doesnt but that may be wrong.

Last edited by kev82; 07-08-2003 at 11:03 AM.
 
Old 07-08-2003, 11:05 AM   #4
kbeaver
Member
 
Registered: Jul 2003
Location: Lakewood, CO
Distribution: SuSE 8.2
Posts: 41

Original Poster
Rep: Reputation: 15
I have tried both g++ and gcc, I have named it both .c and .cpp, to no avail. Is there some environment variable to tell it where my libraries are?
 
Old 07-08-2003, 11:10 AM   #5
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
on the compiler command line add -I/absolute/path/to/file and see if that makes a difference
 
Old 07-08-2003, 03:28 PM   #6
kbeaver
Member
 
Registered: Jul 2003
Location: Lakewood, CO
Distribution: SuSE 8.2
Posts: 41

Original Poster
Rep: Reputation: 15
Is there a way to let the compiler know that the libraries will always be in a certain area? I have seen environment variables ($LD_LIBRARY_PATH) that could be set to allow for this.
 
Old 07-08-2003, 03:35 PM   #7
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
kbeaver:

The compiler knows to look in standard places, like /usr/include for headers, and /lib, /usr/lib, /usr/local/lib for libraries.. Might posting the code, the command you are using to compile it, and the error message returned?
 
Old 07-08-2003, 03:54 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
A "nice mistake" to make here would
be

#include "iostream.h"

instead of

#include <iostream.h>

:}

The compiler would look in the current
directory and rightfuly say it's not there ...

Cheers,
Tink
 
Old 07-08-2003, 05:54 PM   #9
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
Like kev82 has stated iostream.h is depreciated and you should instead use:

Code:
#include <iostream>
using namespace std;
Also, wether it is "iostream" or <iostream> (or "iostream.h" and <iostream.h>) doesn't matter (or shouldn't matter), the only difference it makes is where the compiler looks first.

If you specify <iostream> then the compiler searches for the library in the system (implementation dependent) dir and skips the current dir. If you specify it "iostream" then the compiler looks in the current program dir first, before looking through the system dir anyway.

Unless, ofcourse, you decided to make your own iostream library...

kbeaver, if you are still having problems, post your code, so we can make sure it's not something syntactical in nature.

Hope this helps

Last edited by coolman0stress; 07-08-2003 at 05:57 PM.
 
Old 07-08-2003, 06:19 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally posted by coolman0stress
Also, wether it is "iostream" or <iostream> (or "iostream.h" and <iostream.h>) doesn't matter (or shouldn't matter), the only difference it makes is where the compiler looks first.
Not according to B. Stroustrup, "The C++ programming
language" (3rd edition) ... he's quit clear on the notation,
and says that < > is for the "standard include directory"
and " " for the "current working directory". It might not
matter for some compilers, but it should :}

Cheers,
Tink
 
Old 07-08-2003, 06:37 PM   #11
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
not according to the current c++ standard

http://www.comnets.rwth-aachen.de/do...ml#cpp.include

part 3
 
Old 07-08-2003, 06:55 PM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally posted by kev82
not according to the current c++ standard

http://www.comnets.rwth-aachen.de/do...ml#cpp.include

part 3
DOH :} ... I should upgrade my bookshelf ;)

Cheers,
Tink
 
Old 07-09-2003, 08:04 PM   #13
kbeaver
Member
 
Registered: Jul 2003
Location: Lakewood, CO
Distribution: SuSE 8.2
Posts: 41

Original Poster
Rep: Reputation: 15
Wow, that was a nice little argument

Here is my code:

#include <iostream>
using namespace std;
int main() {
int temp = 2;
cout << temp << endl;
return 0;
}

Here is the error:
[kbeaver@BeaverBox kbeaver]$ g++ test.cpp -I /usr/include/g++-3/
/tmp/ccMVjyzy.o(.text+0x1b): In function `main':
: undefined reference to `endl(ostream&)'
/tmp/ccMVjyzy.o(.text+0x26): In function `main':
: undefined reference to `cout'
/tmp/ccMVjyzy.o(.text+0x2b): In function `main':
: undefined reference to `ostream:perator<<(int)'
/tmp/ccMVjyzy.o(.text+0x34): In function `main':
: undefined reference to `ostream:perator<<(ostream& (*)(ostream&))'
collect2: ld returned 1 exit status

Any help is appreciated
 
Old 07-09-2003, 08:29 PM   #14
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
That's linker errors ....

It appears that you'll have to explicitly point
it to the right path for the libraries ....

g++ test.cpp -I /usr/include/g++-3/ -L /usr/<whereevertheyliveinRH>

Cheers,
Tink
 
Old 07-09-2003, 08:35 PM   #15
kbeaver
Member
 
Registered: Jul 2003
Location: Lakewood, CO
Distribution: SuSE 8.2
Posts: 41

Original Poster
Rep: Reputation: 15
What does the -L mean? I don't understand what I am suppossed to point the compiler to.
 
  


Reply


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 a simple shell program yekrahs Programming 3 09-26-2005 12:07 PM
Compiling a simple C++ program for Windows Baix Programming 2 03-05-2005 07:17 PM
compiling simple c/c++ program but no good freeindy Programming 3 02-12-2005 11:38 AM
Compiling a simple Mesa/GLUT program Bojdom Linux - Software 2 02-02-2003 04:19 PM
Simple compiling question??? cameronknives Linux - General 5 02-10-2002 10:58 AM

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

All times are GMT -5. The time now is 03:01 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