Last Updated: February 25, 2016
· guido

Compiling MySQL with Xcode 4.3 on OS X

Today I ran into an issue when running a bundle install on a project I've been working on for quite a while. Apparently MySQL does not play nice nowadays with Clang, Xcode's default compiler since 4.3.

An excerpt from my console:

> bundle install
Installing mysql2 (0.3.11) 
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

        /Users/guido/.rbenv/versions/1.9.3-p392/bin/ruby extconf.rb 
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for mysql.h... no
checking for mysql/mysql.h... no
mysql.h is missing.  please check your installation of mysql and try again.
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

In the mkmf.log file we have the following error:

have_header: checking for mysqld_error.h... -------------------- yes

"gcc -E -I/Users/guido/.rbenv/versions/1.9.3-p392/include/ruby-1.9.1/x86_64-darwin12.3.0 -I/Users/guido/.rbenv/versions/1.9.3-p392/include/ruby-1.9.1/ruby/backward -I/Users/guido/.rbenv/versions/1.9.3-p392/include/ruby-1.9.1 -I. -I'/Users/guido/.rbenv/versions/1.9.3-p392/include'  -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE  -I'/Users/guido/.rbenv/versions/1.9.3-p392/include'  -I/usr/local/Cellar/mysql/5.6.12/include  -Os -g -fno-strict-aliasing   -Wno-error=shorten-64-to-32 -pipe  conftest.c -o conftest.i"
checked program was:
/* begin */
1: #include "ruby.h"
3: #include <mysqld_error.h>
/* end */

Turns out that mysql_config has some compile switches that work on GCC, but not on Clang. Lines 119 & 120 are the culprits, with the -Wno-null-conversion and -Wno-unused-private-field switches:

cflags="-I$pkgincludedir  -Wall -Wno-null-conversion -Wno-unused-private-field -Os -g -fno-strict-aliasing -DDBUG_OFF " #note: end space!
cxxflags="-I$pkgincludedir  -Wall -Wno-null-conversion -Wno-unused-private-field -Os -g -fno-strict-aliasing -DDBUG_OFF " #note: end space!

Strip out the bad switches, and everything works fine:

cflags="-I$pkgincludedir  -Wall -Os -g -fno-strict-aliasing -DDBUG_OFF " #note: end space!
cxxflags="-I$pkgincludedir  -Wall -Os -g -fno-strict-aliasing -DDBUG_OFF " #note: end space!

Small note:

By default mysql_config is situated in /usr/local/mysql/bin/mysql_config. When using homebrew it's situated in /usr/local/Cellar/mysql/5.6.12/bin/mysql_config, where 5.6.12 is your installed version of course.