Linker error resolution: undefined reference to static class members

My Arduinoish code. I turned on verbose compilation to check if all .o files were really transferred to the linker, and they are (link below). It makes me think this is some kind of syntax error.

Google for the error "undefined function reference" gives a lot of results with answers like "add foo.o to your linker command like this" etc.

I hope the solution will be as simple as a missing point or → somewhere.

I get this series of errors in a single file from the linker:

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' 

.H file:

 #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 

and .cpp file:

 #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 & 0x1f80; char pos_hi = temp >> 7; char pos_low = angle & 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<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 

Linker command (I removed the explicit temporary directory paths that the IDE generates for clarity and split them into several lines. The actual command is not specified with respect to the location of all these files):

  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 

All these files (SerialServoControl, Wheel_Chair_Joystick, NewSoftSerial, WheelChairMotor) exist in the Arduino sketch directory. Core.a is a compiled AVR library.

+4
source share
4 answers

You must define the statics of your class in the source file somewhere. Putting them in a class simply declares that they are, but something else needs to be defined.

Inside your .cpp file, you can do it like this:

 NewSoftSerial SerialServoControl::_serial(9, 8); char SerialServoControl::_tx = 0; char SerialServoControl::_rx = 0; 

Put the appropriate initial values; I just assumed that the comment was for the constructor.

+11
source

You need to create memory and initialize your static variables.

In your CPP file, add the following:

 NewSoftSerial SerialServoControl::_serial(9, 8); char SerialServoControl::_tx = 0; char SerialServoControl::_rx = 0; 
+2
source

Since the value of _serial is static, it exists when the object is not created. This means that you must declare it in code as

 NewSoftSerial SerialServoControl::_serial(9, 8); char SerialServoControl::_tx = 0; char SerialServoControl::_rx = 0; 

which has already been proposed.

If you want to change it in the init function, you just need to change the line _serial = ... to SerialServoControl::_serial = NewSoftSerial(tx, rx). Of course, this means that you will need to define an appropriate constructor for the NewSoftSerial class.

Hope this helps.

+2
source

I know this is somewhat dead, but in my case I forgot to identify the methods that I referenced, imagine that .......

Yes, they were DECLARED, but when I checked my .cpp file, there was no definition for them, so this time the error was literal.

+1
source

Source: https://habr.com/ru/post/1347336/


All Articles