OSDev.org

The Place to Start for Operating System Developers
It is currently Mon Aug 03, 2020 10:27 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Undefined reference to '_ZTVN3hal7ConsoleE' [SOLVED]
PostPosted: Sun Jun 14, 2020 4:04 am 
Offline
Member
Member
User avatar

Joined: Wed Nov 07, 2012 2:40 am
Posts: 26
Location: Petaling Jaya, Malaysia
I can't get rid of 'undefined reference to '_ZTVN3hal7ConsoleE' which point to constructor of Console class.
I have a static linked library, in it, I've define an pure virtual class name ITty and place in a file name hal.hpp, Console class inherits ITty and have all the virtual function implemented. Everything is fine until I decide to add a global variable 'Console theConsole'. No matter where I place this global variable within the static library's scope, I'll get the 'undefined reference...' error message.

hal.hpp
Code:
namespace hal {
class ITty {
public:
   // Attributes
   virtual CONSOLE_COLOR GetForeColor()               = 0;
   virtual void        SetForeColor(CONSOLE_COLOR fore) = 0;
   virtual CONSOLE_COLOR GetBackColor()               = 0;
   virtual void        SetBackColor(CONSOLE_COLOR back) = 0;

public:
   // Operations
   virtual DWORD Print(const char *, ...) = 0;
   virtual void  Clear()               = 0;
   virtual void  MoveTo(const PCOORD)      = 0;
   virtual INT     PutChar(INT)            = 0;
};
}


Console.hpp
Code:

namespace hal {
class Console : public ITty {
   COORD        m_pos;
   CONSOLE_COLOR m_back;
   CONSOLE_COLOR m_fore;

public:      //   Constructor
   Console(CONSOLE_COLOR, CONSOLE_COLOR);
   ~Console();

public:
   // Attributes
   CONSOLE_COLOR GetForeColor();
   void        SetForeColor(CONSOLE_COLOR fore);
   CONSOLE_COLOR GetBackColor();
   void        SetBackColor(CONSOLE_COLOR back);

public:
   // Operations
   DWORD Print(const char *, ...);
   void  Clear();
   void  MoveTo(const PCOORD);
   INT     PutChar(INT);

private:
   // Operations
   int PrintBuffer(char *__restrict, const char *__restrict, va_list);
   int PrintNumber(char *, int, format_specifier_t *);
   int PrintString(char *, const char *, format_specifier_t *);
};
}


Console.cpp
Code:
Definition of Console class define here.


hal.cpp
Code:

namespace hal {

Console theConsole(CONSOLE_COLOR::WHITE, CONSOLE_COLOR::BLUE);

//    The rest are all hal code.
...
}


build output
Code:
/home/tongko/opt/cross/x86_64-elf/lib/gcc/x86_64-elf/10.1.0/../../../../x86_64-elf/bin/ld: /home/tongko/projects/qios/obj/libhal.a(Console.o): in function `_ZN3hal7ConsoleC2ENS_13CONSOLE_COLORES1_':
/home/tongko/projects/qios/hal/Console.cpp:340: undefined reference to `_ZTVN3hal7ConsoleE'
/home/tongko/opt/cross/x86_64-elf/lib/gcc/x86_64-elf/10.1.0/../../../../x86_64-elf/bin/ld: /home/tongko/projects/qios/obj/libhal.a(Console.o): in function `_ZN3hal7ConsoleD2Ev':
/home/tongko/projects/qios/hal/Console.cpp:342: undefined reference to `_ZTVN3hal7ConsoleE'
collect2: error: ld returned 1 exit status


line 340 and 342 in Console.cpp are definition of constructor and destructor:
Code:
Console::Console(CONSOLE_COLOR fore, CONSOLE_COLOR back)
   : m_fore(fore)
   , m_back(back) {}

Console::~Console() {
   // Nothing to delete.
}


Before I add a destructor, the error message is pointing to first line of the class declaration. After adding destructor, I've got this. If I remove the global variable, then the build succeeded.

I've run out of idea what to look at, as this is just a very simple class and global variable.


Last edited by tongko on Sun Jun 14, 2020 9:23 pm, edited 3 times in total.

Top
 Profile  
 
 Post subject: Re: Undefined reference to '_ZTVN3hal7ConsoleE'
PostPosted: Sun Jun 14, 2020 5:25 am 
Offline
Member
Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 739
The issue is that the first virtual function is defined nowhere.

Compilers emit the vtable while compiling the first virtual function of a class (to avoid multiple definitions / messing around with COMMON sections).

_________________
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].


Top
 Profile  
 
 Post subject: Re: Undefined reference to '_ZTVN3hal7ConsoleE'
PostPosted: Sun Jun 14, 2020 6:54 am 
Offline
Member
Member
User avatar

Joined: Wed Nov 07, 2012 2:40 am
Posts: 26
Location: Petaling Jaya, Malaysia
I didn't know that one need to define the pure virtual function, nevertheless, I've added a file ITty.cpp:

Code:
#include <hal.hpp>
#include <types.hpp>

namespace hal {

CONSOLE_COLOR ITty::GetForeColor() {}

void ITty::SetForeColor(CONSOLE_COLOR fore) {}

CONSOLE_COLOR ITty::GetBackColor() {}

void ITty::SetBackColor(CONSOLE_COLOR back) {}

DWORD ITty::Print(const char *fmt, ...) {}

void ITty::Clear() {}

void ITty::MoveTo(const PCOORD pCoord) {}

INT ITty::PutChar(INT value) {}

}    // namespace hal


but it make no difference...


Top
 Profile  
 
 Post subject: Re: Undefined reference to '_ZTVN3hal7ConsoleE'
PostPosted: Sun Jun 14, 2020 6:59 am 
Offline
Member
Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 739
Not the pure virtual functions but the overriden ones in the Console class (they are only pure in the base class). By the way, it is good practice to add the override keyword here.

_________________
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].


Top
 Profile  
 
 Post subject: Re: Undefined reference to '_ZTVN3hal7ConsoleE'
PostPosted: Sun Jun 14, 2020 7:04 am 
Offline
Member
Member
User avatar

Joined: Wed Nov 07, 2012 2:40 am
Posts: 26
Location: Petaling Jaya, Malaysia
After re-read your post, I think I definitely miss out something about the pure virtual in C++, thus, I check that again and found that if one doesn't provide the destructor body even if it is pure virtual class (interface), then compiler will throw the error I'm hitting now.

Thank you very much for the advise, thought.

P.s. I'll start using the keyword override from now onwards :D


Top
 Profile  
 
 Post subject: Re: Undefined reference to '_ZTVN3hal7ConsoleE'
PostPosted: Sun Jun 14, 2020 9:31 am 
Offline
Member
Member
User avatar

Joined: Wed Nov 07, 2012 2:40 am
Posts: 26
Location: Petaling Jaya, Malaysia
I celebrate too early... :(

After applying changes, now I fall into another 'undefined references to ' error message

Code:
/home/tongko/opt/cross/x86_64-elf/lib/gcc/x86_64-elf/10.1.0/../../../../x86_64-elf/bin/ld: /home/tongko/projects/qios/obj/libhal.a(Console.o): in function `_ZN3hal7ConsoleD0Ev':
/home/tongko/projects/qios/hal/Console.cpp:344: undefined reference to `_ZdlPvm'


In face, I don't have the any symbol named _ZdlPvm or anything close.


Top
 Profile  
 
 Post subject: Re: Undefined reference to '_ZTVN3hal7ConsoleE'
PostPosted: Sun Jun 14, 2020 10:26 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 2156
tongko wrote:
_ZdlPvm

This thread might help.


Top
 Profile  
 
 Post subject: Re: Undefined reference to '_ZTVN3hal7ConsoleE'
PostPosted: Sun Jun 14, 2020 9:22 pm 
Offline
Member
Member
User avatar

Joined: Wed Nov 07, 2012 2:40 am
Posts: 26
Location: Petaling Jaya, Malaysia
Octocontrabass wrote:
tongko wrote:
_ZdlPvm

This thread might help.


Thank you so much! All I need to do is to include my declaration of [new] & [delete] operators, then all is working now.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group