locked
DLL using singleton class from another DLL RRS feed

  • Question

  • I have a link problem:

    error LNK2001: unresolved external symbol "public: static class a a::_a"

    I have one DLL project with this class

    file a.h

    #pragma once

    class AFX_EXT_CLASS a
    {
    protected:
        static a _a;

        a(void);
        virtual ~a(void);
    public:
        static a * getSingleton() { return &_a; }

        void startup();

    };

    file a.cpp

    #include "StdAfx.h"
    #include "a.h"

    a a::_a;

    a::a(void)
    {
    }

    a::~a(void)
    {
    }

    void a::startup() {   
    }

    this project create the dll correctly.

    The second DLL project has this code:

    file b.h

    #pragma once

    class AFX_EXT_CLASS b
    {
    public:
        b(void);
        virtual ~b(void);
    };

    file b.cpp

    #include "StdAfx.h"
    #include "b.h"
    #include "a.h"

    b::b(void)
    {
    }

    b::~b(void)
    {
    }

    I have the link error 2001.

    Why, does anyone has any idea. If I use the DLL with singleton class in a project that create an application the link error doesnt appear...why.

    Wednesday, May 12, 2010 9:58 PM

Answers

  • static methods are not part of the class.

    Only the scope is limited to the class.

    You could try to export the static variable globally in the .cpp file.


    «_Superman_»
    Microsoft MVP (Visual C++)
    • Proposed as answer by Yi Feng Li Wednesday, May 19, 2010 2:38 AM
    • Marked as answer by Yi Feng Li Wednesday, May 19, 2010 2:39 AM
    Wednesday, May 12, 2010 10:40 PM
  • In general, it is actually a bad idea to expose classes across a DLL boundary. It forces programmers to use identical compiler versions on both the DLL and any client code, and even use the same compiler switches, in order to ensure that the implementation of the class layout is identical on both sides. If it's not too late, change your design so that the entry points to your DLL are basic functions that pass only POD parameters (plain-ole-data). Specifically, that means not passing class references or STL containers.

     

    • Marked as answer by Yi Feng Li Wednesday, May 19, 2010 2:39 AM
    Thursday, May 13, 2010 12:55 AM

All replies

  • static methods are not part of the class.

    Only the scope is limited to the class.

    You could try to export the static variable globally in the .cpp file.


    «_Superman_»
    Microsoft MVP (Visual C++)
    • Proposed as answer by Yi Feng Li Wednesday, May 19, 2010 2:38 AM
    • Marked as answer by Yi Feng Li Wednesday, May 19, 2010 2:39 AM
    Wednesday, May 12, 2010 10:40 PM
  • In general, it is actually a bad idea to expose classes across a DLL boundary. It forces programmers to use identical compiler versions on both the DLL and any client code, and even use the same compiler switches, in order to ensure that the implementation of the class layout is identical on both sides. If it's not too late, change your design so that the entry points to your DLL are basic functions that pass only POD parameters (plain-ole-data). Specifically, that means not passing class references or STL containers.

     

    • Marked as answer by Yi Feng Li Wednesday, May 19, 2010 2:39 AM
    Thursday, May 13, 2010 12:55 AM