0

I have defined a global variable in a c++ file. eg: bool _variable =1;

Now i use this variable in other c++ file using : extern bool _variable.

But i am getting the error error LNK2001: unresolved external symbol "bool _variable" (?_variable@@3_NA).

How can i resolve this error??

peoro
  • 25,562
  • 20
  • 98
  • 150
SPB
  • 4,040
  • 16
  • 49
  • 62

2 Answers2

4

It means the linker cannot find the definition of such variable.

  • Are you sure you wrote it in the same way anywhere?

  • Are you sure the c++ file containing the definition gets compiled and linked with the others when you're getting such error?

  • Are you sure the namespace where such variable is defined is the same?

peoro
  • 25,562
  • 20
  • 98
  • 150
  • there is no namespace when i declare it globally...and in other file i use extern bool _variable at the top, but after that functions are inside namespace base where i am trying to use this variable.....what should i do? – SPB Jan 20 '11 at 11:06
  • @SPB: `_variable` must always be declared in the same namespace, both when you define it, and when you declare it as external. If both declarations and definitions happen in the global namespace that's not the problem. – peoro Jan 20 '11 at 11:08
1

Maybe you forgot to include file with defined variable into the project (assuming you're using VS).

Vladimir
  • 1,781
  • 13
  • 12