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

http://www.ucl.ac.uk/is/mysql/c/
http://www.osix.net/modules/article/?id=517

/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

src: http://lists.mysql.com/mysql/200178

8 comments:

Zlabs 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

CC=/usr/bin/gcc
#CC=powerpc-linux-gcc
CP=/usr/bin/cp
CFLAGS = -g -Wall $(mysql_config --cflags) $(mysql_config --libs)

www=/var/www
htdocs=/htdocs
cgi_bin=/cgi-bin
config=/etc/apache2/sites-available/default

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...

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.

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.