![](/img/trans.png)
[英]I can't figure out why I'm getting an undefined reference issue when calling methods within my header file
[英]I can see the definition in the header file, so why is the compiler complaining of an “undefined reference”?
我从Analog Devices购买了开发板,用于他们的ad7176-2模数IC。 我下载了“通用驱动程序文件”(不是特定于操作系统)。 我一直在Raspberry Pi上编译它们,Raspberry Pi是运行Debian变种Raspian的手臂设备。 我将Code :: Blocks用作IDE,并且同时使用了“ GNU GCC”和“ GNU ARM GCC”编译器。
我收到错误消息“对'AD7176_regs的未定义引用”
这是来自AD7176_regs.h文件的代码,该文件包含在AD7176.h中,包含在AD7176.h中,而该文件包含在编译器发现错误的AD7176.c中。
编码:
/*! AD7176 register info */
typedef struct _st_reg
{
int32_t addr;
int32_t value;
int32_t size;
}st_reg;
/*! AD7176 registers list*/
enum AD7176_registers
{
Status_Register = 0x00,
ADC_Mode_Register,
Interface_Mode_Register,
Register_Check,
Data_Register,
IOCon_Register,
ID_st_reg,
CH_Map_1,
CH_Map_2,
CH_Map_3,
CH_Map_4,
Setup_Config_1,
Setup_Config_2,
Setup_Config_3,
Setup_Config_4,
Filter_Config_1,
Filter_Config_2,
Filter_Config_3,
Filter_Config_4,
Offset_1,
Offset_2,
Offset_3,
Offset_4,
Gain_1,
Gain_2,
Gain_3,
Gain_4,
Communications_Register,
AD7176_REG_NO
};
**Defining the undefined**
#ifdef AD7176_INIT
/*! Array holding the info for the AD7176 registers - address, initial value, size */
st_reg AD7176_regs[] =
{
{0x00, 0x00, 1}, //Status_Register
{0x01, 0x0000, 2}, //ADC_Mode_Register
{0x02, 0x0100, 2}, //Interface_Mode_Register
{0x03, 0x0000, 3}, //Register_Check
{0x04, 0x0000, 3}, //Data_Register
{0x06, 0x0000, 2}, //IOCon_Register
{0x07, 0x0000, 2}, //ID_st_reg
{0x10, 0x8002, 2}, //CH_Map_1
{0x11, 0x0000, 2}, //CH_Map_2
{0x12, 0x0000, 2}, //CH_Map_3
{0x13, 0x0000, 2}, //CH_Map_4
{0x20, 0x0000, 2}, //Setup_Config_1
{0x21, 0x0000, 2}, //Setup_Config_2
{0x22, 0x0000, 2}, //Setup_Config_3
{0x23, 0x0000, 2}, //Setup_Config_4
{0x28, 0x020A, 2}, //Filter_Config_1
{0x29, 0x0200, 2}, //Filter_Config_2
{0x2a, 0x0200, 2}, //Filter_Config_3
{0x2b, 0x0200, 2}, //Filter_Config_4
{0x30, 0, 3}, //Offset_1
{0x31, 0, 3}, //Offset_2
{0x32, 0, 3}, //Offset_3
{0x33, 0, 3}, //Offset_4
{0x38, 0, 3}, //Gain_1
{0x39, 0, 3}, //Gain_2
{0x3a, 0, 3}, //Gain_3
{0x3b, 0, 3}, //Gain_4
{0xFF, 0, 1} //Communications_Register
};
#else
extern st_reg AD7176_regs[AD7176_REG_NO];
#endif
这是我们要访问寄存器值但收到错误消息的地方之一。
/***************************************************************************//**
* @brief Reads the conversion result from the device.
*
* @param pData - Pointer to store the read data.
*
* @return Returns 0 for success or negative error code.
*******************************************************************************/
int32_t AD7176_ReadData(int32_t* pData)
{
int32_t ret;
/* Read the value of the Status Register */
ret = AD7176_ReadRegister(&AD7176_regs[Data_Register]);
/* Get the read result */
*pData = AD7176_regs[Data_Register].value;
return ret;
}
如果我解决了这个问题,我应该将GCC用于ARM还是GCC。 该代码将在Raspberry Pi上运行,并控制开发板上的ADC IC。
谢谢彼得
显然, AD7176_INIT
没有定义,因此您最终得到:
extern st_reg AD7176_regs[AD7176_REG_NO];
它声明了AD7176_regs
但未定义它,这是您收到链接器未定义参考错误的原因。
现在,对象定义不应出现在头文件中,而应将定义放置在.c
文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.