3

I've declared the static const array in the header and then implemented it in the cpp file but I cannot figure out what is going on.

Subsys.h:

#ifndef _SUBSYS_H
#define _SUBSYS_H

namespace Engines
{

    namespace Particles
    {

        class SubSys : public ISubSys
        {

        private:
            static const int _M[ 3 ];
            ...
            //rest of class
        };

    }

}

#endif

Subsys.cpp:

#include "Subsys.h"

namespace Engines
{

    namespace Particles
    {

        const int SubSys::_M[ 3 ] = 
        {
            0x80,
            0x7f,
            0x01
        };

    }

}

error LNK2001: unresolved external symbol "private static int const * const Engines::Particles::SubSys::_M" (?_M@SubSys@Particles@Engines@@0QBIB)

If I implement the array in the header outside of the class I don't get the LNK2001 error in the application using the static library. I do get LNK4006 when compiling the static library though (i.e. symbol added more than once).

I've also removed the namespaces in the .cpp file and used the full Engines::Particles::SubSys::_M name. The same problem occurs.

Hans
  • 55
  • 7

1 Answers1

1

Als thanks for all your help but it didn't quite answer my question, which was to use a cpp file within the library itself instead of moving the initialization into a cpp file in the application calling the library.

The way I resolved it, using VC++, was to use another header file in the library. That header file contains all the static initializers needed. Then I just #include that header file from the library in the application and it works.

The full design is:

Subsys.h

#ifndef _SUBSYS_H
#define _SUBSYS_H

namespace Engines
{

    namespace Particles
    {

        class SubSys : public ISubSys
        {

        private:
            static const int _M[ 3 ];
            ...
            //rest of class
        };

    }

}

#endif

SubsysParticlesInit.h

#ifndef _SUBSYS_PARTICLES_INIT_H
#define

#include "Subsys.h"

namespace Engines
{
    namespace Particles
    {
        const int SubSys::_M[ 3 ] = 
        {
            0x80,
            0x7f,
            0x01
        };
    }
}

#endif

Application.cpp

#include "Subsys.h"
#include "SubsysParticlesInit.h"

    int main() { ... }

It requires calling 2 headers from the application instead of 1 but at least all the code is contained in the library.

Hans
  • 55
  • 7