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.
C++11 implementation statusYou can use Clang in C++11 mode with the -std=c++11 option. Clang's C++11 mode can be used with libc++ or with gcc's libstdc++.
If you are looking for source analysis or source-to-source transformation tools, Clang is probably a great solution for you. Clang supports C++11, C++14 and C++17, please see the C++ status page for more information.
Yes, for C code Clang and GCC are compatible (they both use the GNU Toolchain for linking, in fact.) You just have to make sure that you tell clang to create compiled objects and not intermediate bitcode objects. C ABI is well-defined, so the only issue is storage format.
clang is a C, C++, and Objective-C compiler which encompasses preprocessing, parsing, optimization, code generation, assembly, and linking. Depending on which high-level mode setting is passed, Clang will stop before doing a full link.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With