1

I have a weird problem. Using this code

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

    // ...
}

I am getting error "undefined reference to 'i2c'" (in function main). Based on what I was able to find about this error this code should work. Do you see anything that could cause this error?

edit: Function MX_GPIO_Init(). Same error with "undefined reference to 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);
}
joce
  • 9,624
  • 19
  • 56
  • 74
  • Have you included the header that defines `I2C_HandleTypeDef`? – Barmar Dec 24 '15 at 17:21
  • Yes, the header is properly included. When i try to typo this class i start getting error from my IDE. – Tomáš Neumaier Dec 24 '15 at 17:24
  • Do you initialize `i2c` anywhere? That may be what the IDE is warning you about. In the snippet you posted `i2c` is an undefined pointer that is pointing to some random memory address. – pstrjds Dec 24 '15 at 17:35
  • I've added the code of function where i initialize that code. – Tomáš Neumaier Dec 24 '15 at 17:45
  • 1
    Is this the exact code you have in your IDE? Maybe you've copied it from somewhere and it's got some non-printable character in `i2c`. – ForceBru Dec 24 '15 at 17:48
  • Have you reviewed [What is an undefined reference/unresolved symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/) It usually covers most issues, though a casual read of your question doesn't show why there's a problem — one reason I've not made this question a duplicate of that one. – Jonathan Leffler Dec 24 '15 at 18:11
  • Is the variable `i2c` declared in a header? If not, how does the file with `MX_GPIO_Init()` get to know about the existence of the variable? The definition shown in `main()` should be sufficient. What happens if you initialize the pointer to `nullptr`? Does `mpu9250` also show up undefined? Why not? What's different about those definitions? Is there a macro somewhere wreaking havoc? Have you looked at the output from the preprocessor to see what is being generated for the compiler proper to look at? Does that contain the `i2c` definition? Is there conditional compilation around the definition? – Jonathan Leffler Dec 24 '15 at 18:23
  • The first snippet is not complete, so it isn't possible to tell why this is happening. If you add `struct I2C_HandleTypeDef { }; struct MPU9250 { MPU9250(void *) { } }; void HAL_Init(void) { } void systemClock_Config(void) { } void MX_GPIO_Init(void) { }` to the top of the first snippet it compiles and links without the warning. This means either the code is not exactly the same or the environment (STM32 tools/library?) is breaking things, maybe it has defined some nasty macro for I2C_HandleTypeDef that is extern??? What you posted here is right and should work, but doesn't show the problem. – Anders Dec 24 '15 at 18:41
  • You need to provide enough code to replicate the problem. Since you have no code that could possibly introduce the `I2C_HandleTypeDef` type, this question doesn't meet the minimum required standard. – paxdiablo Jul 29 '18 at 04:47

0 Answers0