Pranav
Pranav

Reputation: 1

Using extern variable in header and using across multiple projects

I have 4 projects in a single solution. I want to use variables defined as extern in one header file globally. The scenario is like this:

headerfile.h and function.cpp in project1 mainfile.cpp in project 2

headerfile.h containts:

extern int nframes;

mainfile.cpp containt:

#include "headerfile.h"
int nframes=0;

function.cpp containt:

//use value of nframes

and I am trying to directly access the value of nframes in function.cpp. But there is a linker error in project 1 : unresolved external symbol "int nframes".

Now, when i define nframes in function.cpp as well, the builds are successful, but the value of nframes is reset to 0 when the control switches from mainfile.cpp to function.cpp.

Kindly help.

Upvotes: 0

Views: 982

Answers (1)

Anonymouse
Anonymouse

Reputation: 945

you need to include mainfile.cpp in each of your projects. The link error is just telling that it cannot find where the variable is defined. Suggest changing the name of mainfile.cpp to GlobalVariables.CPP it should make things clearer to you.

Upvotes: 0

Related Questions