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 6144623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:40:36+00:00 2026-05-23T18:40:36+00:00

I need to create structure like this: // file main.h: #pragma once #ifndef _MAIN_H

  • 0

I need to create structure like this:

// file main.h:
#pragma once

#ifndef _MAIN_H
#define _MAIN_H

#include <iostream>

#include "first.h"
#include "second.h"

#endif
// -----------------

// file first.h:
#pragma once

#ifndef _FIRST_H
#define _FIRST_H

#include "main.h" // because of using SomeFunction() in first.cpp

int SomeVar; // used in first.cpp

#endif
// -----------------

// file second.h:
#pragma once

#ifndef _SECOND_H
#define _SECOND_H

#include "main.h" // because of using SomeVar in second.cpp

int SomeFunction(); // implemented in second.cpp

#endif
// -----------------

By logic, if main.h will be compiled first, then each of this headers will be included only once and all variables/functions will be defined normally.

For example, this configuration compiled normally in IAR C++ Compiler, if set up in options “preinclude file” (not precompiled) to main.h.

But, in Visual Studio 2010 same structure causes linker errors like:

1>second.obj : error LNK2005: "int SomeVar" (?SomeVar@@3HA) already defined in first.obj
1>second.obj : error LNK2005: "int SomeFunction" (?SomeFunction@@3HA) already defined in first.obj

I think because of including files in alphabetic order. Apparently pragma- and define-guards are ignored by linker.

Errors can be fixed by additional header and external variables definitions, but this is hard and wrong way.

Question is: What should i do?

  • 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-05-23T18:40:38+00:00Added an answer on May 23, 2026 at 6:40 pm
    int SomeVar; // used in first.cpp
    

    Variables should never be defined in headers. They should be declared with extern:

    extern int SomeVar; // used in first.cpp
    

    Then you can actually define them in “first.cpp” with int SomeVar;.


    Also, “first.h” does not need to include “main.h”. Headers should only include files if the definitions in that header absolutely need the contents of those files. The definitions in “first.h” do not need anything in “main.h”. Therefore, it should not include it.

    If “first.cpp” needs to include something from “second.h”, then it is the responsibility of “first.cpp” to include it.

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

Sidebar

Related Questions

I have a C file that looks like this: #include <stdio.h> #include <sys/types.h> #include
I've a large xml file and need to create a partial 'copy' of this
I need to create a structure or series of strings that are fixed lenght
I need to create a structure that holds a variable number of 'char[2]'s, i.e.
I need to create a db structure where Supplier inherits from Client. I used
I need to create an XML schema that validates a tree structure of an
In data processing, I frequently need to create a lookup data structure to map
Ok, so here's the entire structure I'm trying to create. I need to create
Say I have this file structure Soultion-> Folder1 -> FileIwant.html So this could be
I have a structure in C like this typedef struct SKT_SubHeader_s { U32 subLength;

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.