V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
kimjosda
V2EX  ›  问与答

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

  •  
  •   kimjosda · 2023-06-08 12:05:23 +08:00 · 1117 次点击
    这是一个创建于 532 天前的主题,其中的信息可能已经有所发展或是发生改变。

    下载了完整的 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;
    }
    
    
    第 1 条附言  ·  2023-06-08 15:14:55 +08:00
    其实注释掉 driver 这一行语句是可以编译成功的,取消注释就不行了,出现上述问题
    6 条回复    2023-06-08 17:33:07 +08:00
    weidaizi
        1
    weidaizi  
       2023-06-08 12:45:43 +08:00
    你这链接失败了,检查一下库的位置是否正确。以及尽量别用绝对路径,cmake 的 find_package 配合着 CMAKE_PREFIX_PATH 使用就好了,这个随手搜一搜就知道了
    mangoDB
        2
    mangoDB  
       2023-06-08 12:51:55 +08:00
    error: ld returned 1 exit status

    是不是库没在搜索路径里
    kimjosda
        3
    kimjosda  
    OP
       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
        4
    kimjosda  
    OP
       2023-06-08 13:34:41 +08:00
    @mangoDB 请问这个需要怎么解决,能具体点吗?
    weidaizi
        5
    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
        6
    kimjosda  
    OP
       2023-06-08 17:33:07 +08:00
    @weidaizi 完全看不懂
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1075 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 18ms · UTC 19:19 · PVG 03:19 · LAX 11:19 · JFK 14:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.