Multiple Definition of Variable

I’m altering a library to use in a project; the library was written to give a device WiFi credentials over BLE, but I need to give the device more than just WiFi credentials to initialize it. I am able to successfully send this extra information over BLE and receive it (and print it over serial), but this is all done in the C++ file of the original library, and I need to access this data in the .ino file of the project. I added a variable to the header file of the original library, set it in the C++ file, and tried to access it in the .ino file, but this results in a ‘multiple definition’ error. I’m almost certain there isn’t a collision with another variable name somewhere, because I’m not using any other libraries in this project and have changed the name multiple times. What could be causing this?

I was able to make a quick fix by just including the variable I wanted as a member of the class originally defined in the library and working with it that way, but I’m still curious as to why this is; it seems like this shouldn’t make a difference.

Without seeing your code and knowing which library you are using it’s hard to tell, but I’d assume that the library’s header file is not guarded against multiple includes.
When the respective header is included in the library’s .cpp and your .ino and is not guarded any global variable in there will be flagged as multiple definitions.

If you add a global that should also be used in other modules you could use the extern keyword.

But it’s usually not considered good practice to “share” globals across multiple modules.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.