未定义的变量引用

时间:2015-12-24 17:18:33

标签: c++

我有一个奇怪的问题。使用此代码

I2C_HandleTypeDef* i2c;
MPU9250* mpu9250;

int main(void)
{
    HAL_Init();
    /* Configure the system clock */
    SystemClock_Config();
    /* Initialize all configured peripherals */
    MX_GPIO_Init();

    mpu9250 = new MPU9250(i2c);

    // ...
}

我收到错误"未定义引用' i2c'" (在函数main中)。基于我能够找到的有关此错误的内容,此代码应该可以正常工作。你看到任何可能导致这个错误的东西吗?

编辑: 功能MX_GPIO_Init()。与"未定义的对i2c"的引用相同的错误。

void MX_GPIO_Init(void) {

    GPIO_InitTypeDef GPIO_InitStruct;

    /* GPIO Ports Clock Enable */
    __GPIOB_CLK_ENABLE();

    /*Configure GPIO pins : PB6 PB7 */
    GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    i2c = new I2C_HandleTypeDef;
    i2c->Instance = I2C1;
    i2c->Init.ClockSpeed = 100000;
    i2c->Init.DutyCycle = I2C_DUTYCYCLE_2;
    i2c->Init.OwnAddress1 = 0;
    i2c->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
    i2c->Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
    i2c->Init.OwnAddress2 = 0;
    i2c->Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
    i2c->Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
    HAL_I2C_Init(i2c);
}

0 个答案:

没有答案