problem wit MySQL C Api


I’m trying to compile a program that use the MySQL C API, I compile my program using:

gcc -I/usr/local/mysql/include -L/usr/local/mysql/lib -lmysqlclient program -o program.c

I obtain the next errors:

/tmp/ccbOFwBQ.o: In function main': /tmp/ccbOFwBQ.o(.text+0xe): undefined reference to mysql_init’
/tmp/ccbOFwBQ.o(.text+0x34): undefined reference to mysql_real_connect /tmp/ccbOFwBQ.o(.text+0x4c): undefined reference to mysql_error’
/tmp/ccbOFwBQ.o(.text+0x9d): undefined reference to mysql_real_query' /tmp/ccbOFwBQ.o(.text+0xba): undefined reference to mysql_error’
/tmp/ccbOFwBQ.o(.text+0xec): undefined reference to mysql_use_result' /tmp/ccbOFwBQ.o(.text+0x108): undefined reference to mysql_field_count
/tmp/ccbOFwBQ.o(.text+0x120): undefined reference to mysql_fetch_row' /tmp/ccbOFwBQ.o(.text+0x140): undefined reference to mysql_num_fields’
/tmp/ccbOFwBQ.o(.text+0x198): undefined reference to `mysql_close’
collect2: ld returned 1 exit status

what I am doing wrong? or what else I have to do?

spidey, this is probably unrelated, but your line should be:

gcc -I/usr/local/mysql/include -L/usr/local/mysql/lib -lmysqlclient program.c -o program

if you’re sure that the library is indeed in the correct place, and that you have permissions to see it, then maybe there is a problem with the library, sometimes I get hassles (not just on QNX) with prebuilt libraries and often find I have more luck going from source, but this may not be the problem with your’s. Post a code snippet if you like, and I’ll see if it builds on my machines.



thegman, thanks for your help and I’ll appreciate if you test my code in your machines, the code is the next, I have a database named temp and a table named variables …

#include <mysql/mysql.h>
#include <stdio.h>

int main()
MYSQL *mysql;
char *query;

int t,r;

if (!mysql_real_connect(mysql,"localhost","mysql",

printf( “Error connectin ot database: %s\n”,mysql_error(mysql));
else printf(“Connected…\n”);

query=“select * from variables”;

t=mysql_real_query(mysql,query,(unsigned int) strlen(query));
if (t)
printf(“Error making query: %s\n”,
else printf(“Query made…\n”);
if(row<0) break;
printf("%s “,row[t]);

As thegman mentioned, you link command line is incorrect.
I’ve just tried your code with mysql from and it links without any errors.

I just downloaded and installed the mysql that noc mentions, and your code above compiles and links no problems with the following:

gcc -I/opt/mysql/include -L/opt/mysql/lib/mysql -lmysqlclient testmysql.c -o testmysql

I think your MySQL install might be duff, I got a pre-built MySQL package for Solaris and had the same problem as you, but I recall installing MySQL onto another Sun box from source, and it worked fine, so maybe a lot of the prebuilds have problems.



I’ve installed the MySQL 3.23.54a from the 3rd repository and I just build like this

qcc testmysql.c -o testmysql -lmysqlclient -lz -lsocket

I finally could compile my program. I used:

gcc myprogram.c -o myprogram -lmysqlclient -lz -lsocket -lm