Dll erstellen bzw. Dllexport

Alles rund um die Programmierung mit Qt
Antworten
Whitefurrows
Beiträge: 138
Registriert: 1. Mai 2006 19:50

Dll erstellen bzw. Dllexport

Beitrag von Whitefurrows »

Hi,

wenn ich eine DLL erstellen möchte, ist es nach meinem Wissen nicht ohne weiteres möglich eine Klasse zu exportieren um diese z.B. in C# einzubinden. Deshalb habe ich eine DLL erstellt, in der die entsprechenden Funktionen der Klasse aufgerufen werden können. Bestimmte includes wie z.B. <QApplication> und bestimmte Funktionen der Klasse werden in der DLL nicht benötigt bzw. gewünscht. Diese Dinge habe ich versucht mit unschönen defines herauszunehmen. Es wäre mir aber lieber wenn ich die Dateien (MyDll.h und MyDll.cpp) nicht benötige und nur aus den notwendigen includes und Funktionen mit defines meine dll aus der Klasse (MyDll.cpp) erstellen könnte. Kann mir jemand sagen wie das evtl. möglich ist?

Nachfolgend eine Beispiel wie die DLL zurzeit erstellt wird.

Application.pro

Code: Alles auswählen

DEFINES = COMPILE_MYDLL
DEF_FILE = MyDLL.def

# ... and the usually stuff
MyDll.h

Code: Alles auswählen

#ifndef MYDLL_H
#define MYDLL_H

using namespace std;

class MyClass;

#ifdef __cplusplus
extern "C" {  //C-Deklarationen für C++
#endif

	void anyLocalFunction(float fltLocal);
	__declspec( dllexport ) float _stdcall myDllFunction(float intDLL);

	MyClass *myClass;

#ifdef  __cplusplus
}
#endif

#endif
MyDll.cpp

Code: Alles auswählen

#include "MyDll.h"
#include "MyClass.h"

#define   STRICT //Type checking

//Funktionsnamen leserlich exportieren
#pragma comment(linker, "/DEF:MyDLL.def")

float anyLocalFunction(float fltLocal)
{
	fltLocal = 2*3.14*fltLocal;
}

float myDllFunction(float intDLL)
{
	float fltRadius = intDLL;
	float fltExtent = anyLocalFunction(fltRadius);
	
	return myClass->myClassExportFunction(fltExtent);
}
MyClass.h

Code: Alles auswählen

#ifndef MYCLASS_H
#define MYCLASS_H

#if !defined(COMPILE_MYDLL)
#include <QApplication>
#include <QMap>
#endif

#include <map>
#include <string>
using namespace std;

class MyClass{
public:
	MyClass();
	~MyClass();

	float myClassExportFunction(float fltExtent);
#if !defined(COMPILE_MYDLL)
	void myClassFunction2(map<float,string>& mapValues);
	void myClassFunction3(map<float,string>& mapValues);
	void myClassFunction4(map<float,string>& mapValues);
#endif

private:
	float myClassLocalFunction(float fltA);
	float myClassLocalFunction2(float fltB);
	float myClassLocalFunction3(float fltC);
};
#endif
MyClass.cpp

Code: Alles auswählen

#include "MyClass.h"
#if !defined(COMPILE_MYDLL)
#include "AnyClass.h"
#include "AnyClass.h"
#endif

MyClass::MyClass(){}
MyClass::~MyClass(){}

float MyClass::myClassExportFunction(float fltExtent)
{
	//do anything ...
	return fltExtent;
}

#if !defined(COMPILE_MYDLL)
void MyClass::myClassFunction2(map<float,string>& mapValues)
{
	//do anything ...
}
void MyClass::myClassFunction3(map<float,string>& mapValues)
{
	//do anything ...
}
void MyClass::myClassFunction4(map<float,string>& mapValues)
{
	//do anything ...
}
#endif
float MyClass::myClassLocalFunction(float fltA)
{
	//do anything ...
	return fltA;
}
float MyClass::myClassLocalFunction2(float fltB)
{
	//do anything ...
	return fltB;
}
float MyClass::myClassLocalFunction3(float fltC)
{
	//do anything ...
	return fltC;
}
Christian81
Beiträge: 7319
Registriert: 26. August 2004 14:11
Wohnort: Bremen
Kontaktdaten:

Beitrag von Christian81 »

Code: Alles auswählen

#ifndef MYDLL_STATIC
# ifdef COMPILE_MYDLL
#  define MYDLL_EXPORT Q_DECL_EXPORT
# else
#  define MYDLL_EXPORT Q_DECL_IMPORT
# endif
#endif
...
class MYDLL_EXPORT MyClass ...
Sollte helfen.
MfG Christian

'Funktioniert nicht' ist keine Fehlerbeschreibung
Antworten