2024-11-01 09:09 PM
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
..
Solved! Go to Solution.
2024-11-01 10:55 PM
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.
2024-11-01 10:55 PM
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.
2024-11-02 02:48 AM
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:
2024-11-02 02:52 AM
@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: