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

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

本文关键字:MySQL 连接 数据库 问题 C++ CLion Mac      更新时间:2023-10-16

我一直在尝试使用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(


我在跑;苹果操作系统 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)

我相信我在上面的路径都是正确的。

我的工具链看起来像

我的 CMake 配置看起来像

最后,输出错误如下所示;

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

不幸的是,进展仍未真正接近。

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

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

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