Search the MySQL manual:

2.6.1.6 Compiling MySQL Clients on Windows

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.

Note that because the MySQL client libraries are compiled as threaded libraries, you should also compile your code to be multi-threaded!

User Comments

Posted by Jacob Devore on Tuesday January 29 2002, @8:30am[Delete] [Edit]

I am trying to link with the mysqlclient.lib and
keep getting errors like these

mysqlclient.lib(libmysql.obj) : error LNK2001:
unresolved external symbol _WSAGetLastError@0

I'm running on win2k with vc6.

Posted by patrick on Friday February 8 2002, @7:54am[Delete] [Edit]

Yup, I have the same problem using the mingw compiler (gcc) under Win2k.

Posted by Thomas Koch on Sunday February 10 2002, @8:02am[Delete] [Edit]

I've the same Problem too. (LNK Error with
unresolved symbols in a VC++ 6.0 on Windows 98).
I had alredy try it to compile it multithreated
and to include the wondows.h in my application
but the problem remains. Can anyone give me a
good hint to solve this problem. (Is it necessary
to have a mysqlclient.lib compiled with the
VC++ ? or where one can get the source for the
library)

Posted by Dan Dickinson on Thursday February 21 2002, @8:46am[Delete] [Edit]

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.

Posted by [name withheld] on Monday March 11 2002, @6:14pm[Delete] [Edit]

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).

Posted by Dinesh V. R. on Monday December 9 2002, @12:16am[Delete] [Edit]

the linker problem I have solved.
see
www.dvrsol.com/programming.html#mymingw
here you can download client library for mingw
compiler

Posted by Monaco Olivier on Thursday February 27 2003, @2:41am[Delete] [Edit]

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

Posted by HeartIcy on Friday May 23 2003, @9:34am[Delete] [Edit]

How to Fix This Complier ERROR?

Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Examples\MySQLClientTest\Makefile.win"
Executing make clean
rm -f MySQLClientTest.o MySQLClientTest.exe

g++.exe -c MySQLClientTest.cpp -o MySQLClientTest.o -I"C:/Dev-Cpp/include/c++" -I"C:/Dev-Cpp/include/c++/mingw32" -I"C:/Dev-Cpp/include/c++/backward" -I"C:/Dev-Cpp/include" -fsave-memoized -O3

gcc.exe MySQLClientTest.o -o "MySQLClientTest.exe" -L"C:/Dev-Cpp/lib" -lstdc++ c:/mysql/lib/opt/mysqlclient.lib -lmyntdll -lws2_32 -lmsvcrt40 -lkernel32

Info: resolving __fltused by linking to __imp___fltused (auto-import)

c:/mysql/lib/opt/mysqlclient.lib(release/password.obj)(.text+0xa4):\src\mysqlwin-auto: undefined reference to `_ftol2'
c:/mysql/lib/opt/mysqlclient.lib(release/password.obj)(.text+0xd5):\src\mysqlwin-auto: undefined reference to `_ftol2'

c:/mysql/lib/opt/mysqlclient.lib(release/password.obj)(.text+0x93):\src\mysqlwin-auto: undefined reference to `_ftol2'
c:/mysql/lib/opt/mysqlclient.lib(release/password.obj)(.text+0xca):\src\mysqlwin-auto: undefined reference to `_ftol2'

make.exe: *** [MySQLClientTest.exe] Error 1

Execution terminated

Posted by HeartIcy on Monday May 26 2003, @2:34am[Delete] [Edit]

in mingw32 and cygwin compile a MySQL client using libmysql.dll file,

please a create de def files,use:

reimp -d c:\mysql\lib\opt\libmysql.dll

and use libmysql.def create 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

in mingw32 add -lmysql and -lws2_32 and -lmsvcrt20.

enjoy,
HeartIcy.

Add your own comment.