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.
User Comments
I solved some of those link problems (using gcc
on Win2K) with these options:
-lm -lwsock32 -luser32 -lgcc
However, I still get an unresolved _chkstk symbol
in mysqlclient.lib.
It took me a couple of hours to iron the problems
out, but I succeeded in compiling a client under
Microsoft Visual C++ version 6.0. mysql_com.h
causes problems with sockets unless winsock.h is
included beforehand. Also, VC++ included a library
that caused conflicts with mysqlclient.lib, which
I took care of with a directive to the linker to
ignore that library (I'm not at the lab anymore,
so I can't look up the exact name of that library;
if you run into the same problem I did, just add
the directive that the linker suggests when it fails).
the linker problem I have solved.
see
www.dvrsol.com/programming.html#mymingw
here you can download client library for mingw
compiler
Previous comment permit to compile a MySQL client using libmysql.dll file under GCC for Windows (Dev-C++). But if you want to use mysqlclient.lib you need more manipulation.
Tools to use :
- dlltool.exe which comes with GCC
- pexports.exe from http://starship.python.net/crew/kernr/mingw32/Notes.html for example
1) First you need to create de def file from your ntdll.dll :
pexports c:\winnt\system32\ntdll.dll > c:\mylibs\ntdll.def
This command line extract each exports from ntdll.dll and put its in ntdll.def. If you prefer more complex command line do
echo EXPORT > c:\mylibs\ntdll.def
nm c:\winnt\system32\ntdll.dll | grep " T _" | sed "s/.* T _//" >> c:\mylibs\ntdll.def
It's same.
2) You need to create a .a file for gcc from ntdll.dll and ntdll.def
dlltool --input-def c:\mylibs\ntdll.def --dllname ntdll.dll --output-lib c:\mylibs\libmyntdll.a
Don't name the .a file as libntdll.a because standard libs of GCC include libntdll.a. But this version seem to don't content chkstk function symbol...
3) Now configure your GCC (I'm using Dev-C++) :
Library directory to include :
c:\mylibs
Liker directives :
c:\mysql\lib\mysqlclient.lib -lws2_32 -lmsvcrt20 -lmyntdll
These directives permit you to incule mysqlclient.lib in link process which need libws2_32.a form Windows Socket API, libmsvcrt20.a because mysqlclient.lib was compiled with MS Visual C++ and libmyntdll.a for _chkstk function...
For moment I don't have tested all function of mysqlclient.lib to be sure of no errors but I'm think it's good :))
Good luck...
Note : your program need ntdll.dll (which is part of Windows NT/2000/XP), msvcrt20.dll and ws2_32.dll (which comes with Windows 2000/XP).
Olivier
in mingw32 and cygwin compiled a MySQL client using libmysql.dll file,
please create an def files,use:
reimp -d c:\mysql\lib\opt\libmysql.lib
***
do this case please do not use the pexport!!
***
and use libmysql.def created by reimp in dlltool,
dlltool --input-def x:\path\libmysql.def --dllname libmysql.dll --output-lib x:\path\libmysql.a
copy libmysql.a to x:\mingw\lib <<--!!needed!!
in mingw32 added -lmysql and -lws2_32 and -lmsvcrt20.
Enjoy,
HeartIcy.
reimp http://prdownloads.sourceforge.net/mingw/mingw-utils-0.2.tar.gz?download
The advice of Monaco Olivier made my application really compile instead of stopping in a linker error. Monaco Olivier you are a genius.
After hours and then days of scouring the web for a solution I was unable to find a way to statically link libmysql.lib with mingw. However, I have found a simpler solution to compile with the dll.
I used mysys, however the same instructions should work from a command prompt, though you will need to adjust the path structure to standard windows (ie: c:\Programs instead of /c/Programs)
-------------
cd /c/Programs/mysql-4.1.12-win32/lib/opt
reimp libmysql.lib
mv LIBMYSQL.def libmysql.def
dlltool --input-def libmysql.def --dllname libmysql.dll --output-lib mysql.a
mv liblibmysql.a libmysql.a
-------------
Add the following path to your IDE's compiler directories:
C:\Programs\mysql-4.1.12-win32\lib\opt
--or--
Add the following line to your compile script/command:
-LC:\Programs\mysql-4.1.12-win32\lib\opt
Add the following to your IDE's link library:
mysql
--or--
Add the following line to your compile script/command:
-lmysql
Copy libmysql.dll to your program's directory, or to your path (c:\WINDOWS\system32\libmysql.dll).
This is what my compile line and my link line look like:
mingw32-gcc.exe -IC:\Programs\mysql-4.1.12-win32\include -IC:\Programs\MinGW\include -c mytest.c -o .objs\mytest.o
mingw32-g++.exe -LC:\Programs\mysql-4.1.12-win32\lib\opt -LC:\Programs\MinGW\lib -o U:\Programming\MySQLClient\MySQLClient.exe .objs\mytest.o -lmysql
I use the follows to make libmysql.a for mingw32:
1. reimp -d libmysql.lib
2. dlltool -k -d libmysql.def -l libmysql.a
and it's OK to my first hello-mysql program.
Tried static linking on visual Studio 2005 of mysqlclient.lib. Ended up with Windows sockets linker errors like this one
below
error LNK2001: unresolved external symbol _htons@4
Tried adding winsock library also but this increased the errors nonetheless.
Tried dynamic linking with libmysql, works perfect.
Regards,
Kavitesh Singh.
Add your own comment.