what's wrong with this helloworld program?

B

ben

When I try to gcc the following Helloworld program:

#include <iostream>

int main()
{
using namespace std;
cout << "Hello, World!!\n";
return 0;
}

I got errors as follows:

C:\Documents and Settings\benben>gcc ben.cpp
C:\DOCUME~1\benben\LOCALS~1\Temp/ccojbaaa.o(.text+0x33):ben.cpp: undefined
refer
ence to `std::cout'
C:\DOCUME~1\benben\LOCALS~1\Temp/ccojbaaa.o(.text+0x38):ben.cpp: undefined
refer
ence to `std::basic_ostream<char, std::char_traits<char> >& std::eek:perator<<
<std
::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&,
char c
onst*)'
C:\DOCUME~1\benben\LOCALS~1\Temp/ccojbaaa.o(.text+0x64):ben.cpp: undefined
refer
ence to `std::ios_base::Init::Init()'
C:\DOCUME~1\benben\LOCALS~1\Temp/ccojbaaa.o(.text+0x83):ben.cpp: undefined
refer
ence to `std::ios_base::Init::~Init()'

What has happend? Could anyone help me?
 
A

Alvin

ben said:
When I try to gcc the following Helloworld program:

#include <iostream>

int main()
{
using namespace std;
cout << "Hello, World!!\n";
return 0;
}

I got errors as follows:
snip

What has happend? Could anyone help me?

You declare "using namespace std;" inside of main()...it should be outside.
 
R

Rolf Magnus

ben said:
When I try to gcc the following Helloworld program:

#include <iostream>

int main()
{
using namespace std;
cout << "Hello, World!!\n";
return 0;
}

I got errors as follows:

C:\Documents and Settings\benben>gcc ben.cpp
C:\DOCUME~1\benben\LOCALS~1\Temp/ccojbaaa.o(.text+0x33):ben.cpp: undefined
refer
ence to `std::cout'
C:\DOCUME~1\benben\LOCALS~1\Temp/ccojbaaa.o(.text+0x38):ben.cpp: undefined
refer
ence to `std::basic_ostream<char, std::char_traits<char> >&
std::eek:perator<< <std
::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&,
char c
onst*)'
C:\DOCUME~1\benben\LOCALS~1\Temp/ccojbaaa.o(.text+0x64):ben.cpp: undefined
refer
ence to `std::ios_base::Init::Init()'
C:\DOCUME~1\benben\LOCALS~1\Temp/ccojbaaa.o(.text+0x83):ben.cpp: undefined
refer
ence to `std::ios_base::Init::~Init()'

What has happend?

You used the compiler the wrong way.
Could anyone help me?

Try with g++ instead of gcc.
 
K

Kai-Uwe Bux

ben said:
When I try to gcc the following Helloworld program:

#include <iostream>

int main()
{
using namespace std;
cout << "Hello, World!!\n";
return 0;
}

Nothing is wrong with the program.
I got errors as follows:

C:\Documents and Settings\benben>gcc ben.cpp
C:\DOCUME~1\benben\LOCALS~1\Temp/ccojbaaa.o(.text+0x33):ben.cpp: undefined
refer
ence to `std::cout'
C:\DOCUME~1\benben\LOCALS~1\Temp/ccojbaaa.o(.text+0x38):ben.cpp: undefined
refer
ence to `std::basic_ostream<char, std::char_traits<char> >&
std::eek:perator<< <std
::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&,
char c
onst*)'
C:\DOCUME~1\benben\LOCALS~1\Temp/ccojbaaa.o(.text+0x64):ben.cpp: undefined
refer
ence to `std::ios_base::Init::Init()'
C:\DOCUME~1\benben\LOCALS~1\Temp/ccojbaaa.o(.text+0x83):ben.cpp: undefined
refer
ence to `std::ios_base::Init::~Init()'

What has happend? Could anyone help me?

Try compiling with g++ instead of gcc.


Best

Kai-Uwe Bux
 
B

ben

Thanks a lot!

ben

Rolf Magnus said:
Basically, yes. For the compiler, it doesn't matter much. g++ will assume
your code is C++, while gcc tries to detect it from the file name. However,
for linking, the difference is more important. For C++, the linker needs
some additional options (e.g. to link in the C++ standard library), and g++
adds those options, gcc doesn't.
Further questions about g++ should be asked in gnu.g++.help
 
B

ben

I think the using namespace statement is meant to be inside a function, for
localization's sake.
 
R

Rolf Magnus

ben said:
That works! So is g++ specified for C++ programs and gcc for C programs?

Basically, yes. For the compiler, it doesn't matter much. g++ will assume
your code is C++, while gcc tries to detect it from the file name. However,
for linking, the difference is more important. For C++, the linker needs
some additional options (e.g. to link in the C++ standard library), and g++
adds those options, gcc doesn't.
Further questions about g++ should be asked in gnu.g++.help
 
R

Ron Natalie

ben said:
That works! So is g++ specified for C++ programs and gcc for C programs?

In general, yes. The programs "g++" and "gcc" both invoke the same
compiler back ends, and either will compile .cpp or .c to .o as
as appropriate (some others as well). The difference is that g++
will seed the linker with the names of the C++ libraries, etc.. where
gcc assumes you're doing only C things.
 
R

Richard Herring

ben said:
When I try to gcc the following Helloworld program:

#include <iostream>

int main()
{
using namespace std;
cout << "Hello, World!!\n";
return 0;
}

I got errors as follows:

C:\Documents and Settings\benben>gcc ben.cpp
C:\DOCUME~1\benben\LOCALS~1\Temp/ccojbaaa.o(.text+0x33):ben.cpp: undefined
refer
ence to `std::cout'
C:\DOCUME~1\benben\LOCALS~1\Temp/ccojbaaa.o(.text+0x38):ben.cpp: undefined
refer
ence to `std::basic_ostream<char, std::char_traits<char> >& std::eek:perator<<
<std
::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&,
char c
onst*)'
C:\DOCUME~1\benben\LOCALS~1\Temp/ccojbaaa.o(.text+0x64):ben.cpp: undefined
refer
ence to `std::ios_base::Init::Init()'
C:\DOCUME~1\benben\LOCALS~1\Temp/ccojbaaa.o(.text+0x83):ben.cpp: undefined
refer
ence to `std::ios_base::Init::~Init()'

What has happend?

Those are linker errors. You haven't included the correct libraries,
probably because you started the compiler in "C" mode instead of "C++"
mode.
Could anyone help me?

Try typing g++ instead of gcc ;-)
 
R

Rolf Magnus

Ron said:
In general, yes. The programs "g++" and "gcc" both invoke the same
compiler back ends, and either will compile .cpp or .c to .o as
as appropriate (some others as well).

Wrong (if by "appropriate" you mean that files with names ending in .c
should be compiled as C code). Consider this:

$ cat test.c
#include <stdlib.h>

int main()
{
int* x = malloc(sizeof(int));
free(x);
return 0;
}
$ gcc test.c
$ g++ test.c
test.c: In function `int main()':
test.c:5: error: invalid conversion from `void*' to `int*'
 
G

Gianni Mariani

B

ben

In The C++ Programming Language (Special Edition) by Bjarne Stroustrup,
page46, is Bjarne's Hello, world! version. He didn't include <ostream>
either.

ben

Gianni Mariani said:
As everyone else has pointed out - use g++ - however there might be an
error in your code.

std::basic_ostream (IIRC) is supposed to be defined in ostream, which
you do not include, hence it may not work on all compilers.

see:
http://groups-beta.google.com/group/comp.std.c++/msg/6f6abf4bc98c3d20?output
=gplain
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top