LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Compiling a simple C++ program (http://www.linuxquestions.org/questions/programming-9/compiling-a-simple-c-program-70975/)

kbeaver 07-08-2003 10:54 AM

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

acid_kewpie 07-08-2003 10:58 AM

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

kev82 07-08-2003 11:00 AM

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.

kbeaver 07-08-2003 11:05 AM

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?

kev82 07-08-2003 11:10 AM

on the compiler command line add -I/absolute/path/to/file and see if that makes a difference

kbeaver 07-08-2003 03:28 PM

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.

TheLinuxDuck 07-08-2003 03:35 PM

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?

Tinkster 07-08-2003 03:54 PM

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

coolman0stress 07-08-2003 05:54 PM

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

Tinkster 07-08-2003 06:19 PM

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

kev82 07-08-2003 06:37 PM

not according to the current c++ standard

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

part 3

Tinkster 07-08-2003 06:55 PM

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

kbeaver 07-09-2003 08:04 PM

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::operator<<(int)'
/tmp/ccMVjyzy.o(.text+0x34): In function `main':
: undefined reference to `ostream::operator<<(ostream& (*)(ostream&))'
collect2: ld returned 1 exit status

Any help is appreciated

Tinkster 07-09-2003 08:29 PM

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

kbeaver 07-09-2003 08:35 PM

What does the -L mean? I don't understand what I am suppossed to point the compiler to.


All times are GMT -5. The time now is 01:54 AM.