-
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?
-
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.
-
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
-
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.
-
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!
-
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.