Friday, June 01, 2007

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



Oldfox said...

Thanks for your post, I fixed my problem

Usman said...

Thanks...using -lmysqlclient in my Makefile solve problem for me.

htun htun said...

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

salahuddin66 said...

I think, there should not be space between the '=' sign.

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

htun htun said...

Thx for your reply but I still got the same error. Is there anyway to overcome this problem?

htun htun said...

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)

rdivya said...

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.

salahuddin66 said...

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.