Thread: Loading libmysql.dll

  1. #1
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404

    Loading libmysql.dll

    I need to access a MySQL database using C. My compiler is MinGW.

    So what I have done is get my header files, lib files, and libmysql.dll.

    When I try and compile my project, obviously my linking is off. I know that my header files are fine because I managed to get access to the dll's functions the hardway using LoadLibrary() and such.
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <mysql/mysql.h>
    
    typedef MYSQL*(*mysql_init_imp)(MYSQL*);
    int main (void)
    {
    	mysql_init_imp mysql_init;
    	HINSTANCE hinstLib = LoadLibrary("libmysql");
    	if (hinstLib == NULL) 
    	{
    		printf("ERROR: unable to load DLL\n");
    		return 1;
    	}
    	
    	mysql_init = (mysql_init_imp)GetProcAddress(hinstLib, "mysql_init");
    	if (mysql_init == NULL) 
    	{
    		printf("Unable to load mysql_init()\n");
    		FreeLibrary(hinstLib);
    		return 1;
    	}
    
            FreeLibrary(hinstLib);
            return 0;
    }
    I do not want to do this for every function. I downloaded mysql-noinstall-5.1.30-win32 and am currently trying to figure out how the hell to link them correctly.
    I have tried and tried to find this on google but it seems everything I try is failing.

    I get the impression that all i need to link is mysqlclient.lib? Could anyone who has done this before push me along in the right direction?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you need to link your program with libmysqlclient.lib, which is usually found in the %SYSTEMDRIVE%\Program Files\SQL Server 5.x\lib folder. your include files are in %SYSTEMDRIVE%\Program Files\SQL Server 5.x\include. so just make sure that your compiler knows where to find those files, and tell the linker to include libmysqlclient.lib, put libmysql.dll somewhere in your windows path (%windir% - C:\Windows - works good), and it should work.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    I think that mingw don't use *.lib, but *.a files. Get the libmysql devpac library to do it easy (from www.devpaks.org). If you have already installed it just include the header files (as you have done in the code you posted) and link with libmysql.a (-lmysql).

    Code:
    #include <mysql/mysql.h>
    
    int main()
    {
    MYSQL mysql;
    if(mysql_init(&mysql))
        {
        //ready to connect
        mysql_close(&mysql);
        }
    return 0;
    }
    The devpak intallation will place the libmysql.dll file on the bin folder, so remember to distribute it with your program to make it work out the compiler environtment

    Hope that helps

    Niara
    Last edited by Niara; 12-28-2008 at 11:06 AM.

  4. #4
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Well that sounds like a plan. MinGW can use lib files, but I will give that a shot after I get off work. I hope version difference isn't a problem, because the devpak they have isn't quite up to the newest.

    Thank you both for your input.

  5. #5
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    OK, I finally got it to work.

    You were right about MinGW not using lib files. I found on the web a way to convert lib files to a files, but I never got it right, following the tutorials word for word. Then by chance I decided to change the extension on libmysql.lib to libmysql.a and voila, it worked.

    I only ever ended up needing to link libmysql.a (-lmysql).

    Thanks guys!

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    1

    Thumbs up thanks!

    Hi,

    I tried accessing MySQL using MySQL++ which is really huge and complex stuff. I tested your sample programs and managed to compile successfully. I only needed to replace mysql_connect with mysql_real_connect as I'm using MySQL 5.0 and it works.

    mysql_real_connect(&mysql,"localhost","userid","pa ssword","daabase",3306,"",0)

    Can you post more of your sample codes here as I'm learning c++ and wrapping libmysql.dll.
    Last edited by mcwong644; 08-13-2009 at 10:18 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cargo loading system
    By kyle rull in forum C Programming
    Replies: 1
    Last Post: 04-20-2009, 12:16 PM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Loading a bitmap (Without using glaux)
    By Shamino in forum Game Programming
    Replies: 7
    Last Post: 03-16-2006, 09:43 AM
  4. need help with .md3 file loading
    By Shadow12345 in forum Game Programming
    Replies: 2
    Last Post: 12-06-2002, 04:06 PM
  5. Loading Screen
    By pinkcheese in forum Windows Programming
    Replies: 2
    Last Post: 04-06-2002, 11:48 PM