Compile MySQL Client under cygwin

I want to thank to Cameron Chenier for writting this guide in first place. I found it very helpful and wanted to share it in my site.

C program interfacing with MySQL by Cameron Chenier ([email protected])

Cameron used:

Leandro used:



The following is required when compiling a c program that requires a link to a mysql database all on a Windows machine. There are other ways of accomplishing this task. This method will use cygwin in order to use gcc. A libmysqlclient is used to connect to the database. However cygwin must compile this file from the source code of mysql, or we'll have problems with it. So what we have is Mysql installed on the Windows machine using the Windows Binaries from the website. In order to get the libmysqlclient.a we must compile the mysql source code (NOT the Windows code) using gcc through cygwin. Following the steps below assumes you have Mysql server install on a windows machine. Cygwin is installed with the gcc, g++, and make packages.


  1. Download source for mysql from
    Get the linux source (tar.gz). This is at the end of the download webpage
  2. Untar mysql source (ex. C:\mysql-src)
  3. Open cygwin
  4. Configure mysql install. Do this with "--without-server" option
    1. %>cd /cygdrive/c/mysql-src/mysql-4.1.16
    2. %>./configure --without-server
  5. Make and Install mysql (this will NOT effect the Windows Mysql Server install)
    1. %>make
    2. %>make install
  6. The important files for the mysqlclient are:
    • /usr/local/lib/mysql/libmysqlclient.a
    • /usr/local/include/mysql/mysql.h

    These files may be in different locations depending on your install. The other common location of these files will be /usr/lib/mysql and /usr/include/mysql. This will come into play when compiling your mysqlclient program.
  7. Test file to ensure it all works:
    1. Create a file with this content:
      #include <mysql.h>
      int main() {
      	MYSQL conn;
      	return 0;
    2. Compile with following options
      -I/usr/local/include/mysql -L/usr/local/lib/mysql
    3. In example:
      gcc test.c -o test -I/usr/local/include/mysql -L/usr/local/lib/mysql \
  8. Possible problems:
    I ran into a problem while compiling my test program with my_compress.o missing from the libmysqlclient.a library
    undefined reference to "_compress"

    Solution: to all the .o file into the gcc compile line. The file is in the libmysql directory in the mysql source directory.
    gcc test.c -o test /cygdrive/c/mysql-src/mysql-4.1.16/libmysql/my_compress.o


If you've found this to be an useful document, don't forget to share it.