2012-03-10 68 views
3
#include <stdlib.h> 
#include <mysql.h> 

#include <my_global.h> 


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

MYSQL *conn; 
MYSQL_RES *res; 
MYSQL_ROW row; 

char *server = "127.0.0.1"; 
char *user = "root"; 
char *password = "1386"; 
char *database = "OurDB"; 

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(0); 
} 

    return 0; 
} 

,我收到鏈接錯誤代碼塊:mysql用C++錯誤:未定義參考mysql_init

undefined reference to mysql_init 

我用連接器選項mysql_config --libs和編譯器選項mysql_config --cflags

我在某處閱讀我應該添加一些類似libmysql.lib的庫,但我無法在我的PC上找到此文件(我正在使用Ubuntu 11.04 64位)。

+1

libmysql.lib是Windows。你應該尋找linux/ubuntu的那種。 – RvdK 2012-03-10 11:01:22

+0

以及我應該如何處理Ubuntu? – 2012-03-10 11:09:36

回答

0

設置 - >編譯器和調試器 - >搜索目錄 - >鏈接 然後添加並插入/usr/lib/mysql/

+0

「設置」在哪裏?有一個名爲「設置」的下拉菜單。如果這就是你所指的,那麼在下拉菜單中至少沒有選項「編譯器和調試器」(至少)。我在Compiler> Search directories> Linker> Add中添加了「/ usr/lib/mysql」,但沒有解決問題。 – user124384 2015-06-20 21:16:47

2

用命令編譯您的應用程序波紋管

gcc -o test -L/usr/lib/mysql -lmysqlclient test.c 
+0

我應該使用CodeBlocks – 2012-03-10 11:56:00

+0

你得到了解決方案... – shofee 2012-03-10 13:13:40

3

這是僅適用於Linux環境:

In "Project build options" > "Linker settings" Tab > Under "Other linker options" add -lmysqlclient

enter image description here

您還需要添加使用mysql-connector-C-6.1.3-Linux的glibc2.5-x86_64的/有/「搜索目錄」>「編譯」

enter image description here

您還需要添加使用mysql-connector-C-6.1.3-Linux的glibc2.5-x86_64的/ lib目錄/「搜索目錄」>「接頭」

enter image description here

對於Windows:

-lmysql

MySQL連接庫可以在這裏找到:http://dev.mysql.com/downloads/connector/c/

+0

嘗試了多種其他解決方案後,這是唯一對我有用的東西。 – user124384 2015-06-20 21:19:23