Click to See Complete Forum and Search --> : Include file for..


goon12
04-05-2002, 12:52 AM
I am trying to connect to mysql db that is sitting on the local machine.. I tried #include "mysql.h" and the file was sitting the working directory.

I am getting this error:
cc test_db.c -o test_db
/tmp/ccI7b81q.o: In function `main':
/tmp/ccI7b81q.o(.text+0x20): undefined reference to `mysql_connect'
/tmp/ccI7b81q.o(.text+0x42): undefined reference to `mysql_select_db'
/tmp/ccI7b81q.o(.text+0x59): undefined reference to `mysql_query'
/tmp/ccI7b81q.o(.text+0x6b): undefined reference to `mysql_store_result'
/tmp/ccI7b81q.o(.text+0x8b): undefined reference to `mysql_fetch_row'
/tmp/ccI7b81q.o(.text+0xcb): undefined reference to `mysql_num_fields'
collect2: ld returned 1 exit status
make: *** [test_db] Error 1

What am I doing wrong? Here is the simple simple program:
#include "mysql.h"
#include<stdlib.h>
#include<stdio.h>

int main()
{
MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;

if (!(mysql_connect(&mysql, "xxx.xxx.xxx.xxx", "xxx", "xxxx")))
{
abort();
}

mysql_select_db(&mysql, "MyDataBase");

mysql_query(&mysql, "select id, username, email from user_accounts");

res = mysql_store_result(&mysql);

while((row = mysql_fetch_row(res)))
{
uint i = 0;

for(i=0; i < mysql_num_fields(res); i++)
{
printf("%s\n", row[i]);
}
}

exit(0);
}


Thanks,
goon12

Energon
04-05-2002, 01:41 AM
You need to link to the mysql libraries. It may be -lmysql or something else, and it may require that you -L/path/to/mysql/libraries (you should check the mysql documentation and website to find out for sure).

Just in case you didn't know, those type of errors (where it says the error is in a .o file) are linker errors rather than compiler errors. It's trying to find where the actual functions are defined in a library or one of your object files, but it just can't find it and bails.

[ 05 April 2002: Message edited by: Energon ]

goon12
04-05-2002, 09:20 AM
Cool, thanks for the info.

Thanks,
goon12

goon12
04-05-2002, 10:37 AM
Ok I went to the website and I am kinda stuck..I found a chapter in their docs, that shows the EXACT same erros I am getting, and it says the same about linking:
you should be able to solve this by adding -Lpath-to-the-mysql-library -lmysqlclient last on your link line.



I really dont understand what they are saying about "last on your link line". Im running mdk 7.2.

Am I supposed to do ln -L/path/to/mysql.h mysql.h ?

Thanks,
goon12

Stuka
04-05-2002, 12:36 PM
Your command line for compiling should look like this:
cc -o test_db -L/path/to/libmysqlclient -lmysqlclient test_db.c
The -L option tells gcc where to find the library files (that aren't in standard locations), and the -l option tells it to link your code against a specific library (libmysqlclient in this case, which is where your functions are defined).

goon12
04-05-2002, 04:04 PM
Ok thanks for the help, I think I *MAY* be a bit close.. now I got this error..
/usr/bin/ld: cannot find -lmysql.h
collect2: ld returned 1 exit status

I am actually doing this on a webserver.. which may not be the best thing. I will try it on my other Linux box later if I still haven't got it right.

Thanks again,
goon12

Stuka
04-05-2002, 05:38 PM
The -l option is for the linker, not the compiler. You need to give it the name of the dynamic library to link with. In this case (according to the docs you quoted), its libmysqlclient. The 'lib' part is automagically supplied by gcc, so you need-lmysqlclient in your command line, not -lmysql.h