Keil Logo Arm Logo

GCC: LINKER ERROR (UNDEFINED REFERENCE WITH C++)


Information in this article applies to:

  • GNU C Compiler for ARM All Versions

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:

.\obj\blinky.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);
}

SEE ALSO

Last Reviewed: Monday, December 10, 2012


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 

Keil logo

Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

ARM’s Privacy Policy has been updated. By continuing to use our site, you consent to ARM’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.