?
Solved

How to solve Link Error connecting Mysql with C.

Posted on 2003-03-16
4
Medium Priority
?
662 Views
Last Modified: 2007-12-19
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
Comment
Question by:saraj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 8146195
You probably have NOT included the MySQL client libraries in your link process.
0
 
LVL 1

Accepted Solution

by:
cybeonix earned 300 total points
ID: 8147351
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
 
LVL 20

Expert Comment

by:jmcg
ID: 10142660
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question