MySQL连接器/ C ++ BAD ACCESS崩溃 [英] MySQL Connector/C++ BAD ACCESS crash

查看:280
本文介绍了MySQL连接器/ C ++ BAD ACCESS崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

崩溃。

  EXC_BAD_ACCESS(code = 13,address = 0x0)

 <$ c $ <$> 

c> driver-> connect(url,user,pass)

项目(OS X>命令行工具),在main.cpp中插入代码(见下文),添加Boost和MySQL Connector头包括路径以及libmysqlcppconn.6.1.1.1.dylib作为链接库,并点击运行按钮。




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

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



程序代码取自MySQL Connector / C ++示例,即pthreads.cpp示例,到基本部分:

  / *标准C ++包括* / 
#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;

/ **
*驱动程序,连接,(简单)语句,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();

/ *使用驱动程序创建连接* /
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;
}


解决方案



这里的问题是一个编译标志。 MySQL连接器/ C ++编译时没有

-stdlib = libc ++ 标志,但Xcode在其命令中添加了compile / link标志。这导致崩溃。这也解释了为什么手动编译的程序工作,因为我没有包括那个标志的编译命令。



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

  -DMYSQL_CXXFLAGS = -stdlib = libc ++ 



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

  make VERBOSE = 1 

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


Using C++ in Xcode I try to access a MySQL database with the MySQL Connector/C++. Problem is that the program (compiled with Xcode) always crashes with

EXC_BAD_ACCESS (code=13, address=0x0)

when calling

driver->connect(url, user, pass)

In Xcode I created a complete new project (OS X > Command Line Tool), inserted the code (see below) in the main.cpp, added Boost and MySQL Connector header include paths as well as libmysqlcppconn.6.1.1.1.dylib as Link Library and hit the Run button.

Next thing is, when I compile the program manually using

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

the program runs fine and also runs the INSERT statement on the table.

The program code is taken from the MySQL Connector/C++ examples, namely the pthreads.cpp example, but truncated to the essential parts:

/* 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;
}

解决方案

Ok, the problem is solved.

Here the problem was one compile flag. The MySQL Connector/C++ was compiled without the
-stdlib=libc++ flag, but Xcode added that compile/link flag to its commands. This caused the crash. This also explains, why the manually compiled program worked, as I didn't include that flag to the compile command.

To make it more clear: I recompiled the MySQL Connector/C++ with the -stdlib=libc++ flag. Then the program compiled by Xcode works fine for me. To compile the MySQL Connector/C++ I added

-DMYSQL_CXXFLAGS=-stdlib=libc++

to the cmake command that needs to be run when installing the connector.

make VERBOSE=1

then proved that the flag is actually used when compiling the connectors source.

这篇关于MySQL连接器/ C ++ BAD ACCESS崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆