I 생성 된 MySQL의 C API 함수를 래핑하는 공유 라이브러리입니다. 그것은이는 C++ "이 범위 오류에 선언되지 않은
using namespace std;
class MysqlInstance
{
protected:
string user;
string password;
string socket;
int port;
public:
MySqlInstance(string,string,string,port);
int connectToDB();
}
같이는 sample.h sample.cpp 파일이 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
}
에서
는 사용 :
그램 ++ -fPIC -c sample.cpp
mysql_config --cflags
그램 ++ -shared -Wl, -soname, libsample.so -o libsample.so sample.o
mysql_config --libs
libsample.so 이제 생성되고, I는 지금은/usr/LIB 그것을 이동 동일한 디렉토리에서이 공유 라이브러리를 사용하는 작은 cpp 파일을 만들었습니다. 사용 usesample.cpp
#include "sample.h"
using namespace std;
int main()
{
MysqlInstance* object=new MySQlInstance("root","toor","/lib/socket",3306);
}
:
- 그램 ++ -c usesample.cpp -lsample 그것은 나에게 오류를주고있다
:
error: âMysqlInstanceâ was not declared in this scope error: object was not declared in this scope
감사합니다
질문을 게시 할 때 복사 - 붙여 넣기 오류가 발생했는지 모르겠지만 MysqlInstance 클래스에 두 개의 port 변수가 있습니다. –
Yeah..thats a typo .. 여기의 문제는 MySQL C API 없이도 이런 일을 할 때 오류가 발생합니다. – Sreekar