未定义对 avr-g++ 中元素的引用

Undefined reference to element in avr-g++

我想用 cpp 中的几个附加 类 编译一个简单的项目。不幸的是,当使用 avr-g++.exe -o stepper.elf src/add_functions.o src/ASF/common/services/hugemem/avr8/avr8_hugemem.o src/ASF/common/services/sleepmgr/xmega/sleepmgr.o src/ASF/common/services/spi/xmega_spi/spi_master.o src/ASF/xmega/drivers/dma/dma.o src/ASF/xmega/drivers/spi/spi.o src/gpio_control.o src/spi_control.o src/usart_control.o src/ASF/common/boards/user_board/init.o src/ASF/common/services/clock/xmega/sysclk.o src/ASF/common/services/ioport/xmega/ioport_compat.o src/ASF/common/services/serial/usart_serial.o src/ASF/xmega/drivers/cpu/ccp.o src/ASF/xmega/drivers/usart/usart.o src/main.o -Wl,-Map="stepper.map" -Wl,--start-group -Wl,-lm -Wl,--end-group -Wl,--gc-sections -mmcu=atxmega16a4 为以下代码调用 avr-g++ 时:

#include <asf.h>
#include <string.h>
#include "usart.h"

class usart_controller
{
//variables
public:
protected:
private:
    static usart_rs232_options_t USART_options;
    USART_t *usart;
//functions
public:
    usart_controller(USART_t *usart, uint32_t baudrate, USART_CHSIZE_t charlength, USART_PMODE_t paritybyte, bool stopbit);
    ~usart_controller();
    void send_data(uint32_t *data32, size_t data32_size);
    uint8_t * rec_data();
protected:
private:
    usart_controller( const usart_controller &c );
    usart_controller& operator=( const usart_controller &c );

}; //usart_controller


#include "usart_control.h"


// default constructor
usart_controller::usart_controller(USART_t *usart, uint32_t baudrate, USART_CHSIZE_t charlength, USART_PMODE_t paritybyte, bool stopbit)
{
    this->USART_options.baudrate = baudrate;
    //this->USART_SERIAL_OPTIONS.charlength = charlength;
    //this->USART_SERIAL_OPTIONS.paritytype = paritybyte;
    //this->USART_SERIAL_OPTIONS.stopbits = stopbit;
    this->usart = usart;
    //usart_init_rs232(usart, &(this->USART_SERIAL_OPTIONS));
} //usart_controller

// default destructor
usart_controller::~usart_controller()
{
} //~usart_controller

void usart_controller::send_data(uint32_t * data32, size_t data32_size)
{
    size_t data_size = 4 * data32_size;
    uint8_t *data = new uint8_t[data32_size * 4];
    for(unsigned int i = 0; i < data32_size; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            data[i*j+j] = (data32[i] << (j*8));
        }
    }
    usart_serial_write_packet(this->usart, data, data_size * sizeof(uint8_t));
    delete[] data;
}

uint8_t * usart_controller::rec_data()
{
    uint8_t * data = new uint8_t[32];
    usart_serial_read_packet(this->usart, data, 32*sizeof(uint8_t));
    return data;
}

我收到错误“error: undefined reference to usart_controller::USART_options'" for the commandthis->USART_options.baudrate = baudrate;`。当我单独编译它时,一切正常,我得到了一个有效的对象文件。之后尝试 link 它时,我收到上面显示的错误。为什么?我错过了什么吗?我已经 link 准备了我需要的一切。

在 c++ 中,在 class 中声明静态数据成员是不够的。它还必须在某处定义以为其分配内存。如果您添加

usart_rs232_options_t usart_controller::USART_options;

源文件中的某处(例如,在 class 定义之后),它将被很好地链接 :)

PS:爱特梅尔?玩得开心,小心那个小恶魔! :)