如何在 g++ 中使用 gcc 内置 __sync_bool_compare_and_swap在 MacOSX 上?
我有一些代码使用:
__sync_bool_compare_and_swap
它在 Linux 上编译得很好。
但是当我尝试在 MacOSX 上用 g++ 编译它时,我得到:
error: ‘__sync_bool_compare_and_swap’ was not declared in this scope
我该如何解决这个问题? (这是Mac OSX 10.5.8,所以它是intel ..并且应该有这个指令)。
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试将命令行选项添加
到链接器。
Try adding the command line option
to the linker.
如果找不到,可以在 Mac 上使用 OSCompareAndSwap()。
但如果能有跨平台的代码就好了,不是吗?
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.
如果 gcc --version 不显示 gcc 4.1 或更高版本,那么您没有该指令。
Xcode 3(Leopard 上的)附带 gcc 4.2,但默认编译器是 4.0,它没有
我刚刚在雪豹上测试过的指令,默认 gcc 是 4.2.1,它可以工作。
请参阅注释:设置GCC 4.2 作为 Mac OS X Leopard 上的默认编译器
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
您使用什么版本的 GCC? (在两个平台上)。这是 GCC 中相对较新的新增内容。
What versions of GCC are you using? (On both platforms). This is a relatively recent addition to GCC.
据猜测,您的机器没有您在 Linux 上使用的使用该功能的库。
(该函数包装了指令)
At a guess, your box doesn't have the library you used on Linux that uses that function.
(The function wraps the instruction)
libkern/OSAtomic.h 中的 OSAtomicAdd32
OSAtomicAdd32 in libkern/OSAtomic.h
符号 __sync_bool_compare_and_swap 包含在工具链(gcc/g++ 和其他编译器)中,并且与版本相关。
有时,你会发现它是这样的:
./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”,
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",