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. Small problem with virtual function in DLL class...

Small problem with virtual function in DLL class...

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestionlearning
3 Posts 2 Posters 0 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.
  • V Offline
    V Offline
    vmaltsev
    wrote on last edited by
    #1

    Hi everyone, I have class A in DLL with virtual function in it, like this... class __declspec(dllexport) A { public: vitual void Foo() { some code; }; } And there is derived class B in the same DLL... class __declspec(dllexport) B : public A { public: vitual void Foo() { some code; }; } Now, when I compile it, everything's ok. But I also have a console app where I create instances of class B from DLL. Of course I link .lib file with it. But when I try to compile this app I have an error from linker. error LNK2001: unresolved external symbol "public: virtual void __thiscall B::Foo(void)" Does anybody have any ideas how to solve this matter? P.S. When I check EXPORTS from my DLL with dumpbin.exe, I can see both methods there. So, ?FooA@A@@ and ?FooA@B@@ are there. But linker's looking for ?Foo@B@@, without "A" at the end of the method name and obviously can't find it. Any idea what's wrong? Thanks in advance, va'Lery

    M 1 Reply Last reply
    0
    • V vmaltsev

      Hi everyone, I have class A in DLL with virtual function in it, like this... class __declspec(dllexport) A { public: vitual void Foo() { some code; }; } And there is derived class B in the same DLL... class __declspec(dllexport) B : public A { public: vitual void Foo() { some code; }; } Now, when I compile it, everything's ok. But I also have a console app where I create instances of class B from DLL. Of course I link .lib file with it. But when I try to compile this app I have an error from linker. error LNK2001: unresolved external symbol "public: virtual void __thiscall B::Foo(void)" Does anybody have any ideas how to solve this matter? P.S. When I check EXPORTS from my DLL with dumpbin.exe, I can see both methods there. So, ?FooA@A@@ and ?FooA@B@@ are there. But linker's looking for ?Foo@B@@, without "A" at the end of the method name and obviously can't find it. Any idea what's wrong? Thanks in advance, va'Lery

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #2

      This is, I admit, a guess. When included by the application, is it marked __declspec(dllimport)? You should normally write your header as follows:

      #ifdef BUILDING_DLL
      #define MYDLL_API __declspec(dllexport)
      #else
      #define MYDLL_API __declspec(dllimport)
      #endif // def BUILDING_DLL

      class MYDLL_API A { /* declarations omitted */ };
      class MYDLL_API B : public A { /* declarations omitted */ };

      and define BUILDING_DLL in the DLL project. I'm not sure how well __declspec(dllexport) interacts with in-line member function definitions.

      V 1 Reply Last reply
      0
      • M Mike Dimmick

        This is, I admit, a guess. When included by the application, is it marked __declspec(dllimport)? You should normally write your header as follows:

        #ifdef BUILDING_DLL
        #define MYDLL_API __declspec(dllexport)
        #else
        #define MYDLL_API __declspec(dllimport)
        #endif // def BUILDING_DLL

        class MYDLL_API A { /* declarations omitted */ };
        class MYDLL_API B : public A { /* declarations omitted */ };

        and define BUILDING_DLL in the DLL project. I'm not sure how well __declspec(dllexport) interacts with in-line member function definitions.

        V Offline
        V Offline
        vmaltsev
        wrote on last edited by
        #3

        Thanks Mike, I surely use mechanics that you described, so in my app I have __declspec(dllimport). :) Concerning inline function, if I remove "virtual" when everything's fine. But that's not what I need. I have to use virtual function there. va'Lery

        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