就是运行到 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