Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: sk0r on July 05, 2009, 11:50:03 am

Title: Exporting Functions of a DLL
Post by: sk0r 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
Title: Re: Exporting Functions of a DLL
Post by: cacb 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

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.