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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:37:01+00:00 2026-06-11T01:37:01+00:00

i have a header file global.h where i declare a few variables that i

  • 0

i have a header file global.h where i declare a few variables that i intend to use in other files.

#ifndef GLOBAL_H_
#define GLOBAL_H_

#include <stdio.h>

typedef struct tag_KG_Data
{
   int nKGStationID;
   int nKGComPort;

 }GLOBAL_VAR;

 GLOBAL_VAR g_GlobalVar;

 BOOL b_newDataReady;
 BOOL b_startedSocketClient;



#endif

At first i declared only GLOBAL_VAR g_GlobalVar in file test1.cpp with extern GLOBAL_VAR g_GlobalVar;, and worked just fine. Then i declared the 2 BOOLs and used them in test2.cpp, but i get an error LNK2005: "struct tag_KG_Data g_GlobalVar" (?g_GlobalVar@@3Utag_KG_Data@@A) already defined in test1.obj and for every global variable i have i get a similar error. The thing is that i don’t use GLOBAL_VAR g_GlobalVar in test2.cpp or any of the BOOLs in test1.cpp.

  • 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-11T01:37:03+00:00Added an answer on June 11, 2026 at 1:37 am

    This is because you are defining the globals in the header, while you should be only declaring them.

    Add extern in front of your global definitions, and create a definition in a single cpp file.

    In the header:

    // Declarations
    extern GLOBAL_VAR g_GlobalVar;
    extern BOOL b_newDataReady;
    extern BOOL b_startedSocketClient;
    

    In a cpp file:

    // Definitions
    GLOBAL_VAR g_GlobalVar;
    BOOL b_newDataReady;
    BOOL b_startedSocketClient;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a header file variable.h where i declare all my global variable.Then i
I have a header file like this: #ifndef __GEN_NOTE_MARKERS_TO_DEVELOPERS_HPP__ #define __GEN_NOTE_MARKERS_TO_DEVELOPERS_HPP__ #ifdef _DEBUG //
I have a header file with all the enums listed (#ifndef #define #endif construct
Suppose you have a static global variable in your header file, and you use
I have a header file, lets say Common.h, that is included in all of
I have a header file. In this header I want to use a map
I have a set of global variables and a method in a cpp file.
I have a 2 modules (.c files) and one .h header file: file1.c: #include
In my game I have a header file that contains properties and functions for
I have a C++ program with the following in the header file: #define VARIABLE_X

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.