在开发C和MYSQL应用程序时构建错误

时间:2012-03-20 10:06:16

标签: mysql c netbeans eclipse-cdt

我正在用C编写一些连接到MYSQL服务器的代码。我正在使用Netbeans和新的。我按照指示将其配置并安装了MYSQL Connector C.我还从cygwin站点安装了CYGWIN GCC,G ++,GDB,MAKE。我在属性中创建了一个c项目 - >建设 - > c compiler->包含目录,设置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)

我google了很多但找不到任何解决这个问题的方法。需要帮助。

1 个答案:

答案 0 :(得分:0)

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

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