Loading...
「ツール」は右上に移動しました。
利用したサーバー: wtserver1
0いいね 2 views回再生

Solving the Global Variables Not Allowed in Headers Problem in C+ + Arduino Projects

Discover how to fix the common 'multiple definition' error in your Arduino PlatformIO project caused by global variables in header files.
---
This video is based on the question https://stackoverflow.com/q/67651983/ asked by the user 'user11914177' ( https://stackoverflow.com/u/11914177/ ) and on the answer https://stackoverflow.com/a/67652043/ provided by the user 'selbie' ( https://stackoverflow.com/u/104458/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Global variables not allowed in headers?

Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Understanding Global Variables in C+ + Arduino Projects

When working on a C+ + Arduino project, especially with PlatformIO, you may encounter an error that says something similar to:

[[See Video to Reveal this Text or Code Snippet]]

This typically hints at a problem with how global variables are declared and used across multiple files. Let's explore the problem and its solution step by step.

The Problem

In your scenario, you have two source files:

main.cpp

test.cpp

Both files are including a common header file, test.hpp, which declares a global variable:

[[See Video to Reveal this Text or Code Snippet]]

This declaration leads to a common issue called "multiple definition," which occurs because both main.cpp and test.cpp independently create a value variable. When code is compiled, the linker cannot decide which one to link, resulting in the error message you've seen.

Symptoms of the Issue

When you attempt to build your project, you might notice:

An error message complaining about multiple definitions.

Confusion about why defining a variable seems to work in one file but not another.

The Solution

To resolve this problem, you need to ensure that the global variable is declared correctly for use in multiple files. Here’s how you can do it:

Step 1: Update test.hpp

Replace the current definition of value with an extern declaration in test.hpp. The extern keyword tells the compiler that the actual definition of the variable exists in another file:

[[See Video to Reveal this Text or Code Snippet]]

Step 2: Define the Variable in test.cpp

Now, define the variable only in one of your source files, specifically in test.cpp:

[[See Video to Reveal this Text or Code Snippet]]

Step 3: Use the Variable in main.cpp

Now that value is declared in the header and defined in test.cpp, you can use it in main.cpp without encountering the multiple definition error:

[[See Video to Reveal this Text or Code Snippet]]

Conclusion

By following these steps to correctly declare and define your global variables, you can avoid the common pitfalls associated with multiple definitions in C+ + projects.

Summary of Key Points

Use extern to declare a global variable in a header file.

Define the variable in only one source file to prevent "multiple definition" errors.

Make sure all source files that need to use the variable include the header file.

By applying these best practices, you can effectively manage your global variables in C+ + Arduino projects, ensuring a smoother development process. Happy coding!

コメント