简体   繁体   中英

MySQL and C : undefined reference to `_mysql_init@4'|

I'm trying to write a simple script for C to get values from a MySQL database, but it's throwing this error 'undefined reference to `_mysql_init@4''

Don't know if I'm not linking to something I should be? My C knowledge is limited...

I'm using Code Blocks on Windows, here's my code:

#include <winsock.h>
#include <C:\mysql\include\mysql.h>
#include <stdio.h>
#include <string.h>

int main()
{
   MYSQL mysql;
   MYSQL_RES *res;
   MYSQL_ROW row;

   char query[80];
   mysql_init(&mysql);
   mysql_real_connect(&mysql,"localhost","user","pass","db",0,NULL,0);
   sprintf(query,"SELECT src,dst FROM ipaudit");
   mysql_real_query(&mysql,query,(unsigned int)strlen(query));
   res = mysql_use_result(&mysql);
   while(row = mysql_fetch_row(res))
    printf("%s %sn",row[0],row[1]);
   mysql_free_result(res);
   return 0;
}

That's a linker error, indicating that the linker can't find the function mysql_init .

Make sure you are linking to libmysql.lib or mysqlclient.lib . You also need to include <my_global.h> when building on Windows (see that same page in the MySQL Manual).

undefined reference refers to a problem with the linker. The function mysql_init() is not part of your code and is already compiled in a library. You have to tell the linker to include the code for that function by specifying the library where said code is.

I don't know how to specify libraries in Code Blocks, sorry

Edit

A quick Google search for how to specify libraries in Code Blocks returned a interesting result :)

使用32位连接器: http : //www.mysql.com/downloads/connector/c/,并且不要忘记在#include <mysql.h>之前#include <mysql.h> #include <windows.h> #include <mysql.h>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM