views:

743

answers:

7

I have some code that uses:

__sync_bool_compare_and_swap

it compiles fine on Linux.

But when I try to compile it on MacOSX in g++, I get:

error: ‘__sync_bool_compare_and_swap’ was not declared in this scope

How do I fix this? (This is Mac OSX 10.5.8, so it's intel .. .and should have this instruction).

Thanks!

A: 

At a guess, your box doesn't have the library you used on Linux that uses that function.

(The function wraps the instruction)

Paul Nathan
+1  A: 

What versions of GCC are you using? (On both platforms). This is a relatively recent addition to GCC.

user9876
A: 

OSAtomicAdd32 in libkern/OSAtomic.h

anon
+3  A: 

If you can't find it, you can use OSCompareAndSwap() on Mac.

But it would be nice to have cross-platform code, wouldn't it.

tony
+1  A: 

if gcc --version doesn't show gcc 4.1 or newer then you don't have the instruction.

Xcode 3 (which is what's on Leopard) ships with gcc 4.2, but the default compiler is 4.0, which doesn't have the instruction

I just tested it on snow leopard, and the default gcc is 4.2.1, where it works.

Please see the note: http://stackoverflow.com/questions/1165361/setting-gcc-4-2-as-the-default-compiler-on-mac-os-x-leopard

Petesh
+2  A: 

Try adding the command line option

-march=i686

to the linker.

xzqx
A: 

I had same problem:

‘__sync_bool_compare_and_swap’ was not declared in this scope

Had gcc 4.0. Updated to gcc 4.4 with macport:

sudo port install gcc44 sudo gcc_select mp-gcc44

gcc --version shows 4.4 is being used.

Still have the same problem.

It seems none of the OSAtomic.h files on my system have this.

locate "OSAtomic.h" | xargs grep "sync_bool" returns nothing.

Tried adding -march=i686 to the linker, but the Makefile system is pretty large and couldn't find where the linking is done. no trace of "-l", at least not in the Makefile in this directory.

This is the Makefile to compile the NeL game server code.

Will try using OSCompareAndSwap() instead.

Any ideas on what to try next?