How extern works with header files? (C++ VS2022)

Nikolas Fraser 60 Reputation points
2023-10-25T00:48:21.5933333+00:00

Whenever I try using extern in my header file, the program fails to build. If I don't use extern, the value of the variable is not saved across .cpp files. I've made absolutely sure that the header file is included and the variable name is spelled correctly (there's also no error without the externkeyword).

This is what I've been doing, based on what I've read from numerous sources:

**
globals.h

#pragma once
#include <string>
extern std::wstring name;

void Start();

globals.cpp

#include <string>
#include 'globals.h'

void Start()
{
	std::wstring name = L"Bob";
}

source1.cpp

#include <string>
#include 'globals.h'

Start();

std::wstring someone = name; // LNK2001 unresolved external symbol ...
std::wstring bob = L"Bob";
name = bob;  // error C4430: missing type specifier ...


void myFunction()
{
	wstring bob = L"Bob";

	name = bob; // LNK2001 unresolved external symbol ...
	::name = bob; // LNK2001 unresolved external symbol ...


	wstring someone = name; // LNK2001 unresolved external symbol ...
	wstring someone = ::name; // LNK2001 unresolved external symbol ...
}

How do I correctly use a global variable shared across multiple .cpp files?

Thanks

Developer technologies C++
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Minxin Yu 13,501 Reputation points Microsoft External Staff
    2023-10-25T02:17:39.25+00:00

    Hi, @Nikolas Fraser

    The snippet needs the following modifications:
    std:: is missing
    Supplement the variable type when providing a definition for a global variable. Please check the doc: extern linkage for non-const globals
    //fileA.cpp

    int i = 42; // declaration and definition
    Use Start() inside function.
    Variable names are duplicated
    Updated:
    User's image

    main.cpp

    #include "globals.h"
    #include"source.h"
    #include<iostream>
    //Start();
    std::wstring someone = name;
    std::wstring bob = L"Bob";
    std::wstring name = bob;
    
    void myFunction()
    {
    	std::wstring bob = L"Bob";
    
    	name = bob;
    	//::name = bob; 
    
    
    	std::wstring someone = name;
    	//std::wstring someone = ::name;
    }
    int main()
    {
    std::wcout << name<<std::endl;
    	foo();
    	std::wcout << name;
    }
    

    Source.h

    #pragma once
    void foo();
    

    Source.cpp

    #include<string>
    extern std::wstring name;
    void foo()
    {
    	name = L"aaa";
    }
    

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Nikolas Fraser 60 Reputation points
    2023-10-25T03:39:50.0966667+00:00

    It is mandatory to declare the variable at the global scope inside globals.cpp to resolve properly.

    globals.h

    #pragma once
    #include <string>
    
    extern std::wstring name; // declare for external use
    
    void Start();
    

    globals.cpp

    #include <string>
    
    #include 'globals.h'
    
    std::wstring name;  // **** mandatory to declare here ****
    
    void Start()
    {
        name = L"Bob"; // now we can define it
    }
    

    source.h

    #include <string>
    
    #include 'globals.h'
    
    extern std::wstring zoneName; // declare as an external
    

    source.cpp

    #include <string>
    #include <iostream>
    
    #include 'globals.h'
    
    int main()
    {
    	Start(); // value is set in this function
    	std::wcout << name;  // name set in globals is output here in source
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.