下载了完整的 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<char, std::char_traits<char>, std::allocator<char> > const&)'
C:/Program Files/MySQL/MySQL Connector C++ 8.0/include/jdbc/cppconn/driver.h:83: undefined reference to `check(std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > 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 <iostream>
#include <mysql_driver.h>
#include <mysql_connection.h>
#include "cppconn/statement.h"
int main() {
sql::mysql::MySQL_Driver *driver;
sql::Connection *connection;
// 创建 MySQL 驱动程序实例
driver = sql::mysql::get_mysql_driver_instance();
std::cout << "Hello, World!" << std::endl;
return 0;
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.