How do I ensure declarations are consistent across files?

If you have a variable, structure, or another data type that you want to define in one source file and access in another source file, you can do so using an extern declaration.

The best way to do this is to create a header file(s) that can hold all the declarations and that can be included in each source file that needs to access that particular variable. A variable is said to be defined when the compiler allocates the storage for the variable. A variable is said to be declared when the compiler knows of its data type, and that the variable exists, but has not allocated storage to it.

Let’s say you have a volatile int variable called IOstate that must be accessible anywhere in the program. The file main.c, for example, might define the variable:

Now, create a header file (say vars.h) and declare the IOstate as:

The declaration must include any qualifiers used in the definition. Now this header file can be included to C source files that need to access this variable:

If vars.h was shared between file_1.c and file_2.c.

To prevent duplicate definitions and declarations, guards may be applies as follows in vars.h:

file_1.c and file_2.c may contain

in order to access the shared symbols and variables.

If vars.h is stored in the project parent directory, you don’t need to include the paths to it under the compiler build options.

You should also include the header file into the source file that actually defines the variable. This makes sure that the definition in the C source file is checked against the declaration in the header file. If there is any inconsistency, the compiler will send out a warning and the user should make the declaration and the definition consistent.

You should never place definitions in a header file, other than defining types (typedefs) or structure/union tags, etc.
For example, the definition of count is as:

This definition should not be put in the header file; it should ideally be put into the C source file.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.