MySQL C API 関数をラップする共有ライブラリを生成しました。このようなsample.hおよびsample.cppファイルがあります
using namespace std;
class MysqlInstance
{
protected:
string user;
string password;
string socket;
int port;
public:
MySqlInstance(string,string,string,port);
int connectToDB();
}
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
}
使用済み:
g++ -fPIC -c sample.cpp
mysql_config --cflags
g++ -shared -Wl,-soname,libsample.so -o libsample.so sample.o
mysql_config --libs
libsample.so が生成され、これを /usr/lib に移動しました。この共有ライブラリを使用する小さな cpp ファイルを同じディレクトリに作成しました。 usesample.cpp
#include "sample.h"
using namespace std;
int main()
{
MysqlInstance* object=new MySQlInstance("root","toor","/lib/socket",3306);
}
使用済み:
- g++ -c usesample.cpp -lsample
それは私にこのエラーを与えています:
エラー: 「MysqlInstance」はこのスコープで宣言されていません エラー: オブジェクトはこのスコープで宣言されていません
ありがとう