Colin Charles Agenda

Compiling MySQL UDFs on Mac OS X

Compiling and installing a User Defined Function for MySQL on Mac OS X seems tricky. There are installation notes, but they seem to be sparse on OS X (the comments are clues, though).

I was looking through the tutorial materials for Roland’s talk, and came up with what I think is the most full-proof way to ensure your UDFs get compiled…

gcc -Wall -dynamiclib -o udf_lightspeed.dylib -lstdc++ udf_lightspeed.c

The above will compile just fine, but MySQL will give you an interesting error saying “no suitable image found”. Its the infamous Error 1126.

Upon further poking, it seemed like the following should work:
gcc -Wall -dynamiclib -o udf_lightspeed.dylib -lstdc++ -lc -I`/usr/local/mysql/bin/mysql_config --cflags` udf_lightspeed.c

And it does. MySQL loads the UDF just fine. But sometimes, you get linker errors, which can be annoying.

So, the full-proof solution to compiling UDFs on OS X, Leopard 10.5? A two-step process:
gcc -c `/usr/local/mysql/bin/mysql_config --cflags` udf_lightspeed.c
libtool -dynamic -o udf_lightspeed.so udf_lightspeed.o -lc

Feel free to add -O2 as an option to GCC. One day I might talk about the amazing mysql_config (use it! Read the documentation, in the meantime.)

Remember, once you’ve your your .dylib, to move it to your plugin_dir (find it by doing SHOW VARIABLES LIKE 'plugin_dir';). Also note that on OS X, it doesn’t matter if the extension is .dylib or .so – either one will do.

Happy UDF usage!

Update: As a reader points out in the comments, probably the easiest way, in a one-step process, is to use the bundle_loader. gcc -Wall -bundle -bundle_loader /usr/local/mysql/bin/mysqld -o udf_return_values.so `/usr/local/mysql/bin/mysql_config —cflags` udf_return_values.c works a charm.

Technorati Tags: , , , , , ,