【发布时间】: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 函数添加了库?你的链接器命令是什么?