0

I have a package of C functions and I need to create DLL library from it, that can be used in C++ program. I haven't done any library before, so I am total beginner in that. I am working in Qt Creator.

My first try was to create it according to the manual Creating shared libraries, so I added these two lines to my project file:

TEMPLATE = lib
DEFINES += MYLIB_LIBRARY

Then I created mylib.h file

#ifndef MYLIB_H
#define MYLIB_H

#include "mylib_global.h"
#include "functions1.h"
#include "functions2.h"
#include "functions3.h"

class MYLIBSHARED_EXPORT Mylib
{

public:
    Mylib(){};
};

#endif // MYLIB_H

Finally I added mylib_global.h:

#ifndef MYLIB_GLOBAL_H
#define MYLIB_GLOBAL_H

#include <QtCore/qglobal.h>

#if defined(MYLIB_LIBRARY)
#  define MYLIBSHARED_EXPORT Q_DECL_EXPORT
#else
#  define MYLIBSHARED_EXPORT Q_DECL_IMPORT
#endif

#endif // MYLIB_GLOBAL_H

To make functions usable in C++ I used these lines for each function in library

#ifdef __cplusplus
extern "C"{
#endif

void foo();

#ifdef __cplusplus
}
#endif

When compiling with MSVC2012 everything seems ok and I got some .dll file. But then I sent library to someone, who wanted to use it in Borland C++. He told me that I have to compile it with some DEF file to tell to VS compiler right names and with __stdcall instead of __cdecl. But I have no idea how to do it in Qt. Any explanation and help would be really appreciated. Thanks

P.S. I looked at posts Using VS dll in old Borland and Import VS dll in C Builder, but they did not help me to understand the problem.

Community
  • 1
  • 1
TinF
  • 89
  • 7
  • You probably will have a CRT conflict (multiple independent heaps and incompatible standard libraries) even if you get past the naming. I would tell the friend it is time to use a modern compiler. – drescherjm Oct 23 '14 at 12:41

1 Answers1

1

If you want to export the foo function, you need to tell the linker somehow. What you were suggested is to use a .def file, which is quite easy.

Just create a file like exports.def in your project directory, and write in it something like:

EXPORTS foo

Then go to your library project settings -> Linker -> Input -> Module Definition File

and fill in your .def file name

Photon
  • 3,182
  • 1
  • 15
  • 16
  • Thanks, I'll try this, it seems easy. But I am still not sure how to do change __cdecl to __stdcall problem. Should I remove the mylib.h and mylib_global.h files and just write __stdcall to each function? Or is it so the DEF file is enough and I do not need to write anything specific to my function declarations? – TinF Oct 23 '14 at 14:51
  • I'm not sure. Your safest bet is obviously to put __stdcall before each function. Perhaps there's a global compiler setting for this. – Photon Oct 23 '14 at 17:45