undefined reference to `__sync_add_and_fetch_4′ when compiling Redis

I’m intending to play around with Redis a little over the next few days.  I’ve been wanting to do this for a long time and I may have found a reason for me to do it now.

So I downloaded the source onto a VM and ran make where I was getting this error

zmalloc.o: In function `zmalloc_used_memory':
/opt/redis-2.6.7/src/zmalloc.c:223: undefined reference to `__sync_add_and_fetch_4'
collect2: ld returned 1 exit status
make[1]: *** [redis-server] Error 1
make[1]: Leaving directory `/var/opt/redis-2.6.7/src'
make: *** [all] Error 2

The solution is to explicitly declare the CPU type to make this work.

[[email protected] redis-2.6.7]# uname -m

I had earlier tried setting -march=i386 since that is the CPU type that yum uses.  That didn’t work.  So I set the CPU to exactly the output of uname -m.

[[email protected] redis-2.6.7]# export CFLAGS=-march=i686
 [[email protected] redis-2.6.7]# make distclean
 [[email protected] redis-2.6.7]# make


1 thought on “undefined reference to `__sync_add_and_fetch_4′ when compiling Redis

  1. Just landed in your blog searching for the same error (“undefined reference to __sync_add_and_fetch_4” when building Redis 2.6.12 on CentOS.
    Starting Redis 2.6.x branch, the code uses GCC atomic builtins, like  __sync_add_and_fetch (see src/zmalloc.c:223), and those operations are not available for all target CPUs, i386 for example. If you build Redis 2.6 on a environment with atomic builtins (gcc >= 4.1.0) and i386 you will get this error, as you point, forcing i686 as target processor will fix this.
    If you use CentOS and want to avoid compiling Redis 2.6, you can try http://powerstack.org repository, provides RPM packages with latest versions for LAMP stack (Apache 2.4, MySQL 5.6, PHP 5.4, etc.) and other packages focused on server environment: Redis, node.js, memcached, HAproxy, etc.

Leave a Reply

Your email address will not be published. Required fields are marked *