News:

When registered with our forums, feel free to send a "here I am" post here to differ human beings from SPAM bots.

Main Menu

Exporting Functions of a DLL

Started by sk0r, July 05, 2009, 11:50:03 AM

Previous topic - Next topic

sk0r

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

cacb

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


#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


#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.