0

newbie here! I recently started doing a small project in C++ but as soon as I try to compile it, I get a LNK2001 error due to my global variables file.

namespace {
    namespace Vars {
        extern int x;
        }
    }

As you can see I already tried fixing the error with an 'anonymous namespace' - a solution I found while googling the error, but it seems that none of the posts I saw so far are relatable to the error I'm getting.

In the .cpp the variables.h is included within, I get LNK2001 errors, but the other .cpp that has it included but not using it in any way does not.

The other .cpp would look like this:

#include "Variables.h"
#include "otherstuff.h"

void main(){
    if (Vars::x == 1){
        Stuff::OtherStuff->Run();
    }
}

The exact error given to me by the linker is:

error LNK2001: unresolved external symbol "int `anonymous namespace'::Vars::x" (?x@Vars@?A0x6c0990fe@@3HA)

Please feel free to criticize me, I'm new to C++ in general.

Nals
  • 1
  • 1
    The `main` function in C++ returns `int`. Always. – Thomas Matthews Jan 04 '17 at 00:15
  • Prefer not to use global variables, especially in multi-threading or multitasking environments. – Thomas Matthews Jan 04 '17 at 00:16
  • I fixed my issue by using a global struct that contained variables - and doing a shortcut to it in each file that I used it in. This resolved my issue, might not be the best way but it does the job for the moment :) – Nals Jan 04 '17 at 06:31

0 Answers0