DLL linkage error of static method

DLL linkage error of static method

Post by mara » Sun, 25 May 2003 00:14:52



Is there anything special that needs to be done when exporting a class
containing a static function and then importing it ?

I have a DLL (foo.lib/foo.dll) -

foo.h contents
-------------------------------------------------------
#ifdef DLLTEST_EXPORTS
#define EXPORT _declspec(dllexport)
#else
#define EXPORT _declspec(dllimport)
#endif

class  EXPORT XYZ
{
public:
        static void HELLO();
        XYZ();
private:
        int d_;

Quote:};

foo.cpp contents
----------------------------------------------------
#include "foo.h"
void XYZ::HELLO() {}
XYZ::XYZ() {}

In my app, I have the following code:
----------------------------------------
XYZ xyz;       // line 1
xyz::HELLO();  // line 2

Line 1 links fine.
Line 2 does NOT. Error I get -

testApp error LNK2019: unresolved external symbol
"__declspec(dllimport) public: static void __cdecl XYZ::HELLO(void)"


Using dumpbin on foo.lib -

Quote:>dumpbin /EXPORTS foo.lib | grep XYZ



__thiscall XYZ::o
perator=(class XYZ const &))

XYZ::HELLO(vo
id))

It appears that the static function HELLO() is also defined, in
addition to the ctor() and operator=().

So, what am I doing wrong when linking or importing against foo.lib ?

Please help.

MR

 
 
 

DLL linkage error of static method

Post by Ed Astl » Sun, 25 May 2003 00:44:10


Are you mixing call types (stdcall/cdecl etc) in the 2 projects (Code
Generation/Calling Convention project options) ?

Your dumpbin output shows

static void __stdcall XYZ::HELLO(void)

but the linker error shows it is expecting

static void __cdecl XYZ::HELLO(void)

If no call type is explicitly given for a function in the .h the compiler
assumes the setting in the build options.  Looks like you built the dll
using stdcall but built testApp using cdecl.  Either make both projects the
same or explicitly put __stdcall/__cdecl in front of the function names in
the .h.

I personally don't export an entire class as it exports everything - even
private functions that you might not actually want in the public export
records table.  Instead I do it selectively like so

class  XYZ
{ public:
  static EXPORT void HELLO();    // you will see it in the exports table
  EXPORT XYZ();    // you will see it in the exports table
  private:
  void my_secret_routine();    // you will NOT see it in the exports table
  int d_;

Quote:};

Generally you only need to export c++ public functions so you can actually
call the code in the dll but also c++ protected functions so derived classes
in one dll can call code from base classes in another dll (ie the bases
classes need to export their protected stuff so classes you write that
inherit from them can call these things).  You never need to export private
functions, unless you are using c++ friendship to gain access to these
normally private routines.

Regards,
Ed


> Is there anything special that needs to be done when exporting a class
> containing a static function and then importing it ?

> I have a DLL (foo.lib/foo.dll) -

> foo.h contents
> -------------------------------------------------------
> #ifdef DLLTEST_EXPORTS
> #define EXPORT _declspec(dllexport)
> #else
> #define EXPORT _declspec(dllimport)
> #endif

> class  EXPORT XYZ
> {
> public:
> static void HELLO();
> XYZ();
> private:
> int d_;
> };

> foo.cpp contents
> ----------------------------------------------------
> #include "foo.h"
> void XYZ::HELLO() {}
> XYZ::XYZ() {}

> In my app, I have the following code:
> ----------------------------------------
> XYZ xyz;       // line 1
> xyz::HELLO();  // line 2

> Line 1 links fine.
> Line 2 does NOT. Error I get -

> testApp error LNK2019: unresolved external symbol
> "__declspec(dllimport) public: static void __cdecl XYZ::HELLO(void)"


> Using dumpbin on foo.lib -
> >dumpbin /EXPORTS foo.lib | grep XYZ



> __thiscall XYZ::o
> perator=(class XYZ const &))

> XYZ::HELLO(vo
> id))

> It appears that the static function HELLO() is also defined, in
> addition to the ctor() and operator=().

> So, what am I doing wrong when linking or importing against foo.lib ?

> Please help.

> MR


 
 
 

DLL linkage error of static method

Post by <Alberto> » Thu, 12 Jun 2003 07:12:53


Just try

XYZ::HELLO();

instead of

xyz::HELLO();

Regards,
Alberto


> Is there anything special that needs to be done when exporting a class
> containing a static function and then importing it ?

> I have a DLL (foo.lib/foo.dll) -

> foo.h contents
> -------------------------------------------------------
> #ifdef DLLTEST_EXPORTS
> #define EXPORT _declspec(dllexport)
> #else
> #define EXPORT _declspec(dllimport)
> #endif

> class  EXPORT XYZ
> {
> public:
> static void HELLO();
> XYZ();
> private:
> int d_;
> };

> foo.cpp contents
> ----------------------------------------------------
> #include "foo.h"
> void XYZ::HELLO() {}
> XYZ::XYZ() {}

> In my app, I have the following code:
> ----------------------------------------
> XYZ xyz;       // line 1
> xyz::HELLO();  // line 2

> Line 1 links fine.
> Line 2 does NOT. Error I get -

> testApp error LNK2019: unresolved external symbol
> "__declspec(dllimport) public: static void __cdecl XYZ::HELLO(void)"


> Using dumpbin on foo.lib -
> >dumpbin /EXPORTS foo.lib | grep XYZ



> __thiscall XYZ::o
> perator=(class XYZ const &))

> XYZ::HELLO(vo
> id))

> It appears that the static function HELLO() is also defined, in
> addition to the ctor() and operator=().

> So, what am I doing wrong when linking or importing against foo.lib ?

> Please help.

> MR

 
 
 

1. Normal dll's with static MFC linkage

I am developping an dll which needs to link MFC statically.
The dll can be called in two ways. Either by an system dll
or by my own application.You might agree, that it is the best
way to link MFC dll statically to ensure version compliance of
both, my app and my dll.

I walked through several documentations and found out, that
there is no possibility to implement classes which can be exported.
Is this true or am I wrong? The thing is, that I have problems -
which indeed have nothing to do with exporting classes, but anyway -
passing CString objects between my app and the dll, due to C-style
conventions. If I am wrong or anybody has an idea how to solve
this, please let me know.

2. modem connection

3. static method causes unresolved external error.

4. AutoDESK's AutoSKETCH Features?

5. Static Method accessing private methods

6. Set WIDGET_LIST value keeping TOP position fixed

7. What is the difference between internal linkage and external linkage?

8. rotating tables etc

9. Help: Compiling .cpp including pcap--C++ linkage conflicts with new declaration with C linkage

10. static linkage?

11. How to define (not declare) a static constant with external linkage?

12. Solving the member function static linkage problem, how?

13. CC static internal linkage is changed to global after "-g0" - debug turned on