2011/02/02

CentOS4 and gcc4

Say you are merrily compiling someone else's code on CentOS4 and you get something resembling the following:
query_response_time.cc: In function `void query_response_time::add_time_atomic(query_response_time::TimeCounter*, uint64)':

query_response_time.cc:160: error: `__sync_fetch_and_add' was not declared in this scope
query_response_time.cc: In member function `void query_response_time::time_collector::collect(uint64)':
query_response_time.cc:261: error: `__sync_fetch_and_add' was not declared in this scope
make[3]: *** [query_response_time.o] Error 1
make[3]: *** Waiting for unfinished jobs....
make[3]: Leaving directory /usr/src/redhat/BUILD/Percona-Server/sql'
make[2]: *** [all-recursive] Error 1
make[2]: Leaving directory /usr/src/redhat/BUILD/Percona-Server/sql'
make[1]: *** [all] Error 2
make[1]: Leaving directory /usr/src/redhat/BUILD/Percona-Server/sql'
make: *** [all-recursive] Error 1
error: Bad exit status from /var/tmp/rpm-tmp.49946 (%build)

You curse and swear. __sync_fetch_and_add is part of Intel's Itanium ABI and just plain isn't implemented in GCC 3, the default compiler in CentOS 4.

But the solution is easier then you think:
yum install gcc4 gcc4-c++

export CC=gcc4 CXX=c++4

OK, so maybe it's harder then that; you will probably have to add the following to your spec file:
BuildRequires: gcc4 gcc4-c++

%define cc_cmd ${CC:-gcc4}
%define cxx_cmd ${CCX:-g++4}

And replace any hardcoded reference to gcc or c++ with %{cc_cmd} or %{cxx_cmd}.

__sync_fetch_and_add is just one of a whole bunch of builtins not available to CentOS 4's (aka Red Hat Enterprise Linux 4, aka RHEL4) default version of gcc. In the interest of getting this solution into Google, here is the full list: __sync_fetch_and_add, __sync_fetch_and_sub, __sync_fetch_and_or, __sync_fetch_and_and, __sync_fetch_and_or, __sync_fetch_and_and, __sync_fetch_and_xor, __sync_fetch_and_nand, __sync_add_and_fetch, __sync_sub_and_fetch, __sync_or_and_fetch, __sync_and_and_fetch, __sync_xor_and_fetch, __sync_nand_and_fetch, __sync_bool_compare_and_swap, __sync_val_compare_and_swap, __sync_synchronize, __sync_lock_test_and_set, __sync_bool_compare_and_swap,
__sync_val_compare_and_swap, __sync_synchronize, __sync_lock_test_and_set, __sync_lock_release

No comments: