trouble printing 'Hello World'

J

Jim Anderson

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
>::size() const'
:test.cc:(.text+0x64): undefined reference to `std::basic_string<char,
std::char_traits<char>, std::allocator<char> >::eek:perator[](unsigned int)
const'
:test.cc:(.text+0xa4): undefined reference to `std::basic_string<char,
std::char_traits<char>, std::allocator<char> >::eek:perator[](unsigned int)
const'
:test.cc:(.text+0xd3): undefined reference to `std::basic_string<char,
std::char_traits<char>, std::allocator<char> >::eek:perator[](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 said:
>(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
 
A

Alf P. Steinbach

* 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.
 
M

Markus Schoder

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

[more link errors snipped]
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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.
 
B

BWIGLEY

Juha Nieminen said:

iostream is a standard header file for Input/Output. This is where
cout comes from, so to use cout you need to include it.

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.
 
J

Juha Nieminen

BWIGLEY said:
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?
 
B

BWIGLEY

Juha Nieminen said:
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.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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.
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top