簡體   English   中英

clang 不支持 std::atomic?

[英]std::atomic not supported by clang?

我正在嘗試將 std::atomic 與 clang 一起使用。 但是,每當我嘗試包含頭文件 atomic ( #include <atomic> ) 時,都會收到消息“未找到原子”。 請注意,我在編譯時包括 - std=c++11 -stdlib=libc++ 我錯過了什么?

我使用的 clang 版本是 3.2。

我使用的 clang 版本是 3.2。

Clang 根據LLVM CXX Status為兩個不同版本添加了原子支持。 第一個是 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

我只能說“我認為”,因為cxx_atomic沒有記錄在Clang Language Extensions 中 但是,它出現在 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

您是否指定-I /path/to/your/c++ (或幾乎等效地-cxx-isystem /path/to/your/c++ )以便clang++可以找到它的位置?

如果您認為不需要它們,請嘗試使用clang++ -print-search-dirs進行確認。

您的 clang 版本已過時。 您應該從包管理器或http://clang.llvm.org/獲取最新版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM