I generated a shared library which wraps MySQL C API functions. It has a sample.h and sample.cpp files like this
using namespace std;
class MysqlInstance
{
protected:
string user;
string password;
string socket;
int port;
public:
MySqlInstance(string,string,string,port);
int connectToDB();
}
In sample.cpp
MySqlInstance::MySqlInstance(string user,string pass,string sock,int port)
{
this->port=port;
this->user=user;
this->password=pass;
this->socket=sock;
}
MySqlInstance::connectToDB()
{
//code to load libmysqlclient.so from /usr/lib64 and use mysql_init and mysql_real_connect
// functions to connect and "cout" that connection is successful
}
Used:
-
g++ -fPIC -c sample.cpp
mysql_config --cflags -
g++ -shared -Wl,-soname,libsample.so -o libsample.so sample.o
mysql_config --libs
Now libsample.so is generated and I moved it to /usr/lib
Now I created a small cpp file which uses this shared library in the same directory.
usesample.cpp
#include "sample.h"
using namespace std;
int main()
{
MysqlInstance* object=new MySQlInstance("root","toor","/lib/socket",3306);
}
Used:
- g++ -c usesample.cpp -lsample
It is giving me this error:
error: âMysqlInstanceâ was not declared in this scope
error: object was not declared in this scope
Thanks
You have a few errors. One, is the constructor declaration
You probably mean
Then, the definition, you have the type of
portwrong:Then, the class name
should be
Then, you are using
MySQlInstanceinmainbut your class isMySqlInstance.Remember, C++ is not case insensitive.
Finally, do not put
using namespace stdin a header file. In fact, do not put it anywhere.