MySQL的连接器/ C ++不能进入崩溃(MySQL Connector/C++ BAD ACCE

2019-07-18 21:23发布

使用C ++在Xcode中我尝试用MySQL连接器/ C ++来访问MySQL数据库。 问题是程序(在Xcode编译)总是崩溃

EXC_BAD_ACCESS (code=13, address=0x0)

当调用

driver->connect(url, user, pass)

在Xcode中,我创建一个完整的新项目(OS X>命令行工具),插入main.cpp中的代码(见下文),添加上去和MySQL连接器插头包括路径以及libmysqlcppconn.6.1.1.1.dylib为链接图书馆和点击运行按钮。

接下来的事情是,当我编译程序使用手动

c++ -o test -I /usr/local/mysqlConnector/include/ -lmysqlcppconn main.cpp

该程序运行正常,并运行在表中的INSERT语句。

所述程序代码被从MySQL连接/ C ++的例子,即pthreads.cpp例如拍摄,但截断的主要部分:

/* Standard C++ includes */
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <stdexcept>

#include <mysql_connection.h>
#include <mysql_driver.h>

#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>

std::string url;
std::string user;
std::string pass;
std::string database;

/**
 * Usage example for Driver, Connection, (simple) Statement, ResultSet
 */
int main(int argc, const char **argv)
{
    sql::Driver *driver;
    std::auto_ptr< sql::Connection > con;

    url = "tcp://127.0.0.1:3306";
    user = "appserver";
    pass = "testpw";
    database = "appserver";

    try {
        driver = sql::mysql::get_driver_instance();

        /* Using the Driver to create a connection */
        con.reset(driver->connect(url, user, pass));
        con->setSchema(database);

    sql::Statement* stmt = con->createStatement();
    stmt->execute("INSERT INTO testtable (testnumber) values (5)");
    } catch (sql::SQLException &e) {
        return EXIT_FAILURE;
    } catch (std::runtime_error &e) {
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

Answer 1:

好了,问题就解决了。

这里的问题是一个编译标志。 MySQL的连接器/ C ++没有被编译
-stdlib=libc++标志,但Xcode的补充说,编译/链接标志其命令。 这导致崩溃。 这也解释了,为什么手工编译的程序工作,因为我不包括标志编译命令。

为了更清楚:我重新编译MySQL的连接器/ C ++与-stdlib=libc++标志。 然后通过Xcode的编译程序为我工作得很好。 要编译MySQL的连接器/ C ++我加

-DMYSQL_CXXFLAGS=-stdlib=libc++

cmake需要安装连接器时要运行命令。

make VERBOSE=1

然后证实,在编译源的连接器,当标志被实际使用。



文章来源: MySQL Connector/C++ BAD ACCESS crash