C ++ MySQL连接器库运行时错误

时间:2014-09-27 19:10:08

标签: c++ mysql

我试图在C ++中使用示例MySQL连接器并运行它。我无法找到为什么这不会运行,希望你们能帮助我。

这是终端的日志

root@servah:~/c# g++ -o mysql $(mysql_config --cflags) mysql.cpp $(mysql_config --libs)
root@servah:~/c# ./mysql
./mysql: error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or directory
root@servah:~/c# ldd mysql
    linux-gate.so.1 (0xffffe000)
    libmysqlclient.so.16 => not found
    libz.so.1 => /usr/lib/libz.so.1 (0xb7798000)
    libcrypt.so.1 => /lib/libcrypt.so.1 (0xb7765000)
    libnsl.so.1 => /lib/libnsl.so.1 (0xb774a000)
    libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0xb7661000)
    libm.so.6 => /lib/libm.so.6 (0xb761f000)
    libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0xb7603000)
    libc.so.6 => /lib/libc.so.6 (0xb7478000)
    /lib/ld-linux.so.2 (0xb77ca000)
root@servah:~/c# find / -name libmysqlclient.so.16
/usr/lib/mysql/libmysqlclient.so.16
root@servah:~/c# ldd /usr/lib/mysql/libmysqlclient.so.16
    linux-gate.so.1 (0xffffe000)
    libcrypt.so.1 => /lib/libcrypt.so.1 (0xb7636000)
    libnsl.so.1 => /lib/libnsl.so.1 (0xb761b000)
    libm.so.6 => /lib/libm.so.6 (0xb75d9000)
    libz.so.1 => /usr/lib/libz.so.1 (0xb75c3000)
    libc.so.6 => /lib/libc.so.6 (0xb7437000)
    /lib/ld-linux.so.2 (0xb7702000)

mysql_config --cflags

的输出
-I/usr/include/mysql  -g -DUNIV_LINUX -DUNIV_LINUX

mysql_config的输出 - libs

-rdynamic -L/usr/lib/mysql -lmysqlclient -lz -lcrypt -lnsl -lm

最后,我的代码

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

#define USER "test"
#define PASSWORD "test"
#define SERVER "localhost"
#define DATABASE "test"

struct connection_details {
    char *server;
    char *user;
    char *password;
    char *database;
};

MYSQL* mysql_connect(struct connection_details mysql_details) {
    MYSQL *connection = mysql_init(NULL);

    if (!mysql_real_connect(connection, mysql_details.server,
            mysql_details.user, mysql_details.password, mysql_details.database,
            0, NULL, 0)) {
        printf("Conection error : %s\n", mysql_error(connection));
        exit(1);
    }
    return connection;
}

MYSQL_RES* mysql_query(MYSQL *connection, char *sql_query) {
    if (mysql_query(connection, sql_query)) {
    printf("MySQL query error : %s\n", mysql_error(connection));
    exit(1);
    }
    return mysql_use_result(connection);
}

int main() {
    MYSQL *conn;          // the connection
    MYSQL_RES *res;       // the results
    MYSQL_ROW row;        // the results row (line by line)

    struct connection_details mysqlD;
    mysqlD.server = (char *) SERVER;  // where the mysql database is
    mysqlD.user = (char *) USER;          // the root user of mysql
    mysqlD.password = (char *) PASSWORD; // the password of the root user in mysql
    mysqlD.database = (char *) DATABASE;  // the databse to pick

    // connect to the mysql database
    conn = mysql_connect(mysqlD);

    // assign the results return to the MYSQL_RES pointer
    char *query = (char *) "show tables";
    res = mysql_query(conn, query);

    printf("MySQL Tables in mysql database:\n");
    while ((row = mysql_fetch_row(res)) != NULL)
        printf("%s\n", row[0]);

    /* clean up the database result set */
    mysql_free_result(res);
    /* clean up the database link */
    mysql_close(conn);
    return 0;
}

1 个答案:

答案 0 :(得分:0)

解决方案

我通过

找到了问题
root@servah:~/c# ldconfig -v | grep mysql
ldconfig: Path `/lib' given more than once
ldconfig: Path `/usr/lib' given more than once
ldconfig: Cannot stat /usr/lib/libmysqlclient_r.so: No such file or directory
    libmysqld.so.18 -> libmysqld.so.18

哪个完全没了。解决方案

root@servah:~/c# ldconfig -n /lib 

它会重新更新所有库并清除缓存。