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

2023-06-08 12:05:23 +08:00
 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<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;
}

1166 次点击
所在节点    问与答
6 条回复
weidaizi
2023-06-08 12:45:43 +08:00
你这链接失败了,检查一下库的位置是否正确。以及尽量别用绝对路径,cmake 的 find_package 配合着 CMAKE_PREFIX_PATH 使用就好了,这个随手搜一搜就知道了
mangoDB
2023-06-08 12:51:55 +08:00
error: ld returned 1 exit status

是不是库没在搜索路径里
kimjosda
2023-06-08 13:34:07 +08:00
@weidaizi 还是报错
报错信息如下:
```
CMake Error at CMakeLists.txt:20 (find_package):
By not providing "Findmysqlcppconn.cmake" in CMAKE_MODULE_PATH this project
has asked CMake to find a package configuration file provided by
"mysqlcppconn", but CMake did not find one.

Could not find a package configuration file provided by "mysqlcppconn" with
any of the following names:

mysqlcppconnConfig.cmake
mysqlcppconn-config.cmake

Add the installation prefix of "mysqlcppconn" to CMAKE_PREFIX_PATH or set
"mysqlcppconn_DIR" to a directory containing one of the above files. If
"mysqlcppconn" provides a separate development package or SDK, be sure it
has been installed.
```
我新加入的 cmake 代码如下
```
set(CMAKE_PREFIX_PATH "C:/Program Files/MySQL/MySQL Connector C++ 8.0/lib64/vs14")
find_package(mysqlcppconn REQUIRED)
```
我能看到 mysqlcppconn.lib 就在 vs14 文件夹里面,也设置了路径,但不知道为什么找不到
kimjosda
2023-06-08 13:34:41 +08:00
@mangoDB 请问这个需要怎么解决,能具体点吗?
weidaizi
2023-06-08 15:22:29 +08:00
找了一下,貌似 cmake 自带的 module 里面没有 mysql-connector-cpp 的,自己写一下 search 就好了
```
include(FindPackageHandleStandardArgs)
find_path(MYSQLCPPCONN_INCLUDE_DIR
NAMES mysql_driver.h
PATH_SUFFIXES jdbc
NO_SYSTEM_ENVIRONMENT_PATH
NO_CMAKE_SYSTEM_PATH)
find_library(MYSQLCPPCONN_LIBRARIES
NAMES mysqlcppconn
PATH_SUFFIXES lib64/vs14
NO_SYSTEM_ENVIRONMENT_PATH
NO_CMAKE_SYSTEM_PATH)

find_package_handle_standard_args(MYSQLCPPCONN
REQUIRED_VARS MYSQLCPPCONN_INCLUDE_DIR MYSQLCPPCONN_LIBRARIES)

if (MYSQLCPPCONN_FOUND)
message("# find mysqlcppconn include dir: ${MYSQLCPPCONN_INCLUDE_DIR}")
message("# find mysqlcppconn libraries: ${MYSQLCPPCONN_LIBRARIES}")
else()
message("${MYSQLCPPCONN_INCLUDE_DIR}")
message("${MYSQLCPPCONN_LIBRARIES}")
message(FATAL_ERROR "Failed found mysqlcppconn")
endif()


add_executable(hello src/hello.cpp)
target_include_directories(hello PUBLIC ${MYSQLCPPCONN_INCLUDE_DIR})
target_link_libraries(hello ${MYSQLCPPCONN_LIBRARIES})
```

然后执行
```
cmake -S ${source_dir} -B ${build_dir} -DCMAKE_PREFIX_PATH=${deps_search_path}
```

1. 注意我上面的 find_library 是以 windows 为例,linux 解压缩出来的路径肯定不是 vs14
2. 执行语句中,把对应的变量替换一下就好了
kimjosda
2023-06-08 17:33:07 +08:00
@weidaizi 完全看不懂

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/946912

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX