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

  • Home
  • SEARCH
  • 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 6249295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:12:38+00:00 2026-05-24T13:12:38+00:00

There are few global variables which are static in one of the header files.

  • 0

There are few global variables which are static in one of the header files. I see these variables are used in the associated .cc files. So, looks like this has no issues.

My questions are:

  • Whats the difference between including a global variable vs static global variable ?
    I know static global doesnt have visibility outside its file. But dont know how this would work when it comes as part of a .h which is #included.

  • I wrote a sample program, and tried the same thing. But, I get compilation error the moment I make the variable static. When it is just global, it is fine.
    So, is there something which I am missing on a regular g++ build ? (Please note, the initial case was on our official code base which has enough makefiles, .h files and all).

Thanks for the help !

Here is my sample program :

.h file:

#include <iostream>

typedef unsigned int uint;

static const int appk=189;

class abc1
{
    public:
        abc1(int x);
        virtual void printVal();

};

.cc file:

#include "abc1.h"

extern int appk;

abc1::abc1(int x)
{

}

void abc1::printVal()
{
    printf("abc1 print: %d\n", appk);
}
  • 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-24T13:12:39+00:00Added an answer on May 24, 2026 at 1:12 pm

    (1) If you put a global variable in a .h file and include it in various .cpp/.cc files then it will be defined multiple times for every file. So you are most like to get a linker error.
    To overcome that, mostly you are likely to use extern keyword:

    // myfile.h
    extern int i;
    

    and define that in only one translation unit:

    // somefile.cc
    int i;
    

    (2) If you put a static global in a .h file and include it, then you will not get any error, because for every different translation unit, there will be a different copy for that static global variable.

    // myfile.h
    static int i;  // creates a unique and unrelated copy in all .cc file where included
    

    However, such usage is deprecated; instead of that it’s better to use unnamed namespace:

    namespace {
      int i;
    }
    

    From your question, I don’t see that you should get any linker error for static global.

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

Sidebar

Related Questions

It looks like there a few working solutions for using custom true type fonts
I have few variables which are used system-wide in my rails app. It runs
I need a few global variables that I need in all .js files. For
in my page there are few hidden field which are filled with the correct
I'm converting an old VB form to .NET, and there a few Buttons which
There's a few previous questions on StackOverflow questioning how one goes about accessing local
I'll first describe my initial problem - I have a few site global variables
I have a webpage where there are few text fields to be filled up
I know how to use locks in my app, but there still few things
I have an application that's locked down using forms authentication. There are few nightly

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.