Mysql C API undefined reference to `mysql_init’

Today I was trying to compile simple program written in C to connect mysql

/tmp/ccetdBsn.o: In function `main’:
sqltest.c:(.text+0x35): undefined reference to `mysql_init’
sqltest.c:(.text+0x77): undefined reference to `mysql_real_connect’
sqltest.c:(.text+0x86): undefined reference to `mysql_error’
sqltest.c:(.text+0xbf): undefined reference to `mysql_query’
sqltest.c:(.text+0xce): undefined reference to `mysql_error’
sqltest.c:(.text+0xfc): undefined reference to `mysql_use_result’
sqltest.c:(.text+0x130): undefined reference to `mysql_fetch_row’
sqltest.c:(.text+0x144): undefined reference to `mysql_free_result’
sqltest.c:(.text+0x14f): undefined reference to `mysql_close’

I found some people also face this problem.

The problem solved after giving

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



  1. Hi I got this error even I put the link in Makefile.basic

    Here is my Makefile.basic

    CFLAGS = -g -Wall $(mysql_config –cflags) $(mysql_config –libs)


    Where it goes wrong ? help me please

  2. Hi I even tried this coz someone told me to try it. but no luck with this line too.
    CFLAGS=-g -Wall $(shell mysql_config –cflags) $(shell mysql_config –libs)

  3. hey
    i tried out the command u have given here, all the errors except one have been cleared for me
    undefined reference to `mysql_free_results' is stl existing.

    I know this is a pretty old post, i would appreciate any help at all.

  4. 1. It may be a version specific issue. You may need to check similar method in new API. Check your mysql header file.

    2. You "res = mysql_use_result(&mysql); " can be return null already. So, try with try catch block.

Leave a Reply

Your email address will not be published.