【问题标题】:error while connecting mariadb with c : undefined reference to `mysql_init@4'将 mariadb 与 c 连接时出错:未定义对 `mysql_init@4\' 的引用
【发布时间】:2022-11-02 17:15:47
【问题描述】:

我正在尝试使用 c 程序连接到 mariadb 数据库。最初它显示#include <mysql.h> 错误,因为没有这样的文件或目录。 但是在包含目录名之后,这个问题现在解决了,但它显示了另一个错误。

以下是我试图运行的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// #include "C:/Program Files/MariaDB 10.11/include/mysql/my_global.h"
#include "mysql/mysql.h"


int main (int argc, char* argv[])
{

   // Initialize Connection
   MYSQL *conn;
   if (!(conn = mysql_init(0)))
   {
      fprintf(stderr, "unable to initialize connection struct\n");
      exit(1);
   }

   // Connect to the database
   if (!mysql_real_connect(
         conn,                 // Connection
         "mariadb.example.net", // Host
         "db_user",            // User account
         "db_user_password",   // User password
         "test",               // Default database
         3306,                 // Port number
         NULL,                 // Path to socket file
         0                     // Additional options
      ));
   {
      // Report the failed-connection error & close the handle
      fprintf(stderr, "Error connecting to Server: %s\n", mysql_error(conn));
      mysql_close(conn);
      exit(1);
   }

   // Use the Connection
   // ...

   // Close the Connection
   mysql_close(conn);

   return 0;
}

我在输出中收到以下错误:

C:\Users\hajos\AppData\Local\Temp\ccW0Xnqk.o:test.c:(.text+0x1e): undefined reference to `mysql_init@4'
C:\Users\hajos\AppData\Local\Temp\ccW0Xnqk.o:test.c:(.text+0xa1): undefined reference to `mysql_real_connect@32'
C:\Users\hajos\AppData\Local\Temp\ccW0Xnqk.o:test.c:(.text+0xaf): undefined reference to `mysql_error@4'
C:\Users\hajos\AppData\Local\Temp\ccW0Xnqk.o:test.c:(.text+0xd9): undefined reference to `mysql_close@4'
collect2.exe: error: ld returned 1 exit status

谁能解释问题是什么以及如何解决?

【问题讨论】:

  • 您是否为 MySQL 函数添加了库?你的链接器命令是什么?

标签: mysql c mariadb


【解决方案1】:

您必须链接 MariaDB 连接器/C 库。

来自MariaDB Connector/C documentation

将您的应用程序与 MariaDB Connector/C 链接起来 视窗

对于静态链接库 libmariadb.lib 是必需的,对于动态链接使用 libmariadb.dll。使用 MSI 安装程序,可以在 MariaDB Connector/C 安装的 lib 目录中找到这些库。

除非您使用实验性插件 remote_io(它需要 curl 库),否则除了 Windows 系统库之外,它不依赖于其他库。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    • 2020-09-05
    • 1970-01-01
    相关资源
    最近更新 更多