C application with MySQL, built with NetBeans, on CygWinC application with MySQL, built with NetBeans, on CygWin

February 22, 2010
Tags: , , ,

Because MySQL became one of the most used database engine in the world and because of the ease of development in PHP with MySQL, most of the prototype application can be developed at first using this combination (PHP+MySQL). But then, mainly because of performance issues, one might want to implement that project in C – so there’s the need to connect from a C application to the MySQL server. Nothing easier – you say. MySQL provides us the mysql-connector that has a C implementation and binaries for the most important platforms. But I wanted to develop my application in a full-fledged IDE, and because my workstation runs Windows (7 – big fan) I needed to make it compile somehow on Windows. But the application will run on a Linux server (or Unix), so it had to be POSIX compatible. This took Visual Studio out of the equation.

So, to build a POSIX C application on Windows I recommend using NetBeans IDE (6.8 at this time) – it has lots of features, making it a good development environment. It has a C/C++ plugin implementing support for these two languages.

Now, to make it compile, I needed gcc, so I installed CygWin. It works fine on Windows 7 x64. When installing CygWin, you must make sure you install gcc (and all that it depends on), libncurses-devel, readline and libreadline (from the Devel category). Of course I didn’t unselect anything from the default list. I just added these libraries.

To make CygWin more friendly (I have this friendliness obsession 😉 ) I’m using PuTTYcyg, a great expansion of PuTTY. I am also editing the .vimrc file just like in the post for Solaris. The .bashrc file should exist and contain also the line


export PS1='[\u@\h \w]\$ '

Now, I’m having this Linux-like environment running on my Windows. All I have to do is to install the mysql-connector and to configure NetBeans to compile on CygWin.

To install mysql-connector on CygWin I had to download the sources of MySQL (not only mysql-client, I couldn’t compile it). MySQL includes the client when compiling. These are the steps to follow if you want mysql-client on CygWin

  • make sure you have gcc, libncurses-devel, readline and libreadline
  • download the mysql sources and extract them in a folder in CygWin
  • copy the file ttydefaults.h (from a working Unix system) in /usr/include/sys/
  • go into the sources folder and run ./configure –without-readline CFLAGS=-O2 (you can add –without-server, but I didn’t test that)
  • wait for it to finish (took me about 20 minutes). Hopefully you won’t have any errors
  • run make & make install and wait for it to finish (should take more than half an hour, but maybe your PC is faster)

This should be it. Of course, if you run configure and have a missing library error, install that library and rerun configure. If you have an error during compilation, make sure to run make clean before retrying!

Ok. Now to make sure that NetBeans compiles your C application successfully, follow these steps:

  • create a new C project and paste the following code

/*
 * File:   main.c
 * Author: Bogdan
 *
 * Created on February 18, 2010, 1:09 PM
 */

#include <stdio.h>
#include <stdlib.h>

#include <my_global.h>
#include <mysql.h>

int main(int argc, char** argv) {

 printf("Running just fine!\n");
 printf("MySQL client version: %s\n", mysql_get_client_info());

 return (EXIT_SUCCESS);
}

  • go to Tools->Options and select C/C++ tab. Click the “Add” button and select the “bin” folder of your CygWin installation (mine is C:\cygwin\bin). The other two fields should auto-complete. Click OK, make sure all paths are correct and then click OK again.
  • right-click your project in Projects pane. Go to Properties
  • in the Build->C Compiler section, add the value “c:/cygwin/usr/local/include/mysql” in the “Include directories” field (supposing that you installed CygWin in C:\cygwin)
  • in the Build->Linker section, add the value “c:/cygwin/usr/local/lib/mysql”
  • in the same section (Linker) click on the button near “Libraries”, click “Add Library”, go to “/usr/local/lib/mysql” and add “libmysqlclient.a”
  • after clicking “Select”, press again “Add Library”, go to “/lib” and add “libz.a”
  • click OK and then OK again

Now your application should compile and run successfully. So now I’m ready to develop and test a C application with MySQL connection on my PC and then to deploy it on a UNIX server (of course it will need to be recompiled).

Be Sociable, Share!

Leave a Reply




*