Clion 中使用 MySQL connector/C++报错求助!

查看 59|回复 2
作者:kimjosda   
下载了完整的 MySQL Connector C++ 8.0 ,在 Clion 写了 CmakeList 文件,但是一运行代码就报错。
就是运行到 driver 这一行的语句就报错(代码在下面),查了 chatgpt 或 google 没有发现解决方案, 所以来 v 站想请教一下广大的 v 友们。
下面是错误信息,其实看不太懂
====================[ Build | server | Debug ]==================================
D:\SOFTWARE\jetbrainTool\apps\CLion\ch-0\231.9011.31\bin\cmake\win\x64\bin\cmake.exe --build D:\zjcfile\weblearn\OTHER\RFID\testcodefiles\server\cmake-build-debug --target server -j 10
[1/2] Building CXX object CMakeFiles/server.dir/main.cpp.obj
[2/2] Linking CXX executable server.exe
FAILED: server.exe
cmd.exe /C "cd . && D:\SOFTWARE\mingw64\bin\c++.exe -g  CMakeFiles/server.dir/main.cpp.obj -o server.exe -Wl,--out-implib,libserver.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -LC:/PROGRA~1/MySQL/MYSQLC~1.0/lib64/vs14 -lmysqlcppconn  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
CMakeFiles/server.dir/main.cpp.obj: In function `check_lib':
C:/Program Files/MySQL/MySQL Connector C++ 8.0/include/jdbc/cppconn/driver.h:82: undefined reference to `check(std::__cxx11::basic_string, std::allocator > const&)'
C:/Program Files/MySQL/MySQL Connector C++ 8.0/include/jdbc/cppconn/driver.h:83: undefined reference to `check(std::map, std::allocator >, std::__cxx11::basic_string, std::allocator >, std::less, std::allocator > >, std::allocator, std::allocator > const, std::__cxx11::basic_string, std::allocator > > > > const&)'
CMakeFiles/server.dir/main.cpp.obj: In function `get_driver_instance_by_name':
C:/Program Files/MySQL/MySQL Connector C++ 8.0/include/jdbc/mysql_driver.h:116: undefined reference to `sql::mysql::_get_driver_instance_by_name(char const*)'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
然后附上我的 cmakelist 文件
cmake_minimum_required(VERSION 3.25)
project(server)
set(CMAKE_CXX_STANDARD 17)
# new
# 设置 MySQL Connector/C++的路径
set(MYSQL_CONNECTOR_CPP_INCLUDE_DIR "C:\\Program Files\\MySQL\\MySQL Connector C++ 8.0\\include\\jdbc")  # 替换为实际的安装路径
set(MYSQL_CONNECTOR_CPP_LIBRARY_DIR "C:\\Program Files\\MySQL\\MySQL Connector C++ 8.0\\lib64\\vs14")  # 替换为实际的安装路径
# 添加头文件路径
include_directories(${MYSQL_CONNECTOR_CPP_INCLUDE_DIR})
# 添加库文件路径
link_directories(${MYSQL_CONNECTOR_CPP_LIBRARY_DIR})
# new
add_executable(server main.cpp)
# new
# 链接 MySQL Connector/C++库
target_link_libraries(server mysqlcppconn)
#target_link_libraries(server "${MYSQL_CONNECTOR_CPP_LIBRARY_DIR}/mysqlcppconn.lib")
# new
最后附上 C++代码
#include [i]
#include
#include
#include "cppconn/statement.h"
int main() {
    sql::mysql::MySQL_Driver *driver;
    sql::Connection *connection;
    // 创建 MySQL 驱动程序实例
    driver = sql::mysql::get_mysql_driver_instance();
    std::cout

std, MySQL, char, Server

weidaizi   
你这链接失败了,检查一下库的位置是否正确。以及尽量别用绝对路径,cmake 的 find_package 配合着 CMAKE_PREFIX_PATH 使用就好了,这个随手搜一搜就知道了
mangoDB   
error: ld returned 1 exit status
是不是库没在搜索路径里
您需要登录后才可以回帖 登录 | 立即注册

返回顶部