June 21, 2011

Thomas Kennedy Thomas Kennedy
Lab Rat
66 posts

CReating a DLL whcih can export ‘n’ number of functions for a client application

 

Helo All,

I would like to develop a .dll with Qt which can be able to export some ‘c’ functions to client application. Can experts guide me on this ?

many thanks,

 Signature 

Never Ever Give Up

6 replies

June 21, 2011

Gerolf Gerolf
Area 51 Engineer
3210 posts

you have to sorround the functions with

  1. extern "C"
  2. {
  3.     // do your functions and export definitions here
  4. }

if you use MSVS tool chain. Don’t know whether it is also needed for others like gcc etc.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

June 21, 2011

ludde ludde
Ant Farmer
325 posts

The easiest way to set up a project for a shared library / DLL is to use the Qt Creator New Project… wizard (and select Other Project / C++ Library). That will give you a good starting point with import / export defines etc.

If you want to make it a pure C DLL, the interface obviously has to be pure C, no C++ classes as return values or parameters.

June 21, 2011

jim_kaiser jim_kaiser
Lab Rat
144 posts

Er.. this (”__declspec(dllexport)” and “__declspec(dllimport)”) is required too.. isn’t it?

MyDllDef.h

  1. #ifdef INMYLIB
  2. #define MYLIBDEF __declspec(dllexport)
  3. #else
  4. #define MYLIBDEF __declspec(dllimport)
  5. #endif

MyLib.h

  1. #include "MyDllDef.h"
  2.  
  3. MYLIBDEF void exported_fn();

MyApp.cpp

  1. #include <MyLib.h>
  2.  
  3. void runFn()
  4. {
  5. exported_fn();
  6. }

You might also need to use the extern C.. Look at this Stack Overflow [stackoverflow.com] post on it.

June 21, 2011

ludde ludde
Ant Farmer
325 posts

Yeah, that’s exactly what you get from the Other Project / C++ Library wizard. That’s what I meant by ‘import / export defines etc.’ (though I agree I could have been a bit more explicit).

June 21, 2011

jim_kaiser jim_kaiser
Lab Rat
144 posts

@ludde: Sorry about that, I hadn’t used the feature myself, coding in VS at work.. but you’re right the wizard does make it a lot simpler. Cheers :)

[Edit: @gerolf: Thanks for the info.. i noticed the macros in the wizard generated code.. ]

June 21, 2011

Gerolf Gerolf
Area 51 Engineer
3210 posts

But that’s only needed on windows for MSVC compiler, mingw does it in another way, so I suggest using Q_DECL_EXPORT / Q_DECL_IMPORT. They map to the correct, compiler specific code.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

 
  ‹‹ [SOLVED]display image through http url      [SOLVED] [QComboBox and QSqlRelationalDelegate] How to show conditional content in a QComboBox ››

You must log in to post a reply. Not a member yet? Register here!