Jun 1, 2005

MySQL, mysqlclient.lib, Visual C++, Windows XP

It's really hard to find related information of compiling programs with MySQL static library mysqlclient.lib. Fortunately, I got it!
There are two ways to link MySQL library, use libmysql.dll and mysqlclient.lib.

Use libmysql.dll:
Files libmysql.lib and libmysql.dll are at c:\program files\mysql\mysql server 4.1\lib\opt.
Just add libmysql.lib to your project. libmysql.lib is only a wrapper of libmysql.dll. Therefore, your program must run with libmysql.dll. The run-time library can be single thread version (/ML) or multi thread version.

Use mysqlclient.lib:
mysqlclient.lib is at same dictionary above. Add mysqlclient.lib to your project. If you build your project and get a lot of linker errors (LNK2001), you need to add some lib files to your project. Make sure that your project links wsock32.dll, advapi32.dll, and mysqlclient.lib. Open the propertity page of your project, click Linker options, click "input", click "other dependency", add .lib files as you need. You have to use multi-thread run-time library(/MT). The command line looks like
/OUT:"Release/LGP.exe" /INCREMENTAL:NO /NOLOGO /LIBPATH:"C:\Program
Files\MySQL\MySQL Server 4.1\lib\opt" /DEBUG /PDB:"Release/LGP.pdb"
/SUBSYSTEM:CONSOLE /OPT:REF /OPT:ICF /LTCG /MACHINE:X86 mysqlclient.lib wsock32.lib kernel32.lib
user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib
shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib

If you open libmysql.dll at depends, the tool comes with Visual C++, you will see libmysql.dll depends on three DLLs: wsock32.dll, kernel32.dll, and advapi32.dll. That's why Mr. Armin Schoeffmann this web page:
http://www.issociate.de/board/post/182627/Linker_error_LNK2001_with_vc++6.0_and_mysqlclient.lib.html
suggests add advapi32.lib to LINK32_FLAGs. However, in my case, I lack of wsock32.lib not advapi32.lib.

If you are using Borland C++ Builder, you can use the utility IMPLIB to make your own libmysql.lib file, and use libmysql.dll to connect MySQL. Unfortunately, I don't know how to let BCB users use mysqlclient.lib.
If you have any question, please let me know.

No comments: