[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Hello World!
From: |
Guy Harrison |
Subject: |
Re: Hello World! |
Date: |
Thu, 13 Jan 2005 22:59:07 GMT |
User-agent: |
KNode/0.8.1 |
Bjørnar Libæk wrote:
> Trying to build the helloworld program (below).
> When building, I get this:
>
> g++ -c -o main.o -MD -I/usr/include -I/usr/include/g++-3 main.cpp
> HelloWorld/main.cpp: In function `int main(int, char**)':
> "main.cpp": HelloWorld/main.cpp error: `cout' undeclared in namespace
> `std' at line 6
> "main.cpp": HelloWorld/main.cpp error: `endl' undeclared in namespace
> `std' at line 6
> "main.cpp": HelloWorld/main.cpp error: `cout' undeclared in namespace
> `std' at line 7
> "main.cpp": HelloWorld/main.cpp error: `cin' undeclared in namespace `std'
> at line 9
> "main.cpp": HelloWorld/main.cpp error: `cout' undeclared in namespace
> `std' at line 11
> "main.cpp": HelloWorld/main.cpp error: `endl' undeclared in namespace
> `std' at line 11
> GNU C++ Compiler exited with error code: 1
> Build cancelled due to errors
>
>
> However, if I remove the "-I/usr/include/g++-3" option, it builds and runs
> ok.
You should remove both the -I/usr/include and the -I/usr/include/g++-3
options. g++ knows where to look for its own headers and in what order to
look at them.
Pass "-v" flag to your compilation line. You'll see in the verbose output
that /usr/include is being ignored. You'll probably also note that your g++
wants to use those 3.4 headers (a reasonable guess on my part).
> The g++-3 directory is present, and readable.
>
> sh#> locate iostream.h
> /usr/include/c++/3.3/backward/iostream.h
> /usr/include/c++/3.4/backward/iostream.h
> /usr/include/g++-3/iostream.h
> sh#>
>
> Why the errors?
By passing -I/usr/include/g++-3 you've forced g++ to look at the wrong
headers which (a) is wrong (b) conflicts with its own.
You only need to use -I (ditto -L) when compiler needs to be told to look
somewhere it doesn't normally look.
Code below is fine of course.
> [main.c]
> #include <iostream>
>
> int main(int /*argc*/, char* /*argv*/[])
> {
> std::cout << "Hello world!" << std::endl;
> std::cout << "Please enter a character and press return:";
>
> std::cin.get();
>
> std::cout << "Goodbye world!" << std::endl;
> return 0;
> }
> [/main.c]
- Hello World!, Bjørnar Libæk, 2005/01/13
- Re: Hello World!,
Guy Harrison <=