Author Topic: Exporting Functions of a DLL  (Read 5150 times)

sk0r

  • Guest
Exporting Functions of a DLL
« on: July 05, 2009, 11:50:03 am »
Hello,

I have to export four functions of a DLL.
I declared them as extern "C" __declspec(dllexport) to
avoid name mangling and export them. But C::B saves
in the .DEF file always "FuncName@8" not just "FuncName".
I tried to avoid that by setting the .DEF file to "Read-Only",
but then the Id.exe crashes. What can I do to have
"FuncName" and not  "FuncName@8" ?

Help would be nice.

Regards,

sk0r

Offline cacb

  • Lives here!
  • ****
  • Posts: 547
Re: Exporting Functions of a DLL
« Reply #1 on: July 06, 2009, 12:06:39 am »
You need to say which OS and compiler you are using...

But this looks a lot like Windows and MS compiler?

If so, my advice would be
  • Forget the .DEF file, create a header file "MyDLL_config.h" instead. See below for what it should look like
  • In your DLL project, define a symbol MYDLL_EXPORTS
  • Include the "MyDLL_config.h" in the header files declaring exported functions/classes
  • All functions and classes "tagged" with MYDLL_IMPORT_EXPORT as shown below will be exported
  • For linux, the MYDLL_IMPORT_EXPORT symbol would be defined as blank (I think, but I have not tried.). I am guessing all functions and classes are exported from a linux .so

Code
#ifndef MYDLL_CONFIG_H
#define MYDLL_CONFIG_H

   #ifdef MYDLL_EXPORTS
         #define MYDLL_IMPORT_EXPORT __declspec(dllexport)
   #else
         #define MYDLL_IMPORT_EXPORT __declspec(dllimport)
   #endif

#endif


Example use in header file, exporting C++ functions and classes

Code
#ifndef MYDLL_H
#define MYDLL_H
#include "MyDLL_config.h"

// example function prototype of exported function
MYDLL_IMPORT_EXPORT int example_function(int i);

// example class declaration of exported class
class MYDLL_IMPORT_EXPORT example_class {
public:
     example_class();
     virtual  ~example_eclass();
   
     void do_something();
private:
     int m_whatever;
};

#endif

This technique ensures that the code is compiled with __declspec(dllexport) from within the MyDLL project, but any client code using the DLL would compile the header file using __declspec(dllimport). Obviously, the client projects must link with the export library (MyDll.lib).

Hope this helps.