Page 1 of 1

Undefined reference Error C++

Posted: Thu Apr 21, 2022 9:24 am
by Mehmetdev1
Hello, C++ is getting an undefined frequency error. I am attaching the C++ Source Codes and the Screenshot of the Error.

port.cpp soucres code:

Code: Select all

#include "port.h"

Port::~Port()
{
}



Port8Bit::Port8Bit(uint16_t portnumber)
: Port(portnumber)
{
}

Port8Bit::~Port8Bit()
{
}

void Port8Bit::Write(uint8_t data)
{
   __asm__ volatile("outb %0, %1" : : "a" (data), "Nd" (portnumber));
}

uint8_t Port8Bit::Read()
{
     uint8_t result;
    __asm__ volatile("inb %1, %0" : "=a" (result) : "Nd" (portnumber));
    return result;
}




Port8BitSlow::Port8BitSlow(uint16_t portnumber)
: Port8Bit(portnumber)
{
}

Port8BitSlow::~Port8BitSlow()
{
}

void Port8BitSlow::Write(uint8_t data)
{
   __asm__ volatile("outb %0, %1\njmp 1f\n1: jmp 1f\n1:" : : "a" (data), "Nd" (portnumber));
}





Port16Bit::Port16Bit(uint16_t portnumber)
: Port(portnumber)
{
}

Port16Bit::~Port16Bit()
{
}

void Port16Bit::Write(uint16_t data)
{
   __asm__ volatile("outw %0, %1" : : "a" (data), "Nd" (portnumber));
}

uint16_t Port16Bit::Read()
{
      uint16_t result;
    __asm__ volatile("inw %1, %0" : "=a" (result) : "Nd" (portnumber));
    return result;
}



Port32Bit::Port32Bit(uint16_t portnumber)
: Port(portnumber)
{
}

Port32Bit::~Port32Bit()
{
}

void Port32Bit::Write(uint32_t data)
{
   __asm__ volatile("outl %0, %1" : : "a" (data), "Nd" (portnumber));
}

uint32_t Port32Bit::Read()
{
      uint16_t result;
    __asm__ volatile("inl %1, %0" : "=a" (result) : "Nd" (portnumber));
    return result;
}

port.h soucres code:

Code: Select all

#ifndef __PORT_H
#define __PORT_H


#include "types.h"



    class Port
    {
    protected:
        uint16_t portnumber;
        Port(uint16_t portnumber);
	~Port();
    };
    
    
    class Port8Bit : public Port
    {
    public:
        Port8Bit(uint16_t portnumber);
	~Port8Bit();
	virtual void Write(uint8_t data);
	virtual uint8_t Read();
    };
    
    
     class Port8BitSlow : public Port8Bit
    {
    public:
        Port8BitSlow(uint16_t portnumber);
	~Port8BitSlow();
	virtual void Write(uint8_t data);
    };
    
    
    
      class Port16Bit : public Port
    {
    public:
        Port16Bit(uint16_t portnumber);
	~Port16Bit();
	virtual void Write(uint16_t data);
	virtual uint16_t Read();
    };
    
    
      class Port32Bit : public Port
    {
    public:
        Port32Bit(uint16_t portnumber);
	~Port32Bit();
	virtual void Write(uint32_t data);
	virtual uint32_t Read();
    };
    
    
#endif

Github Repos: https://github.com/mehmetprogramci/Kapilar_OS

Re: Undefined reference Error C++

Posted: Thu Apr 21, 2022 12:27 pm
by Ethin
Well, I can't tell you exactly what's causing the error, but your linker is telling you what's missing. So we don't need to tell you that. But your code is confusing and (for me) hard to follow. First, you don't need to redefine portnumber in every descendant class. Since port is the progenitor of all the different port class types, referring to it will automatically refer to the class member variable in the base class. Second, you don't need all those descendant classes. It would simplify your code dramatically if you used a template class and then just instantiated that for each port type (u8, u16, u32), or used function overloads within the port class itself. And you also don't need a destructor -- there's nothing to "destroy" so you can let the compiler handle that for you automatically via the default destructor. Finally, your implementation of port*slow::write is broken: you have two labels, 1, and I'm pretty sure that's not going to assemble because I'm pretty sure you can't define two labels with the same name. But maybe this is a querk of the GNU As assembler that I just don't know about and this is perfectly okay.
Would you mind giving us a GitHub repository link and posting the error instead of a screenshot?
Edit: actually, there aren't any screenshots attached anyway, so nobody is going to know what your linker is complaining about other than you.

Re: Undefined reference Error C++

Posted: Thu Apr 21, 2022 1:51 pm
by iansjack
Numeric labels have a special usage in as. They have local scope and can be redefined.

Re: Undefined reference Error C++

Posted: Thu Apr 21, 2022 2:15 pm
by Ethin
iansjack wrote:Numeric labels have a special usage in as. They have local scope and can be redefined.
Oh okay, I thought that would be an assemble-time error. Though at my first quick glance I thought it would be an infinite loop so I had to re-read it and then I revised that assessment.

Re: Undefined reference Error C++

Posted: Thu Apr 21, 2022 3:00 pm
by iansjack
The “f” or “b” after the number in the jump instruction refer to the first label with that number searching forwards or backwards from the current IP. It can be quite useful, particularly in macros.

Re: Undefined reference Error C++

Posted: Thu Apr 21, 2022 5:55 pm
by davmac314
Ethin wrote:First, you don't need to redefine portnumber in every descendant class.
I'm looking at the code, and it doesn't do that. Only the Port class defines the portnumber field, in the subclasses portnumber only appears as a constructor parameter.

Re: Undefined reference Error C++

Posted: Thu Apr 21, 2022 7:15 pm
by Ethin
davmac314 wrote:
Ethin wrote:First, you don't need to redefine portnumber in every descendant class.
I'm looking at the code, and it doesn't do that. Only the Port class defines the portnumber field, in the subclasses portnumber only appears as a constructor parameter.
Oh I thought I saw that... My bad. And my re-reads still yielded that... Huh.

Re: Undefined reference Error C++

Posted: Thu Apr 21, 2022 11:42 pm
by iansjack
Show us the error message you are getting.

Re: Undefined reference Error C++

Posted: Thu Apr 21, 2022 11:58 pm
by linuxyne
A required constructor for the Port class is missing.

Re: Undefined reference Error C++

Posted: Fri Apr 22, 2022 12:18 am
by nullplan
You are declaring that Port has a constructor. It is implicitly used by all subclasses. Therefore, you must define a constructor for class Port. And if it is empty.

Re: Undefined reference Error C++

Posted: Fri Apr 22, 2022 5:46 am
by Mehmetdev1
Ethin wrote:Well, I can't tell you exactly what's causing the error, but your linker is telling you what's missing. So we don't need to tell you that. But your code is confusing and (for me) hard to follow. First, you don't need to redefine portnumber in every descendant class. Since port is the progenitor of all the different port class types, referring to it will automatically refer to the class member variable in the base class. Second, you don't need all those descendant classes. It would simplify your code dramatically if you used a template class and then just instantiated that for each port type (u8, u16, u32), or used function overloads within the port class itself. And you also don't need a destructor -- there's nothing to "destroy" so you can let the compiler handle that for you automatically via the default destructor. Finally, your implementation of port*slow::write is broken: you have two labels, 1, and I'm pretty sure that's not going to assemble because I'm pretty sure you can't define two labels with the same name. But maybe this is a querk of the GNU As assembler that I just don't know about and this is perfectly okay.
Would you mind giving us a GitHub repository link and posting the error instead of a screenshot?
Edit: actually, there aren't any screenshots attached anyway, so nobody is going to know what your linker is complaining about other than you.
Hello, The codes for the error are in the Port.cpp and port.h files. I've Created the Github Repository And Uploaded the Image Related to the Error What is the Error Exactly?

Github Repos: https://github.com/mehmetprogramci/Kapilar_OS

Re: Undefined reference Error C++

Posted: Fri Apr 22, 2022 8:24 am
by thewrongchristian
Mehmetdev1 wrote:
Ethin wrote:Well, I can't tell you exactly what's causing the error, but your linker is telling you what's missing. So we don't need to tell you that. But your code is confusing and (for me) hard to follow. First, you don't need to redefine portnumber in every descendant class. Since port is the progenitor of all the different port class types, referring to it will automatically refer to the class member variable in the base class. Second, you don't need all those descendant classes. It would simplify your code dramatically if you used a template class and then just instantiated that for each port type (u8, u16, u32), or used function overloads within the port class itself. And you also don't need a destructor -- there's nothing to "destroy" so you can let the compiler handle that for you automatically via the default destructor. Finally, your implementation of port*slow::write is broken: you have two labels, 1, and I'm pretty sure that's not going to assemble because I'm pretty sure you can't define two labels with the same name. But maybe this is a querk of the GNU As assembler that I just don't know about and this is perfectly okay.
Would you mind giving us a GitHub repository link and posting the error instead of a screenshot?
Edit: actually, there aren't any screenshots attached anyway, so nobody is going to know what your linker is complaining about other than you.
Hello, The codes for the error are in the Port.cpp and port.h files. I've Created the Github Repository And Uploaded the Image Related to the Error What is the Error Exactly?

Github Repos: https://github.com/mehmetprogramci/Kapilar_OS
Exactly what your error screen shot says:

Port::Port(unsigned short) is missing. You declare it, use it, but don't define an implementation of it.