Follow us on Twitter!
The important thing is not to stop questioning. - Albert Einstein
Monday, April 21, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 23
Guests Online: 22
Members Online: 1

Registered Members: 82856
Newest Member: djtonyg
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

MySQL + C Problem

chess_rock
Member



Posts: 244
Location:
Joined: 20.02.08
Rank:
Apprentice
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:

Code
#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:

Code
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

Your avatar

Posts: 167
Location:
Joined: 31.03.08
Rank:
Newbie
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: 244
Location:
Joined: 20.02.08
Rank:
Apprentice
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


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 07-09-09 18:34
chess_rock wrote:
Thank you a lot! It worked Smile

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.


Author

RE: MySQL + C Problem

chess_rock
Member



Posts: 244
Location:
Joined: 20.02.08
Rank:
Apprentice
Posted on 07-09-09 18:40
Thank you Smile