[+/-]
If you compile MySQL clients that you've written yourself or that
you obtain from a third-party, they must be linked using the
-lmysqlclient -lz options in the link command.
You may also need to specify a -L option to tell
the linker where to find the library. For example, if the library
is installed in /usr/local/mysql/lib, use
-L/usr/local/mysql/lib -lmysqlclient -lz in the
link command.
For clients that use MySQL header files, you may need to specify
an -I option when you compile them (for example,
-I/usr/local/mysql/include), so that the compiler
can find the header files.
To make it simpler to compile MySQL programs on Unix, we have provided the mysql_config script for you. See Section 4.7.2, “mysql_config — Get Compile Options for Compiling Clients”.
You can use it to compile a MySQL client as follows:
CFG=/usr/local/mysql/bin/mysql_config sh -c "gcc -o progname `$CFG --cflags` progname.c `$CFG --libs`"
The sh -c is needed to get the shell not to
treat the output from mysql_config as one word.
On Windows, you can link your code with either the dynamic or
static client library. The static library is named
mysqlclient and the dynamic library is named
libmysql.
If you link with the static library, failure can occur if certain conditions are not satisfied:
The client application must be compiled with exactly the same version of Visual Studio as that used to compile the library.
The client application should link the C runtime statically by
using the /MT compiler option.
If the client application is built in in debug mode and uses the
static debug C runtime (/MTd compiler option),
it can link the mysqlclient if that library
was built using the same option. If the client application uses
the dynamic C runtime (/MD option, or
/MDd option in debug mode), it cannot link with
the static client library and must use the dynamic library.
The MSDN page describing the link options can be found here: http://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx

User Comments
You should use "/usr/lib/mysql" path for libraries on RedHat Linux.
It would be great if you have a sort of RFC on the protocol.
the applications i develop for my company have to be cross platform, we work mostly under windows and macintosh.
For faster development, i dont use conventional compilers for my applications, for mac, i have to make a carbon version so it works with both OS X and OS 9, so the C compiler i use is codewarrior, and doesnt work with the mysql standard libraries (codewarrior can also compile for windows, so what i compile comes out for mac os 9, 10.2 and windows), and also, for SSL connections, i dont use openSSL, we use our own SSL classes, i have a SSL socket that supports SSL v1, 2 , 2-3 and 3 and TLS1, so, if i know the protocol for mysql, i would be able to build my own client library.
Perl DBD::mysql (v2.9003) wouldn't build with the default libraries output from `mysql_config` on SuSE Linux 8.2.
It failed with cannot find lnss_files.
I had to use:
perl Makefile.PL --libs="-L/usr/lib/mysql -lz -lmysqlclient"
The Borland linker does not like the "libmySQL.lib" library that comes with MySQL, it gives this error message:
Error: 'LIBMYSQL.LIB' contains invalid OMF record, type 0x21 (possibly COFF)
You can get around this problem by using the "dll" version of the library. Copy "libmySQL.dll" and execute the following two command lines in a DOS box:
implib libmySQL.lib libmySQL.dll
impdef libmySQL.def libmySQL.dll
This will create a new "libmySQL.lib" file which you can use in your projects. Works like a dream, your compiled program will automatically load "libmySQL.dll" when needed (make sure your program can find it, for example by placing a copy in the same directory as your program).
I found that if I were building a client in Visual C++ that it was best to download the source and build the .lib files on my machine. I believe I had many linking problems as a result of the pre-built libraries having been built by a newer version of Visual C++ (and perhaps using a newer version of the platform SDK.) In any event, if you are linking to mysqlclient.lib, you might need to build it first both for debug and release and its also possible you might need to link zlib.lib (and if you can build it as well; my source was missing files so I had to link to the pre-built zlib.lib.) To build mysqlclient.lib, download the source and look for the client folder. To build zlib.lib, download the source and look for the zlib folder.
Best of luck,
Compiling in Dev-Cpp on Windows
1. get the pexports.
2. execute pexports on mysql library (libmysql.dll):
pexports libmysql.dll > libmysql.def
3. with dlltool (comes with mingw:
dlltool -k --input-def libmysql.def --dllname libmysql.dll --output-lib libmysql.a
4. now, create a project, and in option\parameters\linker add -lmysql.
5. now, add the directories where are the mysql includes and lib (libmysql.a).
6. on linking, its has errors, as "mysql_init@4" not found. Enter in the libmysql.def and found mysql_init and put the @* that you are see. execute step 3 again.
7. now, its linking perfect, remember that the libmysql.dll need be in the ambient variable path, or in the windows\system32 or in directory of application that you are creating.
Bye
I'm using gcc with mingw (http://mingw.org/) on a Windows machine and so I had a few problems trying to use the libraries supplied with MySQL.
I have never before had to do this so it was all completely new to me but I couldn't seem to find anything floating around the comments and documentation. However after Googling for a while I came across this solution (pieced together from sites here and there) but basically go through these steps to create your own libmysqlclient.a that works with gcc :)
By the way, some of this might be the wrong terminlogy or whatev from my lack of experience here...
1. Copy the libmySQL.dll file from mysql folder/bin/ to the directory you're working in (or just use absolute paths of "C:\...") and run this line in gcc:
pexports libmySQL.dll | sed 's/^_//' > libmySQL.def
2. This will list all the function names that you can call using the dll.
3. Run this line (absolute addresses if necessary), the -k option is important!
dlltool --input-def libmySQL.def --dllname libmySQL.dll --output-lib libMySQLclient.a -k
4. Now that we have *a* libmysqlclient.a file we can try compiling the program with a line similar to the one below (revolution was the name of my program):
g++ revolution.cpp -lws2_32 -lmysqlclient -o revolution.exe
5. You will now see a undefined references for each of the functions you called that reside in the mysqlclient dll. After the functions there is also an @4 or @32 and so on, go back to your libmySQL.def file and add the @4 or @32 to the end of the functions. Next go back and repeat steps 3 + 4. (I don't understand why we have to do this though so -anyone wish to explain?)
...and voila! Compilation with gcc on a Windows machine!
Notes: don't forget to include the <winsock.h> header and link it to the ws2_32 library!
I have no clue about compiling from the command line, but if you use an IDE such as Code::Blocks/MinGW (www.codeblocks.org), there's an easy way to get this to work.
1) Make sure you have GCC 4.5.0 or above
2) Add Link libraries
...\MinGW\lib\libwsock32.a
...\MySQL\lib\opt\libmysql.dll
(MinGW can handle dll´s directly)
3) Add to other linker options
-Wl,--enable-stdcall-fixup
(to get rid of those @4 ... @32 errors)
4) Add search directory
...\MySQL\include
5) Make sure you include winsock before windows before mysql
#include <winsock2.h>
#include <windows.h>
#include <mysql.h>
6) Copy libmysql.dll to your projects executable.
As long as MinGW and MySQL are both 32bit or both 64bit, this works fine in about any version of Windows, up to Win7 64bit.
If you try to build the source code of mysql connector c or c++ with MinGW build environment on Windows, you might be interested in this post:
http://forums.mysql.com/read.php?117,425191,425191#msg-425191
Add your own comment.