How do I use gcc builtin __sync_bool_compare_and_swap in g++ on MacOSX?
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 d开发者_开发问答o I fix this? (This is Mac OSX 10.5.8, so it's intel .. .and should have this instruction).
Thanks!
Try adding the command line option
-march=i686
to the linker.
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.
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: Setting GCC 4.2 as the default compiler on Mac OS X Leopard
What versions of GCC are you using? (On both platforms). This is a relatively recent addition to GCC.
At a guess, your box doesn't have the library you used on Linux that uses that function.
(The function wraps the instruction)
OSAtomicAdd32 in libkern/OSAtomic.h
the symbol __sync_bool_compare_and_swap is included in the toolchain(gcc/g++, and other compilers), and related with the version.
sometimes, you will find it, like this: ./arm-fsl-linux-gnueabi/4.6.2/lib/gcc/arm-fsl-linux-gnueabi/4.6.2/plugin/include/sync-builtins.def: "__sync_bool_compare_and_swap", ./arm-fsl-linux-gnueabi/4.6.2/lib/gcc/arm-fsl-linux-gnueabi/4.6.2/plugin/include/sync-builtins.def: "__sync_bool_compare_and_swap_1", ./arm-fsl-linux-gnueabi/4.6.2/lib/gcc/arm-fsl-linux-gnueabi/4.6.2/plugin/include/sync-builtins.def: "__sync_bool_compare_and_swap_2", ./arm-fsl-linux-gnueabi/4.6.2/lib/gcc/arm-fsl-linux-gnueabi/4.6.2/plugin/include/sync-builtins.def: "__sync_bool_compare_and_swap_4", ./arm-fsl-linux-gnueabi/4.6.2/lib/gcc/arm-fsl-linux-gnueabi/4.6.2/plugin/include/sync-builtins.def: "__sync_bool_compare_and_swap_8", ./arm-fsl-linux-gnueabi/4.6.2/lib/gcc/arm-fsl-linux-gnueabi/4.6.2/plugin/include/sync-builtins.def: "__sync_bool_compare_and_swap_16",
精彩评论