Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Whats reason of error: LNK2019 unresolved external symbol "__declspec(dllimport) public: __cdecl NS::A::A

Whats reason of error: LNK2019 unresolved external symbol "__declspec(dllimport) public: __cdecl NS::A::A

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
7 Posts 2 Posters 62 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    Oscar K
    wrote on last edited by
    #1

    //TestDll2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl NS::A::A(int (__cdecl*)(class std::basic_string,class std::allocator >))" (__imp_??0A@NS@@QEAA@P6AHV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z@Z) referenced in function main =====file TestDll2.cpp=====

    #include
    #include "..\\MyDll\\MyDll.h"

    using namespace std;
    using namespace NS;

    int Print(string str);
    int main()
    {
    NS::A* a = new NS::A(Print);
    NS::printDlgt("Hello World!");
    }

    int Print(string str)
    {
    cout << str << endl;
    return 0;
    }

    MyDll project files =====MyDll.cpp=====

    #include "pch.h"
    #include "MyDll.h"
    namespace NS
    {
    PrintDelegate printDlgt;
    A::A(PrintDelegate print_Dlgt)
    {
    NS::printDlgt = print_Dlgt;
    }
    }

    =====MyDll.h=====

    #include
    #ifndef MYDLL_H
    #define MYDLL_H

    //MYDLL_EXPORTS is set in C++\Preprocessor
    #ifdef MYDLL_EXPORTS
    #define MYDLL_API __declspec(dllexport)
    #else
    #define MYDLL_API __declspec(dllimport)
    #endif

    using namespace std;
    typedef int (*PrintDelegate)(string str);

    namespace NS
    {
    extern PrintDelegate printDlgt;
    class MYDLL_API A
    {
    public:
    A(PrintDelegate print_Dlgt);
    };
    }
    #endif

    L 1 Reply Last reply
    0
    • O Oscar K

      //TestDll2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl NS::A::A(int (__cdecl*)(class std::basic_string,class std::allocator >))" (__imp_??0A@NS@@QEAA@P6AHV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z@Z) referenced in function main =====file TestDll2.cpp=====

      #include
      #include "..\\MyDll\\MyDll.h"

      using namespace std;
      using namespace NS;

      int Print(string str);
      int main()
      {
      NS::A* a = new NS::A(Print);
      NS::printDlgt("Hello World!");
      }

      int Print(string str)
      {
      cout << str << endl;
      return 0;
      }

      MyDll project files =====MyDll.cpp=====

      #include "pch.h"
      #include "MyDll.h"
      namespace NS
      {
      PrintDelegate printDlgt;
      A::A(PrintDelegate print_Dlgt)
      {
      NS::printDlgt = print_Dlgt;
      }
      }

      =====MyDll.h=====

      #include
      #ifndef MYDLL_H
      #define MYDLL_H

      //MYDLL_EXPORTS is set in C++\Preprocessor
      #ifdef MYDLL_EXPORTS
      #define MYDLL_API __declspec(dllexport)
      #else
      #define MYDLL_API __declspec(dllimport)
      #endif

      using namespace std;
      typedef int (*PrintDelegate)(string str);

      namespace NS
      {
      extern PrintDelegate printDlgt;
      class MYDLL_API A
      {
      public:
      A(PrintDelegate print_Dlgt);
      };
      }
      #endif

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Oscar K. wrote:

      //MYDLL_EXPORTS is set in C++\Preprocessor

      What do you mean by that? MYDLL_EXPORTS should only be defined in MyDll.cpp, just before the #include "MyDll.h". You also need to ensure that you add MyDll.lib to the linker options in your build. [edit] On reflection I think maybe the definition of extern PrintDelegate printDlgt; is not correct. I think it should also by declared with MYDLL_API. I will try and test this later today. [/edit]

      O 1 Reply Last reply
      0
      • L Lost User

        Oscar K. wrote:

        //MYDLL_EXPORTS is set in C++\Preprocessor

        What do you mean by that? MYDLL_EXPORTS should only be defined in MyDll.cpp, just before the #include "MyDll.h". You also need to ensure that you add MyDll.lib to the linker options in your build. [edit] On reflection I think maybe the definition of extern PrintDelegate printDlgt; is not correct. I think it should also by declared with MYDLL_API. I will try and test this later today. [/edit]

        O Offline
        O Offline
        Oscar K
        wrote on last edited by
        #3

        #define MYDLL_EXPORTS is already set in Debug\Properties\C++\Preprocessor #define MYDLL_EXPORTS in MyDll.cpp invokes macro redefinition extern PrintDelegate printDlgt in MyDll.h is usual decision, that's how it's done

        L 1 Reply Last reply
        0
        • O Oscar K

          #define MYDLL_EXPORTS is already set in Debug\Properties\C++\Preprocessor #define MYDLL_EXPORTS in MyDll.cpp invokes macro redefinition extern PrintDelegate printDlgt in MyDll.h is usual decision, that's how it's done

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Oscar K. wrote:

          #define MYDLL_EXPORTS in MyDll.cpp invokes macro redefinition

          You should only have it in one place, and inside the dll implementation file (MyDll.cpp) is the better choice.

          Oscar K. wrote:

          extern PrintDelegate printDlgt in MyDll.h is usual decision, that's how it's done

          Correct, but that only makes it visible to the build of the DLL. When you build your test code the it is declared extern which the compiler accepts as it should be defined in another compilation unit, as part of the build of the main program. But when you try to link the program the linker cannot find where that item is actually defined because it only exists in the DLL. And since it has not been exported it is not listed in the lib or exp files.

          O 1 Reply Last reply
          0
          • L Lost User

            Oscar K. wrote:

            #define MYDLL_EXPORTS in MyDll.cpp invokes macro redefinition

            You should only have it in one place, and inside the dll implementation file (MyDll.cpp) is the better choice.

            Oscar K. wrote:

            extern PrintDelegate printDlgt in MyDll.h is usual decision, that's how it's done

            Correct, but that only makes it visible to the build of the DLL. When you build your test code the it is declared extern which the compiler accepts as it should be defined in another compilation unit, as part of the build of the main program. But when you try to link the program the linker cannot find where that item is actually defined because it only exists in the DLL. And since it has not been exported it is not listed in the lib or exp files.

            O Offline
            O Offline
            Oscar K
            wrote on last edited by
            #5

            I added extern PrintDelegate NS::printDlgt; to TestDll2.cpp, but I have got again error unresolved external. I see class A (MyDll.dll) in Dll viewer.

            L 1 Reply Last reply
            0
            • O Oscar K

              I added extern PrintDelegate NS::printDlgt; to TestDll2.cpp, but I have got again error unresolved external. I see class A (MyDll.dll) in Dll viewer.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              And you will continue to get that error until you create the object inside the namespace and use the MYDLL_API export prefix. As I have said more than once, you cannot use extern on an item that only exists in a DLL.

              O 1 Reply Last reply
              0
              • L Lost User

                And you will continue to get that error until you create the object inside the namespace and use the MYDLL_API export prefix. As I have said more than once, you cannot use extern on an item that only exists in a DLL.

                O Offline
                O Offline
                Oscar K
                wrote on last edited by
                #7

                I changed extern PrintDelegate printDlgt; to extern MYDLL_API PrintDelegate printDlgt; Now it works properly Thanks Richard

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups