In function `main':: undefined reference to ...

Graf László graf.laszlo@axis.hu
Tue Jan 18 10:06:00 GMT 2005


Another question.
I did inherit a class Page from class Base:

------------ page.h -------------------
	#include "base.h"
	#ifndef __PAGE
		#define __PAGE
	
		class Page : public Base{
			private:
				char* _str;
	
			public:
				Page();
				Page(Page &);
				~Page();
	
				char* toString();
				char* toHtml();
		};
	#endif

------------ page.h -------------------
	#include "page.h"
	using namespace std;

	Page::Page(){
	}

	Page::Page(Page &s){
	}

	Page::~Page(){
	}

	char* Page::toString()
	{
		char* rv = "ez a Page toString()";
		return rv;
	}

	char* Page::toHtml()
	{
		char* rv = "ez a Page toHtml()";
		return rv;
	}

and I modified the Makefile:

------------- Makefile ------------------
compile :
	g++ -c base.cpp -o base.o
	g++ -c page.cpp base.o -o page.o      <-------- this line is new
	g++ axf.cpp base.o page.o -o axf.o    <-------- added the page.o

clear :
	clear
	
clean :
	rm *.o

run :
	./axf.o

The result is:
	rm *.o
	g++ -c base.cpp -o base.o
	g++ -c page.cpp base.o -o page.o
	g++: cannot specify -o with -c or -S and multiple compilations
	make: *** [compile] Error 1


Why?


Sriharsha wrote:
> OOps,
> 
> add this to your base.c
> 
> Base::Base(){     }  // This defines your default constructor.
> 
> 
> 
> Graf LĂĄszlĂł wrote:
> 
>> I did all what you said Sriharsha.
>> Whit file names changed, Makefile modified
>> I get the same message:
>>
>> 1. step:   rm *.o
>>
>> 2. step:   g++ -c base.cpp -o base.o
>>
>> 3. step:   g++ axf.cpp base.o -o axf.o
>>
>>            /tmp/ccGshWGj.o(.text+0x4a): In function `main':
>>            : undefined reference to `Base::Base[in-charge]()'
>>
>>            collect2: ld returned 1 exit status
>>
>>
>> Sriharsha wrote:
>>
>>> Hi Laszlo,
>>>      firstly, I suggest that you change the extension of your code 
>>> files from .c to .cpp.
>>>
>>> then, in your make file: Change:
>>> g++ axf.c -o axf.o
>>> to
>>> g++ axf.cpp base.o -o axf.o
>>>
>>> Harsha.
>>>
>>> Graf LĂĄszlĂł wrote:
>>>
>>>> Hi Gurus,
>>>>
>>>> I am Laszlo from Hungary.
>>>> Please, take a look at these files:
>>>>
>>>> --- The main file 
>>>> ---------------------------------------------------------------------
>>>>     #include "stdio.h"
>>>>     #include "stdlib.h"
>>>>     #include "string.h"
>>>>     #include "base.h"
>>>>         int main(int argc, char* argv[])
>>>>     {
>>>>         system("clear");
>>>>         Base* b = new Base();
>>>>         printf("\n---------- %s ---------------\n", b->toString());
>>>>         delete b;
>>>>         return 0;
>>>>     }//main
>>>>
>>>> --- The base.h file 
>>>> ---------------------------------------------------------------------
>>>>     #ifndef __BASE
>>>>         #define __BASE
>>>>         class Base{
>>>>             private:
>>>>                 char* _str;
>>>>
>>>>             public:
>>>>                 Base();
>>>>                 Base(Base &);
>>>>                 ~Base();
>>>>
>>>>                 char* toString();
>>>>                 char* toHtml();
>>>>         };
>>>>     #endif
>>>>
>>>> --- The base.c file 
>>>> ---------------------------------------------------------------------
>>>>     #include "base.h"
>>>>     using namespace std;
>>>>
>>>>     Base::Base(Base &s){
>>>>     }
>>>>
>>>>     Base::~Base(){
>>>>     }
>>>>
>>>>     char* Base::toString()
>>>>     {
>>>>         char* rv = "this is the toString()";
>>>>         return rv;
>>>>     }
>>>>
>>>>     char* Base::toHtml()
>>>>     {
>>>>         char* rv = "this is the toHtml()";
>>>>         return rv;
>>>>     }
>>>>
>>>> --- The Makefile file 
>>>> ---------------------------------------------------------------------
>>>>     compile :
>>>>         g++ -c base.c -o base.o
>>>>         g++ axf.c -o axf.o
>>>>         clear :
>>>>         clear
>>>>            clean :
>>>>         rm *.o
>>>>         run :
>>>>         ./axf.o
>>>>
>>>> -------------------------------------------------------------------------------------------- 
>>>>
>>>> After the command
>>>>        make clear clean compile run
>>>>
>>>> I got these error messages:
>>>>
>>>>     rm *.o
>>>>     g++ -c base.c -o base.o
>>>>     g++ axf.c -o axf.o
>>>>
>>>>     /tmp/cc2K7HjB.o(.text+0x4a): In function `main':
>>>>     : undefined reference to `Base::Base[in-charge]()'
>>>>
>>>>     /tmp/cc2K7HjB.o(.text+0x88): In function `main':
>>>>     : undefined reference to `Base::toString()'
>>>>         /tmp/cc2K7HjB.o(.text+0xaf): In function `main':
>>>>     : undefined reference to `Base::~Base [in-charge]()'
>>>>     collect2: ld returned 1 exit status
>>>>
>>>>     make: *** [compile] Error 1
>>>>
>>>> Could somebody tell me what is wrong there?
>>>> Thank you.
>>>
>>>
>>>
>>>
>>>
>>
> 

-- 
Graf LĂĄszlĂł - bitfaragĂł
http://grafl.port5.com/



More information about the Gcc-help mailing list