如何处理用于将 MariaDB 连接到 C++ 应用程序的 dll 和 lib 文件?

问题描述

我正在尝试将 MariaDB 与 C++ 应用程序连接起来的项目。 我提到了 URL:https://mariadb.com/docs/clients/connector-cpp/#installing-mariadb-connector-c-via-msi-windows。 这个 URL 是连接 MariaDB 和 C++ 的一个很好的来源。但是,它没有描述如何处理 lib 文件和 dll 文件。 当我通过 MSI 安装 MariaDB 连接器/C++ 时,它给了我几个文件:conncpp.hpp、mariadbcpp.dll、mariadbcpp.lib 等。

我尝试通过设置路径 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29910\include 来包含 mariadb/conncpp.hpp 并且我对.lib 文件。 另外,我转到了属性并为 lib 文件设置了链接器。 这是我计划执行的代码

// Includes
#include <iostream>
#include <mariadb/conncpp.hpp>

// Main Process
int main(int argc,char** argv)
{
    try
    {
        // Instantiate Driver
        sql::Driver* driver = sql::mariadb::get_driver_instance();
    // Configure Connection
    // The URL or TCP connection string format is
    // ``jdbc:mariadb://host:port/database``.
    sql::sqlString url("jdbc:mariadb://192.0.2.1:3306/test");

    // Use a properties map for the user name and password
    sql::Properties properties({
          {"user","db_user"},{"password","db_user_password"}
        });

    // Establish Connection
    // Use a smart pointer for extra safety
    std::unique_ptr<sql::Connection> conn(driver->connect(url,properties));

    // Use Connection
    // ...

    // Close Connection
    conn->close();
}

// Catch Exceptions
catch (sql::sqlException& e)
{
    std::cerr << "Error Connecting to MariaDB Platform: "
        << e.what() << std::endl;

    // Exit (Failed)
    return 1;
}

// Exit (Success)
return 0;
}

但是每当我编译和执行代码时,它都会说 Unhandled exception(0x00007FF918058D25(mariadbcpp.dll),MariaDB_Connection.exe): 0xC0000005:Access conflict reading location 0xFFFFFFFFFFFFFFFF at the line std::unique_ptr<sql::Connection> conn(driver->connect(url,properties));

你能告诉我如何解决这个问题吗?

解决方法

您发现了 C++ 连接器 API 的不足。或者称之为错误。它是特定于 Windows 的。 STL 对象的调试和发布版本可能具有不同的布局。 将构建切换到发布配置应该会有所帮助。但是根据使用的 VS 版本,仍然可能出现一些问题。作为一种解决方法,您还可以尝试使用其他连接方法,即 std::unique_ptr<:connection> conn(driver->connect(url,"db_user","db_user_password")); 因为它不依赖于 Properties 映射,所以会导致这里出现问题。