我生成了一个包含 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 示例.cpp
mysql_config --cflags
g++ -shared -Wl,-soname,libsample.so -o libsample.so sample.o
mysql_config --libs
现在生成了 libsample.so 并将其移至 /usr/lib 现在我创建了一个小 cpp 文件,该文件在同一目录中使用此共享库。 使用sample.cpp
#include "sample.h"
using namespace std;
int main()
{
MysqlInstance* object=new MySQlInstance("root","toor","/lib/socket",3306);
}
用过的:
- g++ -c usesample.cpp -lsample
它给了我这个错误:
错误:未在此范围内声明“MysqlInstance”错误:未在此范围内声明对象
谢谢