2012-03-20 61 views
0

我正在寫一些連接到MYSQL服務器的C代碼。我正在使用Netbeans,對此也是新手。我按照指示配置它並安裝了MYSQL連接器C.我還從cygwin站點安裝了CYGWIN GCC,G ++,GDB,MAKE。我創建了一個c項目,並在properties-> build-> c compiler-> Include目錄中設置mysql連接器的路徑(C:\ Program Files \ MySQL \ Connector C 6.0.2 \ include)。現在我編寫一些代碼來與MYSQL服務器進行交互,在構建時會發生一些錯誤。在開發C和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了很多,但找不到任何東西來解決這個問題。需要幫忙。

+0

這些是鏈接器錯誤。你是否與MySQL庫鏈接? – 2012-03-20 10:10:58

+0

@ Pileborg:HI,在properties-> build-> linker->其他庫目錄中,我設置路徑C:\ Program Files \ MySQL \ Connector C 6.0.2 \ lib \ opt。但同樣的錯誤仍然存​​在 – user924888 2012-03-20 10:21:49

+1

好的,您可以設置圖書館可能的路徑,但是您是否在某處添加了實際的圖書館? – 2012-03-20 10:27:13

回答

0

add -lmysql與mysql庫(適用於gcc)鏈接。或者,如果您使用其它編譯器,告訴編譯器

  • 其中的lib(添加路徑)
  • 到庫鏈接到可執行文件。這是不同的,然後添加路徑。
+0

@Beyer:HI,在properties-> build-> linker->其他庫目錄中,我設置路徑C:\ Program Files \ MySQL \ Connector C 6.0.2 \ lib \ opt。但同樣的錯誤仍然存​​在我正在使用WINDOWS XP機器 – user924888 2012-03-20 10:18:31

相關問題