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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:27:39+00:00 2026-05-13T20:27:39+00:00

if I define my constant varibles in my header like this… extern const double

  • 0

if I define my constant varibles in my header like this…

extern const double PI = 3.1415926535;
extern const double PI_under_180 = 180.0f / PI;
extern const double PI_over_180 = PI/180.0f;

I get the following error

1>MyDirectX.obj : error LNK2005: "double const PI" (?PI@@3NB) already defined in main.obj
1>MyDirectX.obj : error LNK2005: "double const PI_under_180" (?PI_under_180@@3NB) already defined in main.obj
1>MyDirectX.obj : error LNK2005: "double const PI_over_180" (?PI_over_180@@3NB) already defined in main.obj
1>MyGame.obj : error LNK2005: "double const PI" (?PI@@3NB) already defined in main.obj
1>MyGame.obj : error LNK2005: "double const PI_under_180" (?PI_under_180@@3NB) already defined in main.obj
1>MyGame.obj : error LNK2005: "double const PI_over_180" (?PI_over_180@@3NB) already defined in main.obj

but If I remove those constants from the header and put them in the document that is including the header like this…

const double PI = 3.1415926535;
const double PI_under_180 = 180.0f / PI;
const double PI_over_180 = PI/180.0f;

It works

Does anyone have Idea what I might be doing wrong ??

Thanks

  • 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-13T20:27:40+00:00Added an answer on May 13, 2026 at 8:27 pm

    The problem is that you define objects with external linkage in header file. Expectedly, once you include that header file into multiple translation units, you’ll get multiple definitions of the same object with external linkage, which is an error.

    The proper way to do it depends on your intent.

    1. You can put your definitions into the header file, but make sure that they have internal linkage.

      In C that would require an explicit static

      static const double PI = 3.1415926535; 
      static const double PI_under_180 = 180.0f / PI; 
      static const double PI_over_180 = PI/180.0f; 
      

      In C++ static is optional (because in C++ const objects have internal linkage by default)

      const double PI = 3.1415926535; 
      const double PI_under_180 = 180.0f / PI; 
      const double PI_over_180 = PI/180.0f; 
      
    2. Or you can put mere non-defining declarations into the header file and put the definitions into one (and only one) implementation file

      The declarations in the header file must include an explicit extern and no initializer

      extern const double PI; 
      extern const double PI_under_180; 
      extern const double PI_over_180; 
      

      and definitions in one implementation file should look as follows

      const double PI = 3.1415926535; 
      const double PI_under_180 = 180.0f / PI; 
      const double PI_over_180 = PI/180.0f; 
      

      (explicit extern in the definitions is optional, if the above declarations precede the definitions in the same translation unit).

    Which method you will choose depends on your intent.

    The first method makes it easier for the compiler to optimize the code, since it can see the actual value of the constant in each translation unit. But at the same time conceptually you get separate, independent constant objects in every translation unit. For example, &PI will evaluate to a different address in each translation unit.

    The second method creates truly global constants, i.e. unique constant objects that are shared by the entire program. For example, &PI will evaluate to the same address in each translation unit. But in this case the compiler can only see the actual values in one and only one translation unit, which might impede optimizations.


    Starting from C++17 you get the third option, which sort of combines “the best of both worlds”: inline variables. Inline variables can be safely defined in header files despite having external linkage

    inline extern const double PI = 3.1415926535; 
    inline extern const double PI_under_180 = 180.0f / PI; 
    inline extern const double PI_over_180 = PI/180.0f; 
    

    In this case you get a named constant object whose initializer value is visible in all translation units. And at the same time the object has external linkage, i.e. it has a global address identity (&PI is the same in all translation units).

    Granted, something like that might only be necessary for some exotic purposes (most use cases in C++ call for the first variant), but the feature is there.

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

Sidebar

Related Questions

I have a file that defines constant variables, like this: define_vars.php <? define(something,value); define(something1,value);
I'd like to define a constant char* in my header file for my .cpp
I would like to define a constant (like the admin-email-adress) depending on the environment.
Suppose I have a constant defined in a header file #define THIS_CONST 'A' I
I have a constant like define(EMPLOYEE_NAME_ID,employee); and a variable $code = EMPLOYEE; now i
I would like to define in my code a constant holding the date on
I want to define a constant in objective-c. Previously I had the following function:
I need to define the constant in the module that use the method from
I want to define a constant that should be available in all of the
I need to define some constant strings that will be used only by one

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.