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

2019-06-26 09:13发布

问题:

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.