Problems with constructors

M

Martin York

I am trying to compile the program from this page:

http://www.gtkmm.org/docs/gtkmm-2.4/docs/tutorial/html/sec-helloworld...

I use this makefile:

all: main

main: helloworld.h helloworld.cc main.cc
g++ main.cc -o main `pkg-config gtkmm-2.4 --cflags --libs`

But when I compile I get:

/tmp/ccPWqvDE.o: In function `main':
main.cc:(.text+0x37): undefined reference to `HelloWorld::HelloWorld()'
main.cc:(.text+0x54): undefined reference to `HelloWorld::~HelloWorld()'
main.cc:(.text+0x67): undefined reference to `HelloWorld::~HelloWorld()'
collect2: ld returned 1 exit status
make: *** [main] Error 1

But I don't understand why since the constructors are specified.

This is not a problem with your code but a problem with what you are
building.
Your code is in two files main.cc and helloworld.cc but you are only
building main.cpp

You should probably drop a note to a group that can help you get your
makefile set up correctly. As a temporary solution change the build
line to:



g++ main.cc helloworld.cc -o main `pkg-config gtkmm-2.4 --
cflags --libs`

Notice: ^^^^^^^^^^^^
 
A

Alf P. Steinbach

* saneman:
I am trying to compile the program from this page:

http://www.gtkmm.org/docs/gtkmm-2.4/docs/tutorial/html/sec-helloworld.html

I use this makefile:

all: main

main: helloworld.h helloworld.cc main.cc
g++ main.cc -o main `pkg-config gtkmm-2.4 --cflags --libs`

But when I compile I get:

/tmp/ccPWqvDE.o: In function `main':
main.cc:(.text+0x37): undefined reference to `HelloWorld::HelloWorld()'
main.cc:(.text+0x54): undefined reference to `HelloWorld::~HelloWorld()'
main.cc:(.text+0x67): undefined reference to `HelloWorld::~HelloWorld()'
collect2: ld returned 1 exit status
make: *** [main] Error 1

But I don't understand why since the constructors are specified.

You have forgotten 'helloworld.cc' in the compilation command.


Cheers, & hth.,

- Alf
 
S

saneman

Alf said:
* saneman:
I am trying to compile the program from this page:

http://www.gtkmm.org/docs/gtkmm-2.4/docs/tutorial/html/sec-helloworld.html


I use this makefile:

all: main

main: helloworld.h helloworld.cc main.cc
g++ main.cc -o main `pkg-config gtkmm-2.4 --cflags --libs`

But when I compile I get:

/tmp/ccPWqvDE.o: In function `main':
main.cc:(.text+0x37): undefined reference to `HelloWorld::HelloWorld()'
main.cc:(.text+0x54): undefined reference to `HelloWorld::~HelloWorld()'
main.cc:(.text+0x67): undefined reference to `HelloWorld::~HelloWorld()'
collect2: ld returned 1 exit status
make: *** [main] Error 1

But I don't understand why since the constructors are specified.

You have forgotten 'helloworld.cc' in the compilation command.


Cheers, & hth.,

- Alf

Ah is there someway to split this up in to targets or is this offtopic?
 
S

saneman

I am trying to compile the program from this page:

http://www.gtkmm.org/docs/gtkmm-2.4/docs/tutorial/html/sec-helloworld.html

I use this makefile:

all: main

main: helloworld.h helloworld.cc main.cc
g++ main.cc -o main `pkg-config gtkmm-2.4 --cflags --libs`

But when I compile I get:

/tmp/ccPWqvDE.o: In function `main':
main.cc:(.text+0x37): undefined reference to `HelloWorld::HelloWorld()'
main.cc:(.text+0x54): undefined reference to `HelloWorld::~HelloWorld()'
main.cc:(.text+0x67): undefined reference to `HelloWorld::~HelloWorld()'
collect2: ld returned 1 exit status
make: *** [main] Error 1

But I don't understand why since the constructors are specified.
 
A

Alf P. Steinbach

* saneman:
Alf said:
* saneman:
I am trying to compile the program from this page:

http://www.gtkmm.org/docs/gtkmm-2.4/docs/tutorial/html/sec-helloworld.html


I use this makefile:

all: main

main: helloworld.h helloworld.cc main.cc
g++ main.cc -o main `pkg-config gtkmm-2.4 --cflags --libs`

But when I compile I get:

/tmp/ccPWqvDE.o: In function `main':
main.cc:(.text+0x37): undefined reference to `HelloWorld::HelloWorld()'
main.cc:(.text+0x54): undefined reference to `HelloWorld::~HelloWorld()'
main.cc:(.text+0x67): undefined reference to `HelloWorld::~HelloWorld()'
collect2: ld returned 1 exit status
make: *** [main] Error 1

But I don't understand why since the constructors are specified.

You have forgotten 'helloworld.cc' in the compilation command.


Cheers, & hth.,

- Alf

Ah is there someway to split this up in to targets or is this offtopic?

Don't know about off-topicality. Some might think it's off-topic. I think that
in order to use C++, you need some basic concepts about tool usage (however,
details about a particular tool-chain are better asked about in other groups).

You could do e.g. (off the cuff)


main: main.o helloworld.o
g++ main.o helloworld.o ungrokkable-pkg-lib-spec

main.o: main.cc helloworld.h
g++ -c -Wall -pedantic -and-so-on-more-options main.cc -o main.o

helloworld.o: helloworld.h helloworld.cc
g++ -c -Wall -pedantic -and-so-on-more-options helloworld.cc -o helloworld.o


but depending on the make you may not need to write all that, or you can define
general rules about how to generate object files from their corresponding source
files. Originally, when make was very C-oriented, it knew how to make most
things. But since it's easier to add complexity than to simplify, over the
years things have become more and more complex.


Cheers, & hth.,

- Alf
 

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,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top