Notification texts go here Contact Us Buy Now!

"Multiple definition of" Error in ESP-IDF

In the C programming language, a "multiple definition of" error occurs when the same variable is defined more than once in different source files. This can happen when the variable is defined in a header file that is included in multiple source files.

For example, consider the following header file:

```c #ifndef _TEST_H_ #define _TEST_H_ struct SomeData { float x; }; struct SomeData someData; #endif ``` If this header file is included in multiple source files, each of those source files will define the variable `someData`. When the source files are linked, the linker will encounter multiple definitions of the same variable and produce an error.

To resolve this error, you should declare the variable in the header file and define it in exactly one source file. For example:

```c // test.h #ifndef _TEST_H_ #define _TEST_H_ struct SomeData { float x; }; extern struct SomeData someData; #endif // test.c #include "test.h" struct SomeData someData; void something(void){ someData.x = 35; } ``` In this example, the variable `someData` is declared in the header file `test.h` and defined in the source file `test.c`. This ensures that there is only one definition of the variable in the program.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.