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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:04:26+00:00 2026-06-17T20:04:26+00:00

This is the subject code: externfile.cpp int i = 10; mainfile.cpp #include <iostream> using

  • 0

This is the subject code:

externfile.cpp

int i = 10;

mainfile.cpp

#include <iostream>

using namespace std;

extern int i;
int main(int param)
{
    cout << i << '\n';
    int i = 0;
    cout << i << '\n';
    cout << ::i << '\n';
}

When compiling this program (using Visual Studio 2008), it works fine and the output will be:

10
0
10

which is not surprising for me, this is a matter of scopes.

But what makes me confused is: How could mainfile.cpp file get the value of i from another .cpp file (externfile.cpp in our case)? is it only because they reside in the same directory? or the same solution?

And in a better way: How are source files “merged” while compiling a project (with VS2008, if I should specify)? in what “order” are they placed? and how are they scoped?

  • 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-17T20:04:27+00:00Added an answer on June 17, 2026 at 8:04 pm

    A “normal” VC++ project1 instruct VC++ to compile each source file by itself to an “object file” (or object module), where the compiler leaves “placeholders” for symbols that aren’t resolved yet (like external variables or functions that are declared but not defined).

    Then, all the object files are tied together by the linker, which ties them together to produce the final executable. During this passage, the “placeholders” are replaced by the actual address of the code/data they refer to that is defined in the various object files. If some needed definition is not found you get an undefined reference error, if some symbol is found more than once you get a multiple definition error2.

    Fore more information about the classical model for linking, have a look at this article by Raymond Chen (and, if you are interested, to the whole series).


    1. Surely there’s room for more flexibility, here I’m just describing what’s the usual situation in VC++.
    2. This is a consequence of the C++ “one definition rule”; still, there are some exceptions to this, in particular inline functions and template instantiations; in those cases, the linker just takes whatever it prefers (which shouldn’t be a problem, since multiple definitions of such objects must be the same).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is MooTools code: var myString = {subject} is {property_1} and {property_2}.; var myObject
So I have this code that checks 4 parameters (author, title, keyword and subject)
I found a forum with this subject. Using enum as id But I couldn't
I made another post earlier about this subject but I've since changed my code
After hours of Googling on this subject matter, I found the following code snippet
Given this example code: 1: using (StreamWriter sw = new StreamWriter(@C:\file.txt)) 2: { 3:
i am using this class https://code.google.com/p/php-mime-mail-parser to parse emails class is fully of problems
I've read many tutorials on this subject and concluded with the below code. The
I'm new in this subject so this might be a silly question for most
I did a lot of searching on this subject, but I couldn't find anything

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.