Join us at IRC!
It is never to LATE to become what you never WERE.
Thursday, May 24, 2012
Navigation
Members Online
Total Online: 37
Web Spiders: 14
Guests Online: 30
Members Online: 7

Registered Members: 70203
Newest Member: monkiman
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

MySQL + C Problem

chess_rock
Member



Posts: 243
Location:
Joined: 20.02.08
Rank:
God
Posted on 06-09-09 22:02
Hey there,

I've been recently struggling to program something simple using C and Mysql but i'm having lots of trouble. I've googled up and down, and i can't make the following code work properly:

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

int main(void)
{
MYSQL con;

mysql_init(&con);
if ( mysql_real_connect(&con, "localhost", "user", "pass", "db", 0, NULL, 0) )
{
printf("Connected!\n");
mysql_close(&con);
}
else
{
printf("Not connected\n");
printf("Error %d : %s\n", mysql_errno(&con), mysql_error(&con));
}
}


I don't really know if the code is wrong or if the mysql library was not correctly installed... So here is the error that appears when i try to compile the program:

gcc -o trem.out trem.c
/tmp/ccORzxsv.o: In function `main':
trem.c:(.text+0x2a): undefined reference to `mysql_init'
trem.c:(.text+0x70): undefined reference to `mysql_real_connect'
trem.c:(.text+0x8e): undefined reference to `mysql_close'
trem.c:(.text+0xaa): undefined reference to `mysql_error'
trem.c:(.text+0xba): undefined reference to `mysql_errno'
collect2: ld returned 1 exit status


I re-installed the library but it seems that it is not the problem. It works when i write a code without a parameter like:

MYSQL con

Can anyone help me please? :)
Author

RE: MySQL + C Problem

p4plus2
Member

Posts: 167
Location:
Joined: 31.03.08
Rank:
God
Posted on 06-09-09 23:35
Try compiling it like this:

gcc -o trem -L/usr/lib/mysql -lmysqlclient trem.c



"You can't be something your not,
Be yourself by yourself
Stay away from me" ~Walk, Pantera

"Playing an acoustic guitar is like having sex with your clothes on" ~Dave Mustaine
p4plus2@hotmail.com
Author

RE: MySQL + C Problem

chess_rock
Member



Posts: 243
Location:
Joined: 20.02.08
Rank:
God
Posted on 07-09-09 17:30
Thank you a lot! It worked :)

I have one question about all this... Why do i have to compile this way? Because if it worked it means that the library was correctly installed, right? Shouldn't the #include <mysql/mysql.h> solve the problem?
Author

RE: MySQL + C Problem

mastergamer
Member



Posts: 432
Location:
Joined: 07.02.06
Rank:
God
Posted on 07-09-09 18:34
chess_rock wrote:
Thank you a lot! It worked :)

I have one question about all this... Why do i have to compile this way? Because if it worked it means that the library was correctly installed, right? Shouldn't the #include <mysql/mysql.h> solve the problem?


You still need to link against the mysql library, otherwise your program wouldn't know where to find the code for the mysql functions.

Including the header tells GCC what the functions look like (return type, arguments etc), not where the actual code is.


mastergamer0168@gmail.com
Author

RE: MySQL + C Problem

chess_rock
Member



Posts: 243
Location:
Joined: 20.02.08
Rank:
God
Posted on 07-09-09 18:40
Thank you :)
Guest
Username

Password

Remember Me


Bookmark This Page
Affiliates
Adverts

 

 

Links
By using, viewing or obtaining any information contained on this site, you agree to the disclaimer.

© HellBound Hackers 2008- 2009. Since 3rd December 2004.