6

I've heard that you shouldn't define anything in header files, because of the possibility of multiple defines, but if you have include guards, this shouldn't happen, right? What other reasons are there for adding extern to variables?

2 Answers 2

12

Include guards merely prevent multiple inclusion of a header within a single translation unit (aka compilation unit). This does not address the problem of multiple definitions from separate translation units at link time. Hence you should only ever put declarations in header (.h) files, and definitions in source (.c) files.

Sign up to request clarification or add additional context in comments.

Comments

1

You generally declare extern variables in header files when the variable is defined in one source file (more specifically, one translation unit), and referenced in another.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.