cancel
Showing results for 
Search instead for 
Did you mean: 

F410: multiple definition of `huart2';

HMSEdinburge
Associate

HI there, i m using F410 Nucleo, and i keep getting these errors when using USART2, as I declare in 2 separe files i.e. main.c and buffer.  could anyone advise how to resolve this?

error: multiple definition of `huart2';

 

//main.c
..
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart2;
..
  /* USER CODE END USART2_Init 1 */
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 115200;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;



// buffer.c
#include "buffer.h"
#include <string.h>
/**** define the UART   ****/
UART_HandleTypeDef huart2;    // woldes
#define uart &huart2
..

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
gbm
Lead III

Your question is about basics of C programming; it's not related to STM32.The whole program may contain only a single definition of a symbol. Define the huart2 in one module only (maybe buffer.c - it's already defined there), put its declaration as extern in buffer.h:

extern UART_HandleTypeDef huart2;

Include buffer.h in main.c.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice

View solution in original post

3 REPLIES 3
gbm
Lead III

Your question is about basics of C programming; it's not related to STM32.The whole program may contain only a single definition of a symbol. Define the huart2 in one module only (maybe buffer.c - it's already defined there), put its declaration as extern in buffer.h:

extern UART_HandleTypeDef huart2;

Include buffer.h in main.c.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
Andrew Neil
Evangelist III

As @gbm said, this is standard C stuff - nothing specific to STM32.

 


@HMSEdinburge wrote:

 as I declare (sic) in 2 separe files i.e. main.c and buffer. 


No, you are defining in both files - that's why the compilers says, "multiple definitions"!

 

This is a definition - the comment even says it:

/**** define the UART   ****/
UART_HandleTypeDef huart2;    // woldes

and this is also a definition:

/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart2;

So that is multiple definitions!

Note that the comment saying "Private variables" is incorrect:  to make that variable private, you would need to make it static, but then it would be distinct from the huart2 in the other file - they would not be the same variable.

 

See this post for how to properly share variables (and functions) between files in C:

https://community.st.com/t5/stm32cubeide-mcus/adding-new-c-source-files-to-my-project-and-navigating-through/m-p/657455/highlight/true#M25847


@gbm wrote:

Include buffer.h in main.c.


Indeed.

@HMSEdinburge  Also Include buffer.h in buffer.c - that way the compiler gets to check that they are  consistent:

https://community.st.com/t5/stm32cubeide-mcus/adding-new-c-source-files-to-my-project-and-navigating-through/m-p/658310/highlight/true#M25925