7
#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;
}

and i get linker error in codeblocks:

undefined reference to mysql_init

I used mysql_config --libs in linker option and mysql_config --cflags in compiler option.

I read somewhere i should add some libraries like libmysql.lib, but i cannot find this file on my PC (I am using Ubuntu 11.04 64bit).

Behnam Safari
  • 2,941
  • 6
  • 27
  • 42

3 Answers3

5

This is for Linux environments only:

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

enter image description here

You will also need to add mysql-connector-c-6.1.3-linux-glibc2.5-x86_64/include/ in "Search directories" > "Compiler"

enter image description here

You will also need to add mysql-connector-c-6.1.3-linux-glibc2.5-x86_64/lib/ in "Search directories" > "Linker"

enter image description here

For windows:

-lmysql

MySQL Connector library can be found here: http://dev.mysql.com/downloads/connector/c/

Abdul Hamid
  • 189
  • 2
  • 4
  • After trying multiple other solutions, this is the only thing that worked for me. – user124384 Jun 20 '15 at 21:19
  • you should add mysqlclient to `link libraries`, not to `other linker options`, it's just more portable. code::blocks will automatically change `-l` to whatever the compiler supports when switching compiler :p – hanshenrik Mar 20 '19 at 16:51
3

compile your app with the command bellow

gcc -o test  -L/usr/lib/mysql -lmysqlclient test.c
shofee
  • 2,079
  • 13
  • 30
0

Setting -> Compiler and debugger -> Search directories -> Linker then Add and insert /usr/lib/mysql/

Behnam Safari
  • 2,941
  • 6
  • 27
  • 42
  • 1
    Where is "Setting"? There is a drop-down menu named "Settings." If that is what you were referring to, there is no option "Compiler and debugger" in that drop-down menu (anymore, at least). I added "/usr/lib/mysql" to Compiler > Search directories > Linker > Add, but that did not fix the problem. – user124384 Jun 20 '15 at 21:16