0

I'm having an unresolved external symbol error for two variables defined in a namespace. This is the code I have in my header and source file defining and declaring my variables.

header file

#include "States.h"

namespace acro {    
    namespace states {
        extern States_t feedback;
        extern States_t down;
    }
}

source file

namespace acro {    
    namespace states {
        States_t feedback;
        States_t down;
    }
}

In my main file I'm calling this.

printf("Iteration: %lld\n", acro::states::feedback.i);

But I receive this error.

Error   LNK2001 unresolved external symbol "class States_t acro::states::feedback" (?feedback@states@acro@@3VStates_t@@A)   simple_acrophobe    C:\Users\...\main.obj   

States_t is class defined in the "Stated.h" an "States.cpp" file which I know works and is properly included in the project. I tested it by declaring an object in the main function. I'm quite certain I've included my header and source files properly. I created them using the Visual Studio UI. There is nothing else in these files except for a header guard and some comments with documentation.

I've ran out of ideas on what could be going wrong here.

Aaron de Windt
  • 16,794
  • 13
  • 47
  • 62
  • extern States_t feedback; should be defined as extern States_t states_feedback; – Sumeet Jan 12 '16 at 13:31
  • move your `#include "States.h"` to cpp, if your *header file* above is `Stated.h`! – John_West Jan 12 '16 at 13:33
  • @Sumeet The last thing I tried was to move the variables out of the states namespace. I forgot to fix the name in the cpp before putting it back in the states namespace and posting it here. That doesn't fix the error though. – Aaron de Windt Jan 12 '16 at 13:34
  • Are you sure you are linking all the obj files? Since this is VS, also make sure that the "stdafx.h" is the first line of code (assuming you are using precompiled headers). – Niall Jan 12 '16 at 13:36
  • @John_West I tried moving the #include "States.h" to the cpp and adding "class States_t;" to the top of the header file. Didn't work. I still got the same error. – Aaron de Windt Jan 12 '16 at 13:39
  • @Niall I'm not using precompiled headers. And as far as I know all the files are part of my project. They appear in my solution explorer, is there something else I could check? – Aaron de Windt Jan 12 '16 at 13:42
  • Double check that all the source files are "included" in the build. In VS you can exclude files on an individual basis. Also check for preprocessor directives that exclude that code from the build. – Niall Jan 12 '16 at 13:44
  • @Niall So I rebuild the project and the source file was indeed not being build. So I removed the file from the project and included it again and now it works. Thanks. – Aaron de Windt Jan 12 '16 at 13:51
  • @AarondeWindt. Good news. The is a canonical QA on these linker errors. This case is covered in one of the answers. – Niall Jan 12 '16 at 13:52

0 Answers0