Including header file in multiple sources in same project

Started by
2 comments, last by edd1986 17 years, 1 month ago
I should know this but am drawing a complete blank. I have several files within my project that need to have access to variables in a header file. Including the header file in each of these files leads to linker errors regarding the symbols already being defined, what is the solution? Thanks in advance
Advertisement
Define the variable in a single translation unit, and place an extern declaration of that variable in the header file.
At the top of your headers, you can write #pragma once, to block against multiple inclusion. This will stop redefintions from including it all over the place. To access the variables

in the h file

extern var_type varname;

Then you just have to define it properly in one of your source files. It can be used anywhere that includes the header.
Excellent, thanks :)

This topic is closed to new replies.

Advertisement