clang不支持std::atomic吗?

9

我正在尝试在clang中使用std :: atomic。然而,每当我尝试包含头文件atomic(#include <atomic>)时,我会收到“未找到原子”消息。请注意,我在编译时包括-std=c++11 -stdlib=libc++。我错过了什么?

我使用的clang版本是3.2。


2
你使用的clang版本是多少? - user3160514
1
这更多关于你使用哪个标准库实现?它是哪个版本?你可以使用选项-stdlib=<value>来更改它。 - pmr
3
在clang 3.4上工作。你可能需要升级。 - Praetorian
我尝试使用std=c++0x,但那也不起作用。 - MetallicPriest
3个回答

3

我使用的clang版本是3.2。

根据LLVM CXX Status,Clang在两个不同的版本中添加了原子支持。第一个版本是Clang 3.1,第二个版本是Clang 3.2。

我认为你可以使用以下代码进行检查:

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

然后,在你的代码中:
#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

我只能说"我认为",因为Clang语言扩展没有对cxx_atomic进行文档化。然而,在LLVM网站上搜索时,它会出现:"cxx_atomic" site:llvm.org

还有一个CFE用户邮件列表的未解决问题:如何检查std::atomic是否可用?


请注意,在编译时我包含了-std=c++11 -stdlib=libc++。我错过了什么吗?
对于这个问题,你可能正在使用其中一个Clang/LLVM C++运行时,它实际上只是C++03,但假装是C++11。这在过去给我带来了很多问题,因为我们支持许多编译器和平台。
以下是Jonathan Wakely帮助我们制作的测试链接,以查看它是否真的是C++11库,还是苹果的伪造C++11库。
// 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

1

你是否指定了 -I /path/to/your/c++(或者几乎等效的 -cxx-isystem /path/to/your/c++)以便让 clang++ 找到它的位置?

如果你认为不需要它们,请尝试 clang++ -print-search-dirs 确认一下。


0

你的clang版本已经过时了。你应该获取最新版本,可以从你的软件包管理器或http://clang.llvm.org/下载。


我现在不想升级。你知道有什么可以与3.2版本兼容的吗?我尝试了-std=c++0x,但也不起作用。 - MetallicPriest
2
@MetallicPriest:只需要升级你的libc++吗? - Kerrek SB

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接