• Advertisement
Sign in to follow this  

Including header file in multiple sources in same project

This topic is 3973 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Define the variable in a single translation unit, and place an extern declaration of that variable in the header file.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement