469,924 Members | 1,492 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,924 developers. It's quick & easy.

trouble printing 'Hello World'

It's been a few years since I have programmed in C++. I'm starting
to develop some programs in C++ and I can't get "Hello, World" working
yet. It looks to me like 'cout' in the std library is not being found, even
though I have it installed on the system.

I'm using gcc version 3.4.6 and I have libstdc++5 and libstdc++6 installed.

my program looks like this:
// Builder.cc
#include "Builder.h"

Builder::Builder() {
// cout << "Builder constructor\n" ;
}

// Builder.h
#ifndef BUILDER_H
#define BUILDER_H
#endif

class Builder {
public:
Builder();

};

// test.cc
#include <iostream>

using namespace std;

#include "Builder.h"

main() {
Builder b = Builder();

cout << "Hello\n";
}
# makefile

Builder.o: Builder.cc
gcc -c Builder.cc

test.o: test.cc
gcc -c test.cc

test: test.o Builder.o
gcc -o test Builder.o test.o

clean:
rm -rf *.o test

When I compile and link, I get the following linking error message:

gcc -c test.cc
gcc -c Builder.cc
gcc -o test Builder.o test.o
test.o: In function `std::__verify_grouping(char const*, unsigned int,
std::basic_string<char, std::char_traits<char>, std::allocator<char
const&)':test.cc:(.text+0xd): undefined reference to
`std::basic_string<char, std::char_traits<char>, std::allocator<char>
>::size() const'
:test.cc:(.text+0x64): undefined reference to `std::basic_string<char,
std::char_traits<char>, std::allocator<char::operator[](unsigned int)
const'
:test.cc:(.text+0xa4): undefined reference to `std::basic_string<char,
std::char_traits<char>, std::allocator<char::operator[](unsigned int)
const'
:test.cc:(.text+0xd3): undefined reference to `std::basic_string<char,
std::char_traits<char>, std::allocator<char::operator[](unsigned int)
const'
test.o: In function `main':test.cc:(.text+0x136): undefined reference to
`std::cout'
:test.cc:(.text+0x13b): undefined reference to `std::basic_ostream<char,
std::char_traits<char& std::operator<< <std::char_traits<char>
>(std::basic_ostream<char, std::char_traits<char&, char const*)'
test.o: In function `__static_initialization_and_destruction_0(int,
int)':test.cc:(.text+0x163): undefined reference to
`std::ios_base::Init::Init()'
test.o: In function `__tcf_0':test.cc:(.text+0x194): undefined reference
to `std::ios_base::Init::~Init()'
test.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
make: *** [test] Error 1
Does anyone have any suggestions on how to fix my problem? Thank
you in advance!!

Jim Anderson
May 6 '07 #1
8 1418
* Jim Anderson:
[Tool usage]
Use the C++ compiler, called g++, instead of the C compiler, called gcc.

More general advice: use the simplest possible programs to test things.

And btw., move the #endif in your header file to the end of that file.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 6 '07 #2
On Sun, 06 May 2007 23:47:35 +0000, Jim Anderson wrote:
It's been a few years since I have programmed in C++. I'm starting to
develop some programs in C++ and I can't get "Hello, World" working yet.
It looks to me like 'cout' in the std library is not being found, even
though I have it installed on the system.

I'm using gcc version 3.4.6 and I have libstdc++5 and libstdc++6
installed.

my program looks like this:
// Builder.cc
#include "Builder.h"

Builder::Builder() {
// cout << "Builder constructor\n" ;
}

// Builder.h
#ifndef BUILDER_H
#define BUILDER_H
#endif
You want that #endif on the last line of the file :)
class Builder {
public:
Builder();

};

// test.cc
#include <iostream>

using namespace std;

#include "Builder.h"

main() {
Use "int main()" the implicit return type stuff is not standard
conforming.
Builder b = Builder();

cout << "Hello\n";
}
# makefile

Builder.o: Builder.cc
gcc -c Builder.cc

test.o: test.cc
gcc -c test.cc

test: test.o Builder.o
gcc -o test Builder.o test.o
Use g++ instead of gcc and it will automatically link against the
required standard C++ libraries which will fix the link errors below.
clean:
rm -rf *.o test

When I compile and link, I get the following linking error message:

gcc -c test.cc
gcc -c Builder.cc
gcc -o test Builder.o test.o
test.o: In function `std::__verify_grouping(char const*, unsigned int,
std::basic_string<char, std::char_traits<char>, std::allocator<char
const&)':test.cc:(.text+0xd): undefined reference to
`std::basic_string<char, std::char_traits<char>, std::allocator<char>
>::size() const'
[more link errors snipped]

--
Markus
May 7 '07 #3
On 7 Maj, 01:47, Jim Anderson <e...@ieee.orgwrote:
It's been a few years since I have programmed in C++. I'm starting
to develop some programs in C++ and I can't get "Hello, World" working
yet. It looks to me like 'cout' in the std library is not being found, even
though I have it installed on the system.

I'm using gcc version 3.4.6 and I have libstdc++5 and libstdc++6 installed.

my program looks like this:

// Builder.cc
In addition to what others have said, you should include iostream
here.
#include "Builder.h"
And either add using namespace std; here, or use std::cout below.
Builder::Builder() {
// cout << "Builder constructor\n" ;

}

// Builder.h
#ifndef BUILDER_H
#define BUILDER_H
#endif

class Builder {
public:
Builder();

};

// test.cc
#include <iostream>

using namespace std;

#include "Builder.h"

main() {
Builder b = Builder();

cout << "Hello\n";

}
--
Erik Wikström

May 7 '07 #4
Erik Wikström wrote:
>// Builder.cc

In addition to what others have said, you should include iostream
here.
Why?
>#include "Builder.h"

And either add using namespace std; here, or use std::cout below.
Why?
May 7 '07 #5

"Juha Nieminen" <no****@thanks.invalidwrote in message
news:46***********************@news.song.fi...
Erik Wikström wrote:
// Builder.cc
In addition to what others have said, you should include iostream
here.

Why?
iostream is a standard header file for Input/Output. This is where
cout comes from, so to use cout you need to include it.
#include "Builder.h"
And either add using namespace std; here, or use std::cout below.

Why?
In c++ standard library is the namespace (scope) std. The book you're
learning from should have information on it. If not I recommend the
free e-text "Thinking in C++", it seems nice for a freebie.
May 7 '07 #6
BWIGLEY wrote:
"Juha Nieminen" <no****@thanks.invalidwrote in message
news:46***********************@news.song.fi...
>Erik Wikström wrote:
>>>// Builder.cc
In addition to what others have said, you should include iostream
here.
Why?

iostream is a standard header file for Input/Output. This is where
cout comes from, so to use cout you need to include it.
I didn't see Builder.cc using cout, so why is the include necessary?
May 7 '07 #7
"Juha Nieminen" <no****@thanks.invalidwrote in message
news:46***********************@news.song.fi...
BWIGLEY wrote:
"Juha Nieminen" <no****@thanks.invalidwrote in message
news:46***********************@news.song.fi...
Erik Wikström wrote:
// Builder.cc
In addition to what others have said, you should include
iostream
>here.
Why?
iostream is a standard header file for Input/Output. This is
where
cout comes from, so to use cout you need to include it.

I didn't see Builder.cc using cout, so why is the include
necessary?

Well that depends if you want to uncomment:
Builder::Builder() {
// cout << "Builder constructor\n" ;
}

Otherwise, no you should be fine.
May 8 '07 #8
On 7 Maj, 23:12, Juha Nieminen <nos...@thanks.invalidwrote:
BWIGLEY wrote:
"Juha Nieminen" <nos...@thanks.invalidwrote in message
news:46***********************@news.song.fi...
Erik Wikström wrote:
// Builder.cc
In addition to what others have said, you should include iostream
here.
Why?
iostream is a standard header file for Input/Output. This is where
cout comes from, so to use cout you need to include it.

I didn't see Builder.cc using cout, so why is the include necessary?
I saw that you had the line '// cout << "Builder constructor\n" ;' in
Builder.cc and I assumed that you had commented it out because it
didn't work, so I told you have to make it work. Of course, if you
don't want to use std::cout in Builder then you don't have to do
anything.

--
Erik Wikström

May 8 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Dave D | last post: by
2 posts views Thread by Twist Around | last post: by
4 posts views Thread by Paul Rubin | last post: by
1 post views Thread by Sujay | last post: by
42 posts views Thread by Prashanth Badabagni | last post: by
7 posts views Thread by slashdotcommacolon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.