解决链接器错误:对静态类成员的未定义引用

vox*_*uro 4 c++ arduino static-members

我的代码是Arduinoish.我打开了详细编译,因此我可以验证所有.o文件确实正确地传递给链接器,它们是(下面的链接器命令).这让我相信它是某种语法错误.

谷歌搜索错误"未定义的函数引用"产生了很多结果,如"将foo.o添加到您的链接器命令"等答案,等等.

我希望解决方案就像丢失点或 - >某处一样简单.

我在链接器中的一个文件中收到了这一系列错误:

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'
Run Code Online (Sandbox Code Playgroud)

.h文件:

#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
Run Code Online (Sandbox Code Playgroud)

和.cpp文件:

#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
Run Code Online (Sandbox Code Playgroud)

链接器命令(为了清楚起见,我删除了IDE生成的显式临时目录路径,并将其分解为多行.实际命令明确指出所有这些文件的位置):

  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
Run Code Online (Sandbox Code Playgroud)

所有这些文件(SerialServoControl,Wheel_Chair_Joystick,NewSoftSerial,WheelChairMotor)都存在于Arduino sketch目录中. Core.a是编译的AVR库.

edA*_*a-y 11

您必须在源文件中的某些位置定义类静态.将它们放在类中只是声明它们在那里,但仍然需要定义它们.

在您的.cpp文件中,您可以这样做:

NewSoftSerial SerialServoControl::_serial(9, 8);
char SerialServoControl::_tx = 0;
char SerialServoControl::_rx = 0;
Run Code Online (Sandbox Code Playgroud)

放入适当的初始值; 我只是假设评论是针对构造函数的.