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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:14:08+00:00 2026-05-27T04:14:08+00:00

Suppose I created a header file constants.h and this file contains: extern const int

  • 0

Suppose I created a header file constants.h and this file contains:

extern const int YEAR = 2011; // definition

If I tried in a cpp file (MainCPP.cpp) to use this constant after declaring it without defining it and without including the constants.h file as following:

extern const int YEAR; // declaration

int main() {
    cout << YEAR << endl;
}

When I try to do that I get: unresolved external symbol “int const YEAR”. On the other hand, if I placed the definition of YEAR within constant.cpp file and done the same in MainCpp.cpp, I will not get the error and the linker will be able to link with YEAR defined within constants.cpp (without including constants.cpp in MainCpp.cpp here too).

Does this mean that the linker can link with source file code but not with header file code unless you explicitly included the header file.

  • 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-27T04:14:08+00:00Added an answer on May 27, 2026 at 4:14 am

    extern tells the compiler that space is allocated for it somewhere else. There must be a definition of it somewhere without an extern on it. BUT in C++ (unlike C), consts have internal linkage, so you don’t need the extern on it. (see Why does const imply internal linkage in C++, when it doesn't in C?)

    Just put const int YEAR = 2011; in your header file and include your header file where ever you need it.

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

Sidebar

Related Questions

What is the best practice for releasing a simple software? Suppose I created a
Suppose I have created a UIView , MyView , in Interface Builder, and I
This is about inheritance in JavaScript. Suppose I create a constructor Bird(), and another
How does a compiler detect duplicate definition across translation unit. Suppose there were a
pxI created a style defenition like this: #container { width: 900px; margin: 0 auto;
I am defining a structure in a header file, and then setting its members
I'm having a bit of trouble exporting a csv file that is created from
I'm using the following script to initiate file downloads: if (file_exists($newfilename)) { header('Content-Description: File
I have a BizTalk schema that looks something like this: <Root> <Header> <SomeData></SomeData> <Detail>
I had a question: Supposed I have a header/source file set and a header

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.