can`t get mysql functions working in c++

This is a discussion on can`t get mysql functions working in c++ within the C++ Programming forums, part of the General Programming Boards category; Hello, first off.. i used the search function and found problems that are about the same.. only no solution is ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    3

    can`t get mysql functions working in c++

    Hello,

    first off.. i used the search function and found problems that are about the same.. only no solution is working for me..

    i`m new to programming c++ and i mainly want to learn it to connect to a mysql database and show infor from the database.. so it`s just normal to start with a simple mysql.cpp script that just makes a connection to mysql and shows if it`s connected or not.. but the compiling itself is going wrong..

    This is the script:
    Code:
    #include <windows.h>
    #include <iostream>
    #include "mysql.h"
    using namespace std;
    
    int main()
    {
    	MYSQL mysql;
    
    	mysql_init(&mysql);
    	mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"your_prog_name");
    	if (!mysql_real_connect(&mysql,"host","user","passwd","database",0,NULL,0))
    	{
    	    fprintf(stderr, "Failed to connect to database: Error: %s\n",
    	          mysql_error(&mysql));
    	}
    	else
    	{
    		cout << "FINALLY CONNECTED" << endl;
    	}
    	return 0;
    }
    so the script seems to be fine.. no errors there.. than i try to compile it.. this is the error that i get (i`m compiling on Windows XP SP2 in the commant prompt.)
    Code:
    C:\Programming\mysql_connection>g++ mysql.cpp -o mysql.exe
    C:\DOCUME~1\Mark\LOCALS~1\Temp/ccW8caaa.o(.text+0x13d):mysql.cpp: undefined refe
    rence to `mysql_init@4'
    C:\DOCUME~1\Mark\LOCALS~1\Temp/ccW8caaa.o(.text+0x15e):mysql.cpp: undefined refe
    rence to `mysql_options@12'
    C:\DOCUME~1\Mark\LOCALS~1\Temp/ccW8caaa.o(.text+0x1a7):mysql.cpp: undefined refe
    rence to `mysql_real_connect@32'
    C:\DOCUME~1\Mark\LOCALS~1\Temp/ccW8caaa.o(.text+0x1bc):mysql.cpp: undefined refe
    rence to `mysql_error@4'
    collect2: ld returned 1 exit status
    
    C:\Programming\mysql_connection>
    i tried to get it working with about all they say here: http://dev.mysql.com/doc/refman/5.0/...compiling.html but no luck.. and -lmysql is also one of the options i tried..

    so do any of you guys know what i might do wrong?

    Thanx alot.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    These are the type of errors you get when you haven't linked in the appropriate library file.

    In your source files, you should include my_global.h before mysql.h:

    #include <my_global.h>
    #include <mysql.h>

    my_global.h includes any other files needed for Windows compatibility (such as windows.h) if you compile your program on Windows.

    You can either link your code with the dynamic libmysql.lib library, which is just a wrapper to load in libmysql.dll on demand, or link with the static mysqlclient.lib library.

    The MySQL client libraries are compiled as threaded libraries, so you should also compile your code to be multi-threaded.
    Looks like maybe you should have tried -lmysqlclient instead of -lmysql but that's just a guess, I've never worked with compiling programs that use MYSQL.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    May 2006
    Location
    Berkshire, UK
    Posts
    29
    You need to use -lmysqlclient

    (I use it all the time with no problems)

    Have you looked at mysql++? Excellent libraries and they take all the work out of MySQL programming. You can find them on the MySQL web site, with full documentation. The tutorial takes you through most of it. If yu are unsure, I am often around on this board.

    Michael.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    3
    i didn`t look at mysql++ beause mysql has a api of it`s own and i want to use that one

    thanx for the replies... i will test the options and post here if it worked or not.

  5. #5
    Registered User
    Join Date
    May 2006
    Location
    Berkshire, UK
    Posts
    29
    mysql++ is a c++ wrapper for the C API - sooooo much easier than the simple C approach. Using the standard C api just means that you have to write the C++ code yourself... Up to you though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static member functions more efficient?
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2006, 07:07 AM
  2. working with functions
    By robasc in forum C++ Programming
    Replies: 13
    Last Post: 12-27-2005, 12:38 PM
  3. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  4. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  5. Passing data/pointers between functions #2
    By TankCDR in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 08:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21