简体   繁体   中英

std::atomic not supported by clang?

I am trying to use std::atomic with clang. However, whenever I try to include the header file atomic ( #include <atomic> ), I get the message "atomic not found". Note that I'm including - std=c++11 -stdlib=libc++ while compiling. What am I missing?

The version of clang I'm using is 3.2.

The version of clang I'm using is 3.2.

Clang added atomic support across two different versions according to LLVM CXX Status . The first was Clang 3.1, and the second was Clang 3.2.

I think you can check for it using:

#if defined(__clang__)
#  if __has_feature(cxx_atomic)
#    define CLANG_CXX11_ATOMICS 1
#  endif
#endif

Then, in your code:

#if CLANG_CXX11_ATOMICS
# include <atomic>
#endif

...

#if defined(CLANG_CXX11_ATOMICS)
# define MEMORY_BARRIER() std::atomic_thread_fence(std::memory_order_acq_rel)
#elif defined(__GNUC__) || defined(__clang__)
# define MEMORY_BARRIER() __asm__ __volatile__ ("" ::: "memory")
...
#endif

I can only say "I think" because cxx_atomic is not documented at Clang Language Extensions . However, it shows up on a search of the LLVM site: "cxx_atomic" site:llvm.org .

There's also an open question of the CFE Users mailing list: How to check for std::atomic availability?


Note that I'm including -std=c++11 -stdlib=libc++ while compiling. What am I missing?

For this, you might be using one of those Clang/LLVM C++ runtimes that's really just C++03, but pretends to be C++11. It caused me a lot of problems in the past because we support a number of compilers and platforms.

Below is a test Jonathan Wakely helped us craft to see if it really was a C++11 library, or one of Apple's fake C++11 libraries:

// Visual Studio began at VS2010, http://msdn.microsoft.com/en-us/library/hh567368%28v=vs.110%29.aspx.
// Intel and C++11 language features, http://software.intel.com/en-us/articles/c0x-features-supported-by-intel-c-compiler
// GCC and C++11 language features, http://gcc.gnu.org/projects/cxx0x.html
// Clang and C++11 language features, http://clang.llvm.org/cxx_status.html
#if (_MSC_VER >= 1600) || (__cplusplus >= 201103L)
# define CXX11_AVAILABLE 1
#endif

// Hack ahead. Apple's standard library does not have C++'s unique_ptr in C++11. We can't
//   test for unique_ptr directly because some of the non-Apple Clangs on OS X fail the same
//   way. However, modern standard libraries have <forward_list>, so we test for it instead.
//   Thanks to Jonathan Wakely for devising the clever test for modern/ancient versions.
// TODO: test under Xcode 3, where g++ is really g++.
#if defined(__APPLE__) && defined(__clang__)
#  if !(defined(__has_include) && __has_include(<forward_list>))
#    undef CXX11_AVAILABLE
#  endif
#endif

Did you specify -I /path/to/your/c++ (or, almost equivalently, -cxx-isystem /path/to/your/c++ ) so that clang++ can find its location?

If you think you should not need them, please try clang++ -print-search-dirs to confirm.

Your version of clang is outdated. You should grab the last version, either from your package manager or from http://clang.llvm.org/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM