Reputation: 13
I'm working on oop c++ with code::Blocks.
These are my first steps in oop because I program in C for microprocessors.
I'm having trouble linking a dll.
My the main from the dll project is:
#include "main.h"
#include "xclass.h"
// a sample exported function
void DLL_EXPORT SomeFunction(const LPCSTR sometext)
{
MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break;
case DLL_PROCESS_DETACH:
// detach from process
break;
case DLL_THREAD_ATTACH:
// attach to thread
break;
case DLL_THREAD_DETACH:
// detach from thread
break;
}
return TRUE; // succesful
}
This is the header:
#ifndef __MAIN_H__
#define __MAIN_H__
#include <windows.h>
#include "xclass.h"
/* To use this exported function of dll, include this header
* in your project.
*/
#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C"
{
#endif
void DLL_EXPORT SomeFunction(const LPCSTR sometext);
#ifdef __cplusplus
}
#endif
#endif // __MAIN_H__
Basic stuff as you can see.
The problem is that I am including the class xclass with th main:
#include "xclass.h"
xclass::xclass()
{
//ctor
}
xclass::~xclass()
{
//dtor
}
and header
#ifndef XCLASS_H
#define XCLASS_H
class xclass
{
public:
xclass();
virtual ~xclass();
unsigned int GetCounter() { return m_Counter; }
void SetCounter(unsigned int val) { m_Counter = val; }
protected:
private:
unsigned int m_Counter;
};
#endif // XCLASS_H
I was able to link and use the dll in other project. A can even use the function in the DLL SomeFunction("teste x");
but I can not access and us the class:
#include <iostream>
#include "main.h"
//#include "../cvWrapper/main.h"
using namespace std;
int main()
{
xclass ClassInDll;// not working
SomeFunction("teste x"); //using the function in dll
printf("%d\n", 1);
return 0;
}
The build error is:
||=== testDLL, Debug ===| obj\Debug\main.o||In function
main':| C:\Users\SoftVision\Desktop\PrinterCode\DLL_test\testDLL\main.cpp|9|undefined reference to
xclass::xclass()'| C:\Users\SoftVision\Desktop\PrinterCode\DLL_test\testDLL\main.cpp|14|undefined reference toxclass::~xclass()'| C:\Users\SoftVision\Desktop\PrinterCode\DLL_test\testDLL\main.cpp|14|undefined reference to
xclass::~xclass()'| ||=== Build finished: 3 errors, 0 warnings ===|
Thank for the help...
Upvotes: 0
Views: 2969
Reputation: 4164
Actually you should export the class:
class DLL_EXPORT xclass
{
public:
xclass();
virtual ~xclass();
unsigned int GetCounter() { return m_Counter; }
void SetCounter(unsigned int val) { m_Counter = val; }
protected:
private:
unsigned int m_Counter;
};
Be careful when you export a class which is not a pure virtual class because you may meet some problems with a memory alignment. This happens because of different RTL versions in a different compilers. Instead export a pure virtual interface of you class.
class DLL_EXPORT IXClass
{
public:
IXClass();
virtual ~IXClass();
virtual unsigned int GetCounter()=0;
virtual void SetCounter(unsigned int val) =0;
};
Also avoid macros... Good luck :).
Upvotes: 1
Reputation: 41812
You need to export the class too:
class DLL_EXPORT xclass {
//...
You might need to rearrange your headers a little - e.g. put the #define
for DLL_EXPORT
somewhere that can be included in both 'main.h' and 'xclass.h'.
http://www.codeproject.com/Articles/28969/HowTo-Export-C-classes-from-a-DLL
Upvotes: 0