我想在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;
}
我收到错误&#34;错误:对usart_controller::USART_options'" for the command
未定义的引用this-&gt; USART_options.baudrate = baudrate;`。当我单独编译它时,一切都很顺利,我得到一个有效的目标文件。当我试图将其链接后,我得到上面显示的错误。为什么?我错过了什么?我已经把我需要的东西连接起来了。
答案 0 :(得分:1)
在c ++中,在类中声明静态数据成员是不够的。它还必须在某处定义为它分配内存。如果你添加
usart_rs232_options_t usart_controller::USART_options;
在您的源文件中的某个地方(例如,在类定义之后),它将被链接得很好:)
PS:Atmel?玩得开心,小心那个小魔鬼吧! :)