Search code examples
c++visual-studiolinkerexternunresolved-external

global variable declaration with extern


My problem is in the following context :

file1.h

#include "graphwnd.h"
#include "file2.h"

class XXX: 
{
....various things....
protected:
CGraphWnd graph_wnd_sensor1D;
}  

file1.cpp

#include "file1.h"
(... various stuff ...)

void main(){
OnInitGraph(&graph_wnd_1_sensor2D, rect_1_sensor2D);
graph_wnd_sensor1D.ShowWindow(SW_HIDE);
myYYY.Init();
}
(... various stuff ...)

here graph_wnd_sensor1D has a value and the ShowWindow works

file 2.h

extern CGraphWnd graph_wnd_sensor1D;
class YYY: 
{
void YYY::Init(){
graph_wnd_sensor1D.ShowWindow(SW_SHOW);
}
....various things....
}

Here, in init, the app crashes because the graph_wnd_sensor1D has not the same information as the previous one.

In file 2.cpp I want to use graph_wnd_sensor1D. But visual yields

CMyTabCtrl.obj : error LNK2001: external symbol unresolved "class CGraphWnd graph_wnd_sensor1D"
  • So the idea is to get graph_wnd_sensor1D to be a global variable which is declared in file1 ! How could I solve this ? *

Solution

  • You only declared, but not defined the variable. Add a definition in a single implementation file.

    file 2.h

    extern CGraphWnd graph_wnd_sensor1D; // declarations
    

    file 2.cpp

    CGraphWnd graph_wnd_sensor1D; // definition