连接过程中mysql c ++分段错误

时间:2013-12-31 16:01:20

标签: c++ mysql mysql-connector

尝试使用c ++代码连接到mysql数据库时,当我调用connect时,我遇到了分段错误。

这是代码:

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
#include "mysql_connection.h"
#include "mysql_driver.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/connection.h>
#include <cppconn/prepared_statement.h>
#include "iolib.h"

int main(int argc, char* argv[])
{
    cout<<"1\n";
    sql::Driver *driver = sql::mysql::get_driver_instance();
    cout<<"Driver: "<<driver<<", "<<driver->getName()<<"\n";
    sql::Connection *conn = driver->connect("tcp://<ip>:3306", "root", "password");
    cout<<"Connected\n";
    return 0;
}

输出结果为:

1
Driver: 0x1755050, MySQL Connector C++ (libmysql)
Segmentation fault (core dumped)

以前我忘了将这台机器添加到允许的IP中,然后它给了我一个错误,说我没有被授权而不是分段错误。我允许IP后,我收到此错误。我可以阻止IP再次获得不允许的IP错误。

这是makefile:

CC=g++
CFLAGS=-c -g -rdynamic
LDFLAGS=-lpthread -ldl -rdynamic -lmysqlcppconn
EXECUTABLE=myexe

all: main
    $(CC) $(LDFLAGS) *.o -o $(EXECUTABLE)
    @echo -e "\033[32mFinished!\033[0m"

main:
    $(CC) $(CFLAGS) -o iolib.o iolib.cpp
    $(CC) $(CFLAGS) -o importer.o importer.cpp
    @echo -e "\033[36mObject files compiled!\033[0m"

这台机器正在运行Fedora 19,并且库安装了“yum install mysql mysql-connector-c ++ - devel mysql ++”。我可能会对这里的确切命令略感一点,但重要的是说它们都是用yum安装的。

在网上搜索类似的问题,我遇到的最可能的事情就是人们说我的库一定有问题所以ldd myexe如下。

    linux-vdso.so.1 =>  (0x00007fff72e6b000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x000000370f000000)
    libdl.so.2 => /lib64/libdl.so.2 (0x000000370ec00000)
    libmysqlcppconn.so.6 => /lib64/libmysqlcppconn.so.6 (0x00007f2c335ee000)
    libstdc++.so.6 => /lib64/libstdc++.so.6 (0x0000003716800000)
    libm.so.6 => /lib64/libm.so.6 (0x000000370f800000)
    libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x0000003710800000)
    libc.so.6 => /lib64/libc.so.6 (0x000000370e800000)
    /lib64/ld-linux-x86-64.so.2 (0x000000370e400000)
    libmysqlclient.so.18 => /usr/lib64/mysql/libmysqlclient.so.18 (0x00007f2c3301d000)
    libz.so.1 => /lib64/libz.so.1 (0x000000370f400000)
    libssl.so.10 => /lib64/libssl.so.10 (0x0000003728800000)
    libcrypto.so.10 => /lib64/libcrypto.so.10 (0x0000003723400000)
    libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x0000003724c00000)
    libkrb5.so.3 => /lib64/libkrb5.so.3 (0x0000003725800000)
    libcom_err.so.2 => /lib64/libcom_err.so.2 (0x0000003723c00000)
    libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x0000003725400000)
    libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x0000003725000000)
    libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x0000003724800000)
    libresolv.so.2 => /lib64/libresolv.so.2 (0x0000003711000000)
    libselinux.so.1 => /lib64/libselinux.so.1 (0x0000003710400000)
    libpcre.so.1 => /lib64/libpcre.so.1 (0x0000003710000000)

我该如何做到这一点?

我感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

许多MySQL C ++ Connector函数都会抛出异常。

使用try / catch块围绕您的函数调用以捕获异常。当执行进入catch块时,使用调试器从异常中查找更多信息,例如what()方法。

另一种方法是链接库的调试版本。您可能必须下载源并自行构建它们。