G++ Undefined Reference to Typeinfo

g++ undefined reference to typeinfo

One possible reason is because you are declaring a virtual function without defining it.

When you declare it without defining it in the same compilation unit, you're indicating that it's defined somewhere else - this means the linker phase will try to find it in one of the other compilation units (or libraries).

An example of defining the virtual function is:

virtual void fn() { /* insert code here */ }

In this case, you are attaching a definition to the declaration, which means the linker doesn't need to resolve it later.

The line

virtual void fn();

declares fn() without defining it and will cause the error message you asked about.

It's very similar to the code:

extern int i;
int *pi = &i;

which states that the integer i is declared in another compilation unit which must be resolved at link time (otherwise pi can't be set to it's address).

Undefined Reference to typeinfo for <class>

The class impedimenta—virtual method table and typeinfo—are generated when the first declared virtual method is compiled. Are you defining the virtual ~CDnsMessage(); (i.e. CDnsMessage::~CDnsMessage() {}) and is the file where it is defined included in the link.

Note that out-of-line definitions are not weak, so it must be defined in exactly one source (not header) file.

undefined reference to `typeinfo for class'

You must either provide a definition for virtual functions in your base class or declare them pure:

class Accel {
public:
virtual void initialize(void) = 0; //either pure virtual
virtual void measure(void) = 0;
virtual void calibrate(void) {}; //or implementation
virtual const int getFlightData(byte) {};
};

Error: undefined reference to `typeinfo for veins::BatteryAccess'

Oh, this is a compiler bug (clang).

The clang compiler does NOT mark the typeinfo symbols as exportable on Windows. This usually goes unnoticed unless you are trying to use an abstract class from a base project (veins) in a project that depends on it. Abstract classes are used regularly to define interfaces in Veins, INET and OMNeT++, too.

There is a workaround for this in INET that can be implemented also in your own project (and in veins too), but it is extremely ugly and it requires the re-linking of the library each time so link time increases heavily.

Other more straightforward workarounds:

  • Add -Wl,--export-all-symbols to the linker command line in Veins to force the exporting of ALL symbols. (Veins is small enough not to cause issues with the 65K limit for the number of exported symbols on Windows)
  • Use GCC instead of clang on Windows (you should rebuild omnet++, too)
  • Use Linux/macOS
  • Use static linking

undefined reference to `typeinfo and 'vtable

Ok, so apparently this all seems to be a compiler issue. This entire time I was using gedit as a text editor and g++ as a compiler, but when I switched over to code blocks it was working just fine.

error: undefined reference to typeinfo while implementing interface pattern in C++

you should use pure virtual function in the interface class:

class LoaderInterface
{
public:
virtual bool reloadFile() = 0;
};

Undefined reference to `typeinfo for class' and undefined reference to `vtable for class'

root::setSize isn't declared pure virtual, which means it must be defined. Presumably, it should be as pure as the other functions:

virtual void setSize(int) = 0;
^^^

If you're interested in the gory details of why you get that particular error: this compiler needs to generate the class's virtual/RTTI metadata somewhere and, if the class declares a non-pure, non-inline virtual function, it will generate it in the same translation unit as that function's definition. Since there is no definition, they don't get generated, giving that error.



Related Topics



Leave a reply



Submit