problem wit MySQL C Api

Hi:

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.

Cheers

Garry

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;
MYSQL_RES *res;
MYSQL_ROW row;
char *query;

int t,r;

mysql_init(mysql);
if (!mysql_real_connect(mysql,"localhost","mysql",
    "mysql","temp",0,NULL,0))

{
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”,
mysql_error(mysql));
}
else printf(“Query made…\n”);
res=mysql_use_result(mysql);
for(r=0;r<=mysql_field_count(mysql);r++)
{
row=mysql_fetch_row(res);
if(row<0) break;
for(t=0;t<mysql_num_fields(res);t++)
{
printf("%s “,row[t]);
}
printf(”\n");
}
mysql_close(mysql);
}

As thegman mentioned, you link command line is incorrect.
I’ve just tried your code with mysql from qnxzone.com/~fliu/ 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.

Cheers

Garry

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