Search code examples
c++visual-c++64-bitlnk2001

LNK2001: unresolved external symbol when building for x64 platform


The project builds on Win32 platform, but not on x64.

Full error message: dllentry.obj : error LNK2001: unresolved external symbol "class CFactoryTemplate * g_Templates" (?g_Templates@@3PAVCFactoryTemplate@@A)

The dllentry.cpp compiles on both platforms. It contains the external declarations:

extern CFactoryTemplate g_Templates[];
extern int g_cTemplates;

g_Templates[] is then used in two functions:

__control_entrypoint(DllExport) STDAPI DllGetClassObject(__in REFCLSID rClsID,
    __in REFIID riid, __deref_out void **pv)
{
    ...
    for (int i = 0; i < g_cTemplates; i++)
    {
        const CFactoryTemplate * pT = &g_Templates[i];
    }
}

and

DllInitClasses(BOOL bLoading)
{
    ...
    for (int i = 0; i < g_cTemplates; i++)
    {
        const CFactoryTemplate * pT = &g_Templates[i];
    }
}

I checked all the libraries in the project settings and all seems to be OK, the 64 bit versions are used. What should I do to make the project build for x64 platform?


Solution

  • You stated in a comment to an earlier answer

    The other, valid definition, is in myClass.cpp (main class of my project) 'CFactoryTemplate* g_Templates=0;' followed by 'int g_cTemplates=0;' at the uppermost level in the file, just after the includes.

    Your definition of of g_Templates is inconsistent with its declaration.

    The declaration is

    extern CFactoryTemplate g_Templates[];
    

    This declares g_Templates as an array of CFactoryTemplate (of unknown size). But your definition of

    CFactoryTemplates* g_Templates = 0;
    

    defines it as a pointer to a CFactoryTemplate, which is not the same thing.

    Therefore, you failed to provide a definition of g_Templates that matches the declaration, and the error message is correct.

    You need to get the declaration and definition to agree. One way is to write

    CFactoryTemplates g_Templates[1];
    

    to create a definition that is consistent with the declaration. (Note that zero-length arrays are disallowed in C++, so instead we create an array of length 1 and ignore the element in it.)

    Another is to change the declaration to

    extern CFactoryTemplates* g_Templates;