Click to See Complete Forum and Search --> : Linux and C++ I need help compiling
sambrose
01-24-2003, 01:56 AM
This is the first time I've tried compiling a C++ program on my linux machine. I'm trying to run a simple "Hello World" program and get this error:
#g++ hello.cpp -o hello
hello.cpp: In function `int main()':
hello.cpp:4: `cout' undeclared (first use this function)
hello.cpp:4: (Each undeclared identifier is reported only once for each
function it appears in.)
Any ideas? Here is the source code:
#include <iostream>
int main() {
cout << "Hello World";
return 0;
}
anmaxp
01-24-2003, 02:05 AM
try adding these to libraries:
#include <stdio.h>
#include <iostream.h>
and I think you should compile by using the command as follows:
$g++ -o helloworld helloworld.cpp
or whatever your source code file is named
sambrose
01-24-2003, 02:18 AM
try adding these to libraries: #include <stdio.h> #include <iostream.h>
I tried this and got an error about deprecated headers...
Somehow I think I don't have everything setup correctly in my environment. Do I need to add some paths to my profile? Or configure the compiler? Remember, this is the first time I've tried to compile anything.
Thanks.
b00zer
01-24-2003, 02:25 AM
im totally new to c++ myself, in fact i just did the hello world last night. heres my code for it. and it complied and ran just fine...
#include <iostream>
int main()
{
std::cout << "Hello World\n";
return 0;
}
anmaxp
01-24-2003, 02:30 AM
the deprecated headers message is just a warning not an error, if you compile using the -Wno-deprecated flag you wont get the warning:
$g++ -o helloworld helloworld.cpp -Wno-deprecated
I really dont think its something wrong with your paths or compiler
sambrose
01-24-2003, 02:44 AM
im totally new to c++ myself, in fact i just did the hello world last night. heres my code for it. and it complied and ran just fine...
#include <iostream>
int main()
{
std::cout << "Hello World\n";
return 0;
}
This worked! Thanks for your guys help. Why do you have to prefix cout with std::cout? Are there any good sites or books for C++ and linux?
Thanks
jetblackz
01-24-2003, 02:55 AM
std::cout is the correct way.
Using cout only requires you add this after the last #include line.
#include <iostream>
using std::cout;
int main() {
cout << "Hello World";
return 0;
}
Sites? A ton.
www.google.com
Enter "C++ programming"
sambrose
01-24-2003, 03:02 AM
std::cout is the correct way.
Using cout only requires you add this after the last #include line.
using std::cout;
.
.
.
cout << "Blah";
This is interesting. I am not new to programming, just new to programming on linux. I've alway used plain old "cout". I learned something new today. Will std::cout compile correctly on Windows?
Thanks
Obrion
01-24-2003, 03:05 AM
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World \n";
return 0;
}
you can do this too
bigrigdriver
01-24-2003, 04:13 AM
Obrion has covered all the bases; using namespace std; means that, in all instances in cout, cin, cerr, or whatever, is covered. If you leave out that line: using etc, then, every time you use any reference to any content of the std library, you will have to use this kind of code: std::cout, std::cin, etc. By using that one line, he can now code cout, cin, cerr, or whatever is in the std library without the std:: preface. It's very much like #include in C, and very convenient.
bwkaz
01-24-2003, 10:43 AM
Originally posted by Obrion
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World \n";
return 0;
}
you can do this too No, no, no, you can't! :D See my sig.
In all seriousness, though, you can legally do that, but I don't like it. It defeats the entire purpose of having namespaces in the standard headers (that's the difference between iostream.h and iostream -- iostream puts everything into the std namespace, so it doesn't clutter up the default namespace, while iostream.h doesn't. So you can have a global variable named cout now, if you wanted (make it, say, a std::vector), and it wouldn't conflict with iostream's cout unless you did one of those using statements).
That's why they're in different namespaces. You don't want to import the entire std namespace because that's pointless. If you do, there'd be no reason to have a std namespace.
jetblackz
01-24-2003, 01:14 PM
Buddy, my school doesn't allow Linux in class. So that says something abut my codes, doesn't it?
Borland C++ 5.5 compiler for Win32 is free. Take a look.
BTW, I check my codes on Linux and Windows.
EscapeCharacter
01-25-2003, 08:37 AM
Originally posted by jetblackz
Buddy, my school doesn't allow Linux in class.
how horrible you should goto another school.
JKlebs9225
01-25-2003, 05:00 PM
I have seen many of these questions about std, namespace and such, so here is the answer that I got form Jesse Liberty's "Teach yourself C++ in 21 days" The old ANSI libraries used the <iostream.h> and you didn't have to declare what you are using, however, the new updated ANSI standard libraries have removed the .h, but you must declare that you are using the standard namespace. You can do this by declaring each item you will be using, such as
using std::cout;
using std::endl;
at the beginning, or you can declare that you will be using the entire library, by stating
using namespace std;
The last option is to tell the compiler what you want to use each time before you use it, such as
std::cout << "Hello world!\n";
std::endl;
Dun'kalis
01-25-2003, 11:04 PM
I believe that code should compile on any compiler. MinGW and Cygwin both compile it.
I *sometimes* use namespace std; when hacking something together, but I always will change it to the better using std::whatever;