在具有CLion的Mac上使用C ++连接到MySQL数据库时出现问题

时间:2020-06-25 14:27:50

标签: c++ mysql macos clion

我一直在尝试使用C ++并特别是从此处使用示例连接到MySQL数据库:

https://dev.mysql.com/doc/connector-cpp/1.1/en/connector-cpp-examples-complete-example-1.html

并得到错误架构x86_64的未定义符号(帖子底部的缩写输出或完整输出:https://gist.github.com/plisken1/2de09557954b16c5a86348177a0bcff8


我在跑步; MacOS 10.14.6

CLion版本:2020.01.2

我已经从以下位置安装了 MySQL C ++连接器版本8.0.18 https://dev.mysql.com/downloads/connector/cpp/

我还通过以下网址通过Home-brew安装了 boost版本1.72 https://formulae.brew.sh/formula/boost

上面链接中的示例代码位于一个名为 main.cpp 的文件中,其来源如下:

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

/*
  Include directly the different
  headers from cppconn/ and mysql_driver.h + mysql_util.h
  (and mysql_connection.h). This will reduce your build time!
*/
#include "mysql_connection.h"

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

using namespace std;

int main(void)
{
    cout << endl;
    cout << "Running 'SELECT 'Hello World!' AS _message'..." << endl;

    try {
        sql::Driver *driver;
        sql::Connection *con;
        sql::Statement *stmt;
        sql::ResultSet *res;

        /* Create a connection */
        driver = get_driver_instance();
        con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
        /* Connect to the MySQL test database */
        con->setSchema("test");

        stmt = con->createStatement();
        res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
        while (res->next()) {
            cout << "\t... MySQL replies: ";
            /* Access column data by alias or column name */
            cout << res->getString("_message") << endl;
            cout << "\t... MySQL says it again: ";
            /* Access column data by numeric offset, 1 is the first column */
            cout << res->getString(1) << endl;
        }
        delete res;
        delete stmt;
        delete con;

    } catch (sql::SQLException &e) {
        cout << "# ERR: SQLException in " << __FILE__;
        cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
        cout << "# ERR: " << e.what();
        cout << " (MySQL error code: " << e.getErrorCode();
        cout << ", SQLState: " << e.getSQLState() << " )" << endl;
    }

    cout << endl;

    return EXIT_SUCCESS;
}

自然地,我还没有输入自己的数据库详细信息。

我的 CMakeLists.txt 文件如下;

cmake_minimum_required(VERSION 3.16)
project(mySQL_Example)

set(CMAKE_CXX_STANDARD 11)

#For mysql connector include..
include_directories(/usr/local/mysql-connector-c++-8.0.18/include/jdbc/)
    
#For Boost..
include_directories(/usr/local/Cellar/boost/1.72.0_3/include/)    
 
add_executable(mySQL_Example main.cpp)    

#For imported linking..
add_library(libmysqlcppconn STATIC IMPORTED)

set_property(TARGET libmysqlcppconn PROPERTY IMPORTED_LOCATION /usr/local/mysql-connector-c++-8.0.18/lib64/libmysqlcppconn-static.a)

target_link_libraries (mySQL_Example libmysqlcppconn)

我相信上面的方法都是正确的。

My Toolchain looks like

My CMake config looks like

最后出现输出错误;

====================[ Build | mySQL_Example | Debug ]===========================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build "/Volumes/D_SLAVE/My Documents/My Projects/CLion/mySQL_Example/cmake-build-debug" --target mySQL_Example -- -j 12
[ 50%] Linking CXX executable mySQL_Example
Undefined symbols for architecture x86_64:
  "_BIO_free", referenced from:
      sha256_password_auth_client(MYSQL_PLUGIN_VIO*, MYSQL*) in libmysqlcppconn-static.a(client_authentication.cc.o)
      sha256_password_auth_client_nonblocking(MYSQL_PLUGIN_VIO*, MYSQL*, int*) in libmysqlcppconn-static.a(client_authentication.cc.o)
      caching_sha2_password_auth_client(MYSQL_PLUGIN_VIO*, MYSQL*) in libmysqlcppconn-static.a(client_authentication.cc.o)
      caching_sha2_password_auth_client_nonblocking(MYSQL_PLUGIN_VIO*, MYSQL*, int*) in libmysqlcppconn-static.a(client_authentication.cc.o)

############################################# #

删除了许多相似的行以保留后限制 此处的完整输出:https://gist.github.com/plisken1/2de09557954b16c5a86348177a0bcff8

############################################# #

  "_X509_check_host", referenced from:
      ssl_verify_server_cert(Vio*, char const*, char const**) in libmysqlcppconn-static.a(client.cc.o)
  "_X509_check_ip_asc", referenced from:
      ssl_verify_server_cert(Vio*, char const*, char const**) in libmysqlcppconn-static.a(client.cc.o)
  "_X509_free", referenced from:
      ssl_verify_server_cert(Vio*, char const*, char const**) in libmysqlcppconn-static.a(client.cc.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [mySQL_Example] Error 1
make[2]: *** [CMakeFiles/mySQL_Example.dir/all] Error 2
make[1]: *** [CMakeFiles/mySQL_Example.dir/rule] Error 2
make: *** [mySQL_Example] Error 2

我猜这里最大的提示是:架构x86_64的未定义符号,但是我不知道如何解决。

我们将不胜感激。

更新:

我现在可以使用以下命令从命令行编译源代码;

g++ -I /usr/local/Cellar/boost/1.72.0_3 main.cpp -I /usr/local/mysql-connector-c++-8.0.18/include/jdbc -I /usr/local/Cellar/boost/1.72.0_3/include/boost -o main.o -L /usr/local/mysql-connector-c++-8.0.18/lib64/ -l mysqlcppconn

但是在运行它时,出现以下错误;

dyld: Library not loaded: @rpath/libmysqlcppconn.7.dylib
  Referenced from: /Volumes/D_SLAVE/SHARED/Debian/mySql_test/./main.o
  Reason: image not found
Abort trap: 6

进展顺利,但不幸的是还没有真正结束。

1 个答案:

答案 0 :(得分:0)

我花了几个小时试图编译 MySQL C++ 连接器网站上的基本教程。使用您的编译命令,我能够得到与您相同的错误。我发现以下文章解决了如何解决 dyld 库未加载错误:

https://appuals.com/how-to-fix-dyld-library-not-loaded-error-on-macos/

问题是库存储在 /usr/local/mysql-connector-c++/lib64 中,但程序没有加载所述库。我遵循链接中的解决方案#1,即为无法加载的库创建符号链接。正如人们所料,每次添加一个新库时,都会发生另一个中止陷阱错误。将所有必需的库添加到 /usr/lib 后,我就能够运行我的程序并访问我的数据库。

相关问题