• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 684
  • Last Modified:

How to solve Link Error connecting Mysql with C.

Linking...

SAMPCODE.OBJ(c:\raj\cprog\db\sampcode.c) : error L2029: '_mysql_select_db' : unresolved external
SAMPCODE.OBJ(c:\raj\cprog\db\sampcode.c) : error L2029: '_mysql_query' : unresolved external
SAMPCODE.OBJ(c:\raj\cprog\db\sampcode.c) : error L2029: '_mysql_free_result' : unresolved external
SAMPCODE.OBJ(c:\raj\cprog\db\sampcode.c) : error L2029: '_mysql_error' : unresolved external
SAMPCODE.OBJ(c:\raj\cprog\db\sampcode.c) : error L2029: '_mysql_store_result' : unresolved external
SAMPCODE.OBJ(c:\raj\cprog\db\sampcode.c) : error L2029: '_mysql_num_fields' : unresolved external
SAMPCODE.OBJ(c:\raj\cprog\db\sampcode.c) : error L2029: '_mysql_fetch_row' : unresolved external
SAMPCODE.OBJ(c:\raj\cprog\db\sampcode.c) : error L2029: '_mysql_connect' : unresolved external
SAMPCODE.OBJ(c:\raj\cprog\db\sampcode.c) : error L2029: '_mysql_close' : unresolved external

 LINK returned error code 2.
SAMPCODE.EXE - 9 error(s), 0 warning(s)

Below is the program ...

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

MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;

int main() {

       int i, count=1;
       char sqlcmd[1024];

       if (!(mysql_connect(&mysql,"localhost","root","password"))) {
               printf("Cant connect to MySQL\nREASON: %s\n", mysql_error(&mysql));
       }

       if (mysql_select_db(&mysql,"mysql")) {
               printf("Cant select database\nREASON: %s\n", mysql_error(&mysql));
       }

       printf("Databases:\n\n");

       sprintf(sqlcmd, "SHOW DATABASES");
       if (mysql_query(&mysql,sqlcmd)) {
               printf("Can't create sql query");
               exit(0);
       }

       if (!(res = mysql_store_result(&mysql))) {
               printf("Cant store data from sql query");
               exit(0);
       }

       while((row = mysql_fetch_row(res))) {
               for (i=0 ; i < (int)mysql_num_fields(res); i++) {
                       printf("%d: %s\n",count,row[0]);
                       count++;
               }
       }

       mysql_free_result(res);
    mysql_close(&mysql);

       printf("Finished..\n");
       
       return 0 ;
}

0
saraj
Asked:
saraj
1 Solution
 
gj62Commented:
You probably have NOT included the MySQL client libraries in your link process.
0
 
cybeonixCommented:
Make sure you are linking with the Link-Libraries provided for mySQL clients.

ie.

gcc myprogram.c -L/usr/lib/mysql -I/usr/include/mysql -lmysqlclient
0
 
jmcgOwnerCommented:
Nothing has happened on this question in more than 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by cybeonix.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now