【问题标题】:How can I deal with dll and lib files for connecting MariaDB to C++ Application?如何处理用于将 MariaDB 连接到 C++ 应用程序的 dll 和 lib 文件?
【发布时间】:2021-04-26 03:10:57
【问题描述】:

我正在尝试将 MariaDB 与 C++ 应用程序连接起来的项目。 我提到了网址: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文件的链接器。 这是我打算执行的代码:

// 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 violation reading location 0xFFFFFFFFFFFFFFFF 在 std::unique_ptr&lt;sql::Connection&gt; conn(driver-&gt;connect(url, properties));.

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

【问题讨论】:

  • 您需要阅读this C++ reference,一个好的C++ programming book,以及您的C++ 编译器和链接器的文档。您还需要使用一个好的调试器。您是否尝试使用 GCC 作为您的 C++ 编译器,并使用 GDB 作为您的调试器?
  • sql::Driver* driver = sql::mariadb::get_driver_instance(); 之后你可能有drivernullptr。你检查了吗?
  • 驱动程序不能真的是 nullptr。可以找到并加载 dll。问题不在于 dll/lib 文件的位置,看来

标签: c++ dll mariadb lib


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 2014-05-30
    相关资源
    最近更新 更多