[LC++]How to link

Carlo Wood carlo at alinoe.com
Tue Aug 28 23:46:04 UTC 2001


On Tue, Aug 28, 2001 at 03:12:45PM +0200, Laszlo Boszormenyi wrote:
> Hi!
> 
> I have these files, but I can not link them together. What I do:
> g++ -c My.cpp
> g++ -c caller.cpp
> g++ My.o caller.o -o caller
> 
> I get:
> caller.o: In function `main':
> caller.o(.text+0xe): undefined reference to `My::My(void)'
> caller.o(.text+0x6f): undefined reference to `My::~My(void)'
> caller.o(.text+0x9a): undefined reference to `My::~My(void)'
> collect2: ld returned 1 exit status
> 
> My g++ version is 2.95.4. Thanks in advance, Laszlo

I doubt that, it doesn't exist :/

> #include <iostream>
> 
> #include "My.h"
> 
> int main(void) {
> 	My my;
> 	cout << "Starts..." << endl;

You should use 'std::cout' and 'std::endl' here.

> 	cout << "...ends" << endl;
> 	return 0;
> }

> #include <iostream>
> 
> class My {
> public:
> 	My(void) {
> 		cout << "Constructed" << endl;
> 	}
> 	~My(void) {

Here is the real error, destructors need to be declared as ~My(), no "void".

> 		cout << "Destructed" << endl;
> 	}
> };

> #ifndef __MY_H
> #define __MY_H

Don't start your macros with '_', that is reserved for standard header files.

> 
> class My {

Again?  You already defined it above?!

> public:
> 	My(void);
> 	~My(void);

Idem, see above.

> };
> 
> #endif

-- 
Carlo Wood <carlo at alinoe.com>



More information about the tuxCPProgramming mailing list