4

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

EXC_BAD_ACCESS (code=13, address=0x0)

打电话时

driver->connect(url, user, pass)

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

接下来是,当我使用手动编译程序时

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

该程序运行良好,并且还在表上运行 INSERT 语句。

程序代码取自 MySQL Connector/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;
}
4

1 回答 1

8

好的,问题解决了。

这里的问题是一个编译标志。MySQL 连接器/C++ 是在没有
-stdlib=libc++标志的情况下编译的,但是 Xcode 将编译/链接标志添加到它的命令中。这导致了崩溃。这也解释了为什么手动编译的程序可以工作,因为我没有将该标志包含在编译命令中。

为了更清楚:我用-stdlib=libc++标志重新编译了 MySQL 连接器/C++。然后 Xcode 编译的程序对我来说很好。为了编译我添加的 MySQL 连接器/C++

-DMYSQL_CXXFLAGS=-stdlib=libc++

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

make VERBOSE=1

然后证明在编译连接器源时实际使用了该标志。

于 2013-01-28T23:46:13.357 回答