ARM Technical Support Knowledge Articles

LINKER ERROR (UNDEFINED REFERENCE WITH C++)

Applies to: GNU C Compiler for ARM

Answer


Information in this article applies to:


SYMPTOM

The GNU linker gives error messages while linking an application which contains class declarations and class instances.

CAUSE

The class declaration specifies a constructor and/or destructor, but the constructor/destructor function is missing.

class clf  {
  public:
   clf();               // Constructor (ctor)
  ~clf();               // Destructor (dtor)
   int   n1, n2, n3;
};

clf clf1;               // class object

int main (void)  {
  return (0);
}

The linker gives the error messages which look like the following ones:

.objlinky.o(.text+0x40): In function `__static_initialization_and_destruction_0':
/cygdrive/c/Keil/ARM/GNU/Examples/Blinky/blinky.cpp(92):
 error: undefined reference to `clf::~clf [in-charge]() 'blinky.o'
(.text+0x44):blinky.cpp:92: undefined reference to `clf::clf[in-charge]()'

RESOLUTION

Add the constructor(s) and/or destructor(s) as shown below:

class clf  {
  public:
   clf();               // Constructor (ctor)
  ~clf();               // Destructor (dtor)
   int   n1, n2, n3;
};

clf::clf ()  {          // define ctor
  n1 = n2 = n3 = 0;
}

clf::~clf()  {          // define dtor
}

clf clf1;               // class object

int main (void)  {
  return (0);
}

Article last edited on: 2006-07-10 18:41:27

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential