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 (cameron.chenier@cameronchenier.com)

Cameron used:

  • Windows 2k3 Server
  • MySQL version 4.1.16
  • cygwin 1.5.18

Leandro used:

  • Windows XP Professional
  • MySQL version 5.0.41
  • cygwin 1.5.24

Prerequisite:

  • Windows Installed
  • Mysql Installed using Windows Binaries (This is separate from the cygwinmysql install)
  • Cygwin installed with gcc, g++, make

Overview:

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 mysql.org 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.

Steps:

  1. Download source for mysql from www.mysql.org
    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 -lmysqlclient
    3. In example:
      gcc test.c -o test -I/usr/local/include/mysql -L/usr/local/lib/mysql \
      
      			-lmysqlclient
  8. Possible problems:
    I ran into a problem while compiling my test program with my_compress.o missing from the libmysqlclient.a library
    
    /usr/local/lib/mysql/libmysqlclient.a(my_compress.o):my_compress.c:(.text+0x5e):
    
    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

References:

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

Bookmark Compile MySQL Client under cygwin at Google Bookmark Compile MySQL Client under cygwin at del.icio.us Digg Compile MySQL Client under cygwin Technorati Compile MySQL Client under cygwin Bookmark Compile MySQL Client under cygwin at YahooMyWeb Bookmark Compile MySQL Client under cygwin at reddit.com Stumble It! Bloglines Compile MySQL Client under cygwin Bookmark using any bookmark manager! 
www.DRK.com.ar - Copyright 2002-2013 by DRK