gpt4 book ai didi

mysql - 开发 C 和 MYSQL 应用程序时出现构建错误

转载 作者:行者123 更新时间:2023-11-30 18:01:31 24 4
gpt4 key购买 nike

我正在用 C 语言编写一些连接到 MYSQL 服务器的代码。我正在使用 Netbeans,对此还很陌生。我按照指示配置它并安装了 MYSQL Connector C。我还从 cygwin 站点安装了 CYGWIN GCC、G++、GDB、MAKE。我创建了一个c项目,并在属性->构建->c编译器->包含目录中,设置mysql连接器的路径(C:\Program Files\MySQL\Connector C 6.0.2\include)。现在我编写了一些代码来与 MYSQL 服务器交互,在构建时出现一些错误。

#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>


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


MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "aaaa"; /* set me first */
char *database = "mysql";
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
/* send SQL query */
if (mysql_query(conn, "show tables")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
/* output table name */
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);
/* close connection */
mysql_free_result(res);
mysql_close(conn);

return (EXIT_SUCCESS);
}

构建后出现以下错误:

build/Debug/Cygwin-Windows/main.o: In function `main':
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:26: undefined reference to `_mysql_init'
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:28: undefined reference to `_mysql_real_connect'
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:30: undefined reference to `_mysql_error'
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:34: undefined reference to `_mysql_query'
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:35: undefined reference to `_mysql_error'
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:38: undefined reference to `_mysql_use_result'
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:41: undefined reference to `_mysql_fetch_row'
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:44: undefined reference to `_mysql_free_result'
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:45: undefined reference to `_mysql_close'
make[2]: Leaving directory `/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2'
make[1]: Leaving directory `/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/Cygwin-Windows/cppapplication_2.exe] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2


BUILD FAILED (exit value 2, total time: 1s)

我用谷歌搜索了很多,但找不到任何东西来解决这个问题。需要帮助。

最佳答案

添加-lmysql以链接mysql库(对gcc有效)。或者,如果您使用另一个编译器,请告诉编译器

  • 库所在位置(添加路径)
  • 将库链接到可执行文件。这是不同的东西,然后添加路径。

关于mysql - 开发 C 和 MYSQL 应用程序时出现构建错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9784691/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com