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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:47:59+00:00 2026-06-11T11:47:59+00:00

There is one variable called BOT_TIME that varies with the difficulty of my game,

  • 0

There is one variable called BOT_TIME that varies with the difficulty of my game, and hence isn’t const. There are many files that use it. I intend to use it as a global variable.

1) In constants.h I declare it extern int BOT_TIME.
In constants.cpp, I declare it extern int BOT_TIME.

BUILD => undefined references to the variable in all sources(Yes, I’ve included the header).

2) In constants.h I declare it int BOT_TIME.
In constants.cpp, I declare it int BOT_TIME.
Since non-consts are by default extern , I decided to leave that keyword.

BUILD => Multiple definition of the variable (shows in each source file that has constants.h included)

3) In constants.h I declare it extern int BOT_TIME.
In constants.cpp, I declare it int BOT_TIME.

This works.

Where is the issue?

Initializing the variable to something in constants.cpp makes it work for cases 1 and 3.

What is this happening?
Which is the right approach?

  • 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-11T11:48:00+00:00Added an answer on June 11, 2026 at 11:48 am

    You can declare a variable as many times as you want, you can and have to define it only once.

    extern int BOT_TIME;
    

    is a declaration.

    int BOT_TIME;
    

    is a definition.

    The definition has to appear in a single implementation file.

    Since non-consts are by default extern , I decided to leave that keyword.

    Nope. How’d you figure?

    To answer the questions:

    1) There’s no definition, only declarations.

    2) The assumption is wrong. You define the symbol multiple times.

    3) It works because that’s the correct way to do it.

    From the comments:

    When you declare a variable with extern, you specify that it has external linkage, yes, but you only declare, and not define it. If you leave it without the extern keyword, it still has external linkage, but it’s also a definition.

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

Sidebar

Related Questions

There's one specific feature that I wish was in the .NET framework, but isn't.
there is one variable var=581 we need to copy the value of this variable
Is there one method or property in the .NET Framework that tells if an
There is one form, inside the form there are many checkboxes. Two operations can
I am developing a live-action tag game (called DeTag) that uses a web interface
I'm starting a web application with MVC3 and Ninject. There is one dependency that
So I create a session on one page and create a new variable called
I'm looking for a standard dual-map structure - is there one implemented in std/boost/another
Network file systems are offered by Windows and (L)Unix. Is there one for IBM
There is one table. Parent_id references same table id. How insert raw if parent_id

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.