Aug 31, 2009 at 1:32pm UTC
Hi,
I'm trying to create a static lib.
This lib needs to read/write to a db, but the library has no information about db (type, pass, etc).
The idea is to create an interface that the library client will fill to provide to the library DB stuff.
------------------------- class DBProvider ------------------------------------
#include "IDBResult.h"
using std::string;
class IDBProvider{
public:
virtual IDBResult * executeSQLCommand(string command);
};
------------------------- class IDBResult --------------------------------------
class IDBResult{
public:
/**
* Retrieves the value of the designated column in the current row of this object
* as a long.
*/
virtual long getLong(string columnLabel);
/**
* Retrieves the value of the designated column in the current row of this object
* as a int.
*/
virtual int getInt(string columnLabel);
};
----------------------------- staticLibImplementation.h ----------------------
#include #include "IDBProvider.h"
bool doLibraryStuff(long *result, IDBProvider db);
----------------------------- staticLibImplementation.cpp ----------------------
bool doLibraryStuff(long *result, IDBProvider db){
db.executeSQLCommand("A_QUERY_TO_DB");
}
I'm creating the library as follows :
g++ -Wall -g -c -o libDB.o staticLibImplementation.cpp
ar rcs libDB.a libDB.o
It compiles ok, but if I do a "nm libDB.a" I can see that:
U _ZN11IDBProvider17executeSQLCommandESs
So IDBProvider::executeSQLCommand is undefined.
Then , if I try to compile a test that uses libDB.a, at linking phase complains about unreferenced symbols.
The question is: Can I create different interfaces like this and use it in a static lib? I'm new in c++ (I come from java) and I don't really don't know about this. How I should compile it?
Thanks in advance.
Aug 31, 2009 at 1:53pm UTC
Where have you implemented executeSQLCommand?
Aug 31, 2009 at 2:22pm UTC
I thought , I could create just the interface, and the implementation is writted by client that wanted to use the lib.
The answer then is nowere...
Can I change :
virtual IDBResult * executeSQLCommand(string command);
for
virtual IDBResult * executeSQLCommand(string command){}
Doing this "nm libDB.a" shows me that the symbol is referenced but with W (weak?)
Aug 31, 2009 at 2:26pm UTC
Yes, you can do that. Or you can declare the function pure virtual.
Aug 31, 2009 at 2:30pm UTC
I tried to declare it pure virtual, but I had problems compiling. The compiler said that is not possible to use an abstract class in:
bool doLibraryStuff(long *result, IDBProvider db);
that is one of the public methods of the lib.
thanks for all.
Aug 31, 2009 at 6:34pm UTC
If you take the IDBProvider object by reference (or pointer) that might clear that up.