解析链接错误:未定义引用静态类成员 [英] Resolving a linker error: undefined reference to static class members

查看:311
本文介绍了解析链接错误:未定义引用静态类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code是Arduinoish。我打开详细的编译,所以我可以验证所有.o文件确实越来越传递到链接器正确,他们(以下链接器命令)。这使我相信这是某种形式的语法错误。

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.

谷歌搜索错误未定义的参考功能产生了不少成果中包含像这样添加foo.o的到你的链接器命令,等等。

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.

我希望的解决方案是为缺少点简单或 - >地方

I hope the solution is as simple as a missing dot or -> somewhere.

我得到这个一系列的错误在一个文件中,从连接器:

I'm getting this series of errors in one 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文件:

#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

和.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

链接器命令(我已经删除了IDE生成清晰,打破它到多行明确的临时目录路径的实际命令是明确为所有这些文件的位置):

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):

  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

在Arduino的所有这些文件(SerialServoControl,Wheel_Chair_Joystick,NewSoftSerial,WheelChairMotor)的存在小品目录。 Core.a 是编译的AVR库。

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

推荐答案

您在源文件中定义类静态一些地方。把他们在类中添加声明,他们在那里的事,但仍然需要定义它们。

You have to define your class statics in a source file some where. Putting them in the class just declares that they are there, but something still needs to define them.

里面的的.cpp 文件你可以这样做是这样的:

Inside your .cpp file your can do so like this:

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

将适当的初始值;我只是认为该意见的构造函数。

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

这篇关于解析链接错误:未定义引用静态类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆