Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8473083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:15:04+00:00 2026-06-10T17:15:04+00:00

I know people recommend including header guards in header files, to prevent header files

  • 0

I know people recommend including header guards in header files, to prevent header files contents from being inserted by the pre-processor into the source-code files more than once.

But consider the following scenario:

Let’s say I have the files main.cpp , stuff.cpp, and commonheader.h, with the .h file having its header guards.

If either .cpp files tries to include commonheader.h more than once, then the preprocessor
will stop that from happening, and after compiling to object code we get,

main.o containing the contents of commonheader.h exactly once.

stuff.o containing the contents of commonheader.h exactly once.

Note that the contents of commonheader, have been repeated across the files, but not within the same .o file.

So what happens during the linking step? Since the .o files are being fused into an exectuable
we will have to ensure for a second time that the contents of commonheader are not being repeated. Does the compiler take care of that? If not, wouldn’t that be a problem when we are dealing with huge header files, giving rise to code repetition across files and leading to large executable sizes.

If I am making some conceptual mistake anywhere in the question, please correct me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:15:06+00:00Added an answer on June 10, 2026 at 5:15 pm

    Typically your header file should not actually define any symbols, it should just declare them. So commonheader.h would look like this (omitting the include guards):

    void commonFunc1(void);
    void commonFunc2(void);
    

    In that case, there is no problem. If you call commonFunc1 in main.cpp and stuff.cpp, both main.o and stuff.o will know they want to link against a symbol called commonFunc1 and the linker will try to find that symbol. If the linker doesn’t find the symbol, you get an undefined reference error. The actual definition of commonFunc1 needs to be in some cpp file.

    If you really want to define functions in your header file, use static so that the linker does not see them. So your commonheader.h could look like:

    static void commonFunc1()
    {
        /* ... do stuff ... */
    }
    

    In this case, the linker does not know about commonFunc1 and no errors will occur. This could increase the executable size though; you’ll probably end up with two copies of the code for commonFunc1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that people recommend that externals should always point to tags or be
I know that most people recommend using HttpRuntime.Cache because it has more flexibility... etc.
I know people asked this before and I found this solution: for (UIView *subview
I know people have asked similar questions, but I believe the causes of their
I know many people ask this but all the answers are specific apps so
I would like to know how people tackle backup and restore of data in
I would like to know other people's opinion on the following style of writing
I use VIM editor for PHP, i know many people will point to PDT
I know that most people have DirectX9-capable cards but is it really necessary to
I know of some people who use git pull --rebase by default and others

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.