title: Resolving a linker error: undefined reference to static class members
body: <p>My code is Arduinoish.  I turned on verbose compiling so I could verify that all the .o files are indeed getting passed to the linker correctly, and they are (linker command below).  This leads me to believe that it is some sort of syntax error.</p>
  
  <p>Googling for the error "undefined reference to in function" produces a lot of results with answers like "add foo.o to your linker command like so", etc.</p>
  
  <p>I hope the solution is as simple as a missing dot or -> somewhere.</p>
  
  <p>I'm getting this series of errors in one file, from the linker:</p>
  
  <pre><code>SerialServoControl.cpp.o: In function `SerialServoControl::send(int, int)':
  SerialServoControl.cpp:31: undefined reference to `SerialServoControl::_serial'
  SerialServoControl.cpp:31: undefined reference to `SerialServoControl::_serial'
  SerialServoControl.cpp.o: In function `SerialServoControl::init(char, char)':
  SerialServoControl.cpp:9: undefined reference to `SerialServoControl::_tx'
  SerialServoControl.cpp:10: undefined reference to `SerialServoControl::_rx'
  </code></pre>
  
  <p>The .h file:</p>
  
  <pre><code>#ifndef SERIALSERVOCONTROL_H
  #define SERIALSERVOCONTROL_H
  
  #include "NewSoftSerial.h"
  
  class SerialServoControl {
      public:
            //                             rx, tx
            static NewSoftSerial _serial;//(9, 8);
  
            int _servo_id;
            static char _tx;
            static char _rx;
  
            static void init(char tx, char rx);
            static void send(int servo_id, int angle);
            void setup(int servo_id);
            void set(int spot);
  };
  
  #endif
  </code></pre>
  
  <p>and the .cpp file:</p>
  
  <pre><code>#ifndef SERIALSERVOCONTROL_CPP
  #define SERIALSERVOCONTROL_CPP
  
  #include "WProgram.h"
  #include "SerialServoControl.h"
  
  //static
  void SerialServoControl::init(char tx, char rx){
      _tx = tx;
      _rx = rx;
      _serial = NewSoftSerial(rx, tx);
      _serial.begin(9600);
  }
  
  //static
  void SerialServoControl::send(int servo_id, int angle){
      unsigned char buff[6];
  
      int temp     = angle &amp; 0x1f80;
      char pos_hi  = temp &gt;&gt; 7;
      char pos_low = angle &amp; 0x7f;
  
      buff[0] = 0x80;        // start byte
      buff[1] = 0x01;        // device id
      buff[2] = 0x04;        // command number
      buff[3] = servo_id;       // servo number
      buff[4] = pos_hi;      // data1
      buff[5] = pos_low;     // data2
  
      for(int i=0; i&lt;6; i++){
          _serial.print(buff[i], BYTE);
  
      }
  }
  
  
  void SerialServoControl::setup(int servo_id){
      _servo_id = servo_id;
  }
  
  void SerialServoControl::set(int angle){
      SerialServoControl::send(_servo_id, angle);
  }
  
  #endif
  </code></pre>
  
  <p>The linker command (I've removed the explicit temporary directory paths that the IDE generates for clarity and broken it out to multiple lines.  The actual command is explicit as to the location of all these files):</p>
  
  <pre><code>  avr-gcc -Os -Wl,--gc-sections -mmcu=atmega328p \
    -o Wheel_Chair_Joystick_Control.cpp.elf \
    SerialServoControl.cpp.o \
    Wheel_Chair_Joystick_Control.cpp.o \
    WheelChairMotor.cpp.o \
    NewSoftSerial.cpp.o \
    core.a \
    -Lbuild277668752723095706.tmp \
    -lm
  </code></pre>
  
  <p>All of these files (SerialServoControl, Wheel_Chair_Joystick, NewSoftSerial, WheelChairMotor) exist in the Arduino <a href="http://www.arduino.cc/en/Tutorial/Sketch" rel="nofollow">sketch</a> directory.  <code>Core.a</code> is the compiled AVR library.</p>