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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:24:48+00:00 2026-06-16T12:24:48+00:00

I have been reading about how linkers work and everything that comes around this

  • 0

I have been reading about how linkers work and everything that comes around this process in order to explain my problem in this(link) question (it’s short, sorry about the reference,but it’s relevant).

The question:
If in a header I have a variable declaration (using extern), and I want to use this variable in multiple source files (#including the header file in each one of them course), I have to provide a definition for this variable somewhere. The problem is that if I provide a definition in the main function of some of the source files for example, the other files still don’t “see” this definition (which results in a linking error). If I define this extern variable in one of the global scopes, it works ok, and everybody sees it.

Why? How can other files access the global scope of another file? And aren’t global scope variables dangerous? Placing the variable definition in what other scope can resolve such a linking error? Does somebody use such extern variable declarations in headers and for what purpose?

Edit: The exact example situation is in the link I have posted in the beginning

  • 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-16T12:24:49+00:00Added an answer on June 16, 2026 at 12:24 pm

    A few clarifications:

    What you’re calling a “file” is more properly called a translation unit. It doesn’t matter how many files are involved. Indeed, the same file could be re-compiled (say with different #defines, etc.)

    Armed with that term, we can now rephrase your question: “How can a translation unit access the global scope of another translation unit”?

    Quoting from the wikipedia article on Linkage:

    If the name has external linkage, the entity that name denotes may be referred to from another translation unit using a distinct declaration for that same name, and from other scopes within the same translation unit using distinct declarations.

    In other words: by making sure that name as external linkage, it’s in the global scope, and that’s global for the program, not the translation unit.

    Your remaining questions:

    1. Aren’t global scope variables dangerous?

      I would never use the word “dangerous” to describe them, as I would certain standard library functions that have security flaws, etc. But I would say that can very often lead to very poorly designed code, and in the case of global variables specifically, a lot of headaches in multithreaded code. A good rule: avoid them unless you have a good reason not to.

    2. How to avoid the linkage error (in some other scope)

      No other magic declaration here: it’s either visible outside the translation unit (external) or not (static). What you’re probably asking for is : “What’s the right way to let other modules access the variable?” And the answer is: write an accessor function (or even better, a class that manages that data with methods).

    3. Who uses this stuff and for what purpose?

      Always a hotly debated topic. Many people advise avoiding completely, but you’d be hard pressed not to find an extern in a project of sufficient size. The best reasons usually come down to the kind of things purists ignore: compatibility with legacy systems, performance, easy access for debuggers and other introspecting tools… or, of course, just cause it was easy, it was understood, and it just worked.

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

Sidebar

Related Questions

I have been reading about Quicksort and found that sometimes it' s referred to
i have been reading about multiple inheritance What is the exact problem with multiple
i have been reading about a function that can overwrite its return address. void
I have been reading about JPA and EJB3 and would like to confirm that
I have been reading about css and js compression and minimizing. In particular that
I have been reading about a database named Starcounter. It makes a claim that
I have been reading about Ruby 1.9 Thread and I see that all ruby
i have been reading about network flooding algorithm that it is used to discover
I have been reading about this but I have to do some thing. I
I have been reading about the function of __init__.py file. It is said that

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.