clang doesn't know std::atomic_bool, but XCode does

心不动则不痛 提交于 2019-12-23 16:24:07

问题


I'm trying to compile C++11 code that declares a variable of type std::atomic_bool. This is on Mac OS 10.8.2 with clang:

clang --version
Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin12.2.0
Thread model: posix

clang complains about std::atomic_bool:

clang++ -c -stdlib=libc++ -msse4 -std=c++11 -Wno-unused-parameter -I. -o query.o query.cpp
In file included from query.cpp:1:
[...]
./threadutils.h:33:10: error: no type named 'atomic_bool' in namespace 'std'; did you mean 'atomic_long'?
    std::atomic_bool work;

However, the same file compiles fine in an XCode project using the same compiler. So I assume I'm missing something in my manual compiler invocation.

I tried a few variations such as -std=c++0x and -std=gnu++11, to no avail.


回答1:


I figured it out. Unfortunately I planted a false flag into my question: it didn't work in XCode either, I had a different version of the source file imported there.

The problem was that C++11 defines "a named type atomic_bool corresponding to the specified atomic<bool>", but clang doesn't support that.

Renaming the type from atomic_bool to atomic<bool> fixed it.




回答2:


I found the same problem. In my case I resolved it by including atomic:

#include <atomic>
static std::atomic_bool varname;

After this, I could call g++ with std=c++11 on a Linux (Ubuntu) as well as compile on XCode.



来源:https://stackoverflow.com/questions/14136128/clang-doesnt-know-stdatomic-bool-but-xcode-does

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!