简体   繁体   English

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

[英]Trouble connecting to MySQL database using C++ on a Mac with CLion

I've been trying to connect to a MySQL database using C++ and specifically using the example from here:我一直在尝试使用 C++ 连接到 MySQL 数据库,特别是使用此处的示例:

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

and getting the error Undefined symbols for architecture x86_64 (abbreviated output at bottom of post or full output here: https://gist.github.com/plisken1/2de09557954b16c5a86348177a0bcff8 ) and getting the error Undefined symbols for architecture x86_64 (abbreviated output at bottom of post or full output here: https://gist.github.com/plisken1/2de09557954b16c5a86348177a0bcff8 )


I'm running;我在跑; MacOS 10.14.6 macOS 10.14.6

CLion Version: 2020.01.2 CLion版本:2020.01.2

I've Installed the MySQL C++ Connector Version 8.0.18 from here: https://dev.mysql.com/downloads/connector/cpp/ I've Installed the MySQL C++ Connector Version 8.0.18 from here: https://dev.mysql.com/downloads/connector/cpp/

I've also installed boost version 1.72 via Home-brew from here: https://formulae.brew.sh/formula/boost我还从这里通过 Home-brew 安装了 boost 1.72 版https://formulae.brew.sh/formula/boost

The example code from the link above is in a file called main.cpp and the source is as below;上面链接中的示例代码位于名为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;
}

Naturally I've not gotten as far as entering my own database details.当然,我还没有输入我自己的数据库详细信息。

My CMakeLists.txt file looks like;我的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)

I believe my paths in the above are all correct.我相信我在上面的路径都是正确的。

My Toolchain looks like我的工具链看起来像

My CMake config looks like我的 CMake 配置看起来像

and lastly the output error looks like;最后 output 错误看起来像;

====================[ 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)

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

MANY SIMILAR LINES REMOVED TO KEEP TO POST CHAR LIMIT FULL OUTPUT HERE: https://gist.github.com/plisken1/2de09557954b16c5a86348177a0bcff8 MANY SIMILAR LINES REMOVED TO KEEP TO POST CHAR LIMIT FULL OUTPUT HERE: 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

I'm guessing the big hint here is: Undefined symbols for architecture x86_64 but I've no clue how to resolve.我猜这里的重要提示是:架构 x86_64 的未定义符号,但我不知道如何解决。

Any and all assistances would be greatly appreciated.任何和所有的帮助将不胜感激。

UPDATE:更新:

I can now get the source to compile from the command-line with the following;我现在可以使用以下命令从命令行获取要编译的源代码;

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

but on running it, I get the following error;但是在运行它时,我收到以下错误;

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

Progress but still not really close unfortunately.进展,但不幸的是仍然没有真正接近。

I spent hours myself trying to compile the basic tutorial on the MySQL C++ Connector website.我自己花了几个小时试图在 MySQL C++ 连接器网站上编译基本教程。 Using your compile command I was able to get the same error as you.使用您的编译命令,我能够得到与您相同的错误。 I found the following article addressing how to solve a dyld Library not loaded error:我发现以下文章解决了如何解决 dyld Library not loaded 错误:

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

The problem is that the libraries are stored in /usr/local/mysql-connector-c++/lib64 but the program is not loading said libraries.问题是库存储在 /usr/local/mysql-connector-c++/lib64 但程序没有加载所述库。 I followed solution #1 in the link, which was to create symbolic links for the library that would not load.我在链接中遵循了解决方案#1,这是为不会加载的库创建符号链接。 As one could expect, another abort trap error would happen each time with a new library to be added.正如人们所预料的那样,每次添加新库时都会发生另一个中止陷阱错误。 Once I added all required libraries to /usr/lib, I was able to run my program and access my database.将所有必需的库添加到 /usr/lib 后,我就能够运行我的程序并访问我的数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM