Reputation: 2691
I've created example C++
project in Visual Studio of dll type. It contains header file SqlLtDb.h
:
using namespace std;
// This class is exported from the SqlLtDb.dll
class CSqlLtDb {
public:
CSqlLtDb(char *fileName);
~CSqlLtDb();
// TODO: add your methods here.
bool SQLLTDB_API open(char* filename);
vector<vector<string>> SQLLTDB_API query(char* query);
bool SQLLTDB_API exec(const char* query);
void SQLLTDB_API close();
int SQLLTDB_API getNameOfClass();
private:
sqlite3 *database;
};
extern "C" SQLLTDB_API CSqlLtDb* getInstanceCSblLtDb();
extern SQLLTDB_API int nSqlLtDb;
extern "C" SQLLTDB_API int fnSqlLtDb();
And in SqlLtDb.cpp
methods are implemented as follows (I'm showing only two implementation):
...
int SQLLTDB_API CSqlLtDb::getNameOfClass()
{
return 777;
}
extern "C" SQLLTDB_API CSqlLtDb* getInstanceCSblLtDb()
{
CSqlLtDb* instance = new CSqlLtDb("");
return instance;
}
SqlLtDb.def
file look like this:
LIBRARY "SqlLtDb"
EXPORTS
getInstanceCSblLtDb
open
query
exec
close
getNameOfClass
SqlLtDb.lib file is generated by LIB command, using above .def file. This is my SqlLtDb.dll file.
Now I want to include this file to my consoleApplication application. ConsoleApplication is in VS 2008. I've set:
Properties->Configuration Properties->Linker->Input->Additional Dependencies : SqlLtDb.lib;
Properties->Configuration Properties->Linker->General->Additional Library
Directories: E:\PM\SqlLtDb\Release;
Runtime Library is set as it was: Multi-threaded Debug DLL (/MDd) (I didn't change it).
I copied files: SqlLtDb.dll, SqlLtDb.lib, SqlLtDb.def, sqlite3.dll
into Debug folder where consoleApplication.exe
is generated. And I added SqlLtDb.h
file into folder where consoleApplication's
source files are stored.
Function main in consoleApplication
look like this:
#include "stdafx.h"
#include "SqlLtDb.h";
int _tmain(int argc, _TCHAR* argv[])
{
CSqlLtDb* mySqlClass = getInstanceCSblLtDb(); // here is ok, this method is
// exported rigth
mySqlClass->open(""); // here is error whit open method
return 0;
}
When I compile this code I get error:
Error 1 error LNK2019: unresolved external symbol "__declspec(dllimport) public:
bool __thiscall CSqlLtDb::open(char *)" (__imp_?open@CSqlLtDb@@QAE_NPAD@Z)
referenced in function _wmain consoleApplication.obj consoleApplication
Method getInstanceCSblLtDb
is exported successful, but the problem is with export methods from class. I won't to export all class, better is export pointer to class.
Thanks
Upvotes: 0
Views: 548
Reputation: 4887
You need to export the class in the DLL with __declspec(dllexport)
, and import it in the linking code with __declspec(dllimport)
. Example:
class SQLLTDB_API CSqlLtDb {
...
};
You don't need SQLLTDB_API for each member, only the class - the linker will generate exports for each method for you.
Upvotes: 1