MySQL’s User-Defined Functions (UDFs) allow you to link code libraries into the database server, and I was excited to learn this week that Jan Kneschke has published a UDF which uses Sean Chittenden’s libmemcache library to connect to memcached. My next post will be all about using the memcache UDFs along with triggers in MySQL 5. First, though, I wanted to clarify a little bit about how to compile UDFs on Mac OS X.

OS X is based on the Mach executable format, and Apple’s compiler is a slightly different version of gcc than anyone else’s. The MySQL manual tells you to use gcc -shared to compile a UDF, like this:

gcc -shared -o udf_example.so udf_example.cc

However, Apple’s gcc doesn’t support the -shared option. So how do you create the shared library? On OS X, I use one command to compile the UDF source file, and then a second command to link it as a loadable shared object. You can compile Jan’s memcache UDF using gcc, like this:

cc -c -DDBUG_OFF -O2 `mysql_config –cflags` udf_memcache.c

And then, to create the shared object file, use Apple’s libtool utility:

libtool -dynamic -o udf_memcache.so udf_memcache.o -lmemcache -lc

This creates a udf_memcache.so file that can be loaded into the “max” version of a MySQL server.