linux環境下寫C++操作mysql(一)


/*****************
connect.cpp
g++ connect.cpp -o connect -I /usr/include/mysql/ -L /usr/lib/mysql/ -lmysqlclient
****************/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"mysql.h"


class CMysqlInterface
{
    public:
        CMysqlInterface();
        ~CMysqlInterface();
        
        void mysqlLibInit();
        void mysqlLibDestroy();
        int Connect();
        int Close();

    private:
        MYSQL *m_mysqlPtr;
};

int main()
{
    printf("version 1.1\n");
    int iRet = -1;
    CMysqlInterface MysqlObj;
    
    iRet = MysqlObj.Connect();
    if(0 == iRet)
    {
        printf("mysql_real_connect success\n");
    }
    else
    {
        printf("mysql_real_connect failed\n");
    }
    return 0;
}

CMysqlInterface::CMysqlInterface()
{
    printf("CMysqlInterface\n");
    m_mysqlPtr = NULL;
    m_mysqlPtr = mysql_init(NULL);
}

CMysqlInterface::~CMysqlInterface()
{
    Close();

}

int CMysqlInterface::Close()
{
    int iRet = 0;
    
    if(NULL != m_mysqlPtr)
    {
        mysql_close(m_mysqlPtr);
        m_mysqlPtr = NULL;
    }
    return iRet;
}

void CMysqlInterface::mysqlLibDestroy()
{
    mysql_library_end();
}

int CMysqlInterface::Connect()
{    
    printf("Connect\n");
    int iRet = -1;
    m_mysqlPtr = mysql_real_connect(m_mysqlPtr,"localhost","root","csql","child",0,NULL,0);
    if(m_mysqlPtr)
    {
        iRet = 0;
    }
    return iRet;
}

 

exbot@ubuntu:~/wangqinghe/MySql/20190621/01$ g++ connect.cpp -o connect -I /usr/include/mysql/ -L /usr/lib/mysql/ -Imysqlclient

/tmp/cceJyiND.o:在函數‘CMysqlInterface::CMysqlInterface()’中:

connect.cpp:(.text+0xaf):對‘mysql_init’未定義的引用

/tmp/cceJyiND.o:在函數‘CMysqlInterface::Close()’中:

connect.cpp:(.text+0x100):對‘mysql_close’未定義的引用

/tmp/cceJyiND.o:在函數‘CMysqlInterface::mysqlLibDestroy()’中:

connect.cpp:(.text+0x121):對‘mysql_server_end’未定義的引用

/tmp/cceJyiND.o:在函數‘CMysqlInterface::Connect()’中:

connect.cpp:(.text+0x17c):對‘mysql_real_connect’未定義的引用

collect2: error: ld returned 1 exit status 

這個問題是因為連接命令出了錯誤 后面跟的應該是 -l 而不是-L

 

輸出結果:

exbot@ubuntu:~/wangqinghe/MySql/20190621/02$ g++ connect.cpp -o connect -I /usr/include/mysql/ -L /usr/lib/mysql/ -lmysqlclient

exbot@ubuntu:~/wangqinghe/MySql/20190621/02$ ./connect

version 1.1

CMysqlInterface

Connect

mysql_real_connect success

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM