在我以这种方式声明一个变量之后:
#include <thread>
namespace thread_space
{
thread_local int s;
} //etc.
我尝试使用“g++ -std=c++0x -pthread [sourcefile]”编译我的代码。我收到以下错误:
example.C:6:8: error: thread-local storage is unsupported for the current target
static thread_local int s;
^
1 error generated.
如果我尝试在带有相同标志的 GCC 4.8.1 的 Linux 上编译相同的代码,我会得到一个正常运行的可执行文件。我在运行 OSX 10.9.3 的 MacBook Pro 上使用 clang-503.0.40(Xcode 5.1.1 附带的那个)。谁能解释一下我做错了什么?
谢谢!!
最佳答案
试试 clang++ -stdlib=libc++ -std=c++11
. OS X 过时的 libstdc++ 不支持 TLS。
编辑
好的,这适用于普通的 clang 版本,但不适用于 Xcode 版本。
我对 Apple's clang (503.0.38) 做了一个比较和 normal released one并发现以下差异:
.Case("cxx_thread_local",
- LangOpts.CPlusPlus11 && PP.getTargetInfo().isTLSSupported() &&
- !PP.getTargetInfo().getTriple().isOSDarwin())
+ LangOpts.CPlusPlus11 && PP.getTargetInfo().isTLSSupported())
所以我认为这是 Apple 的 clang 版本中的一个错误(或者他们故意将它保留在那里 - 但仍然很奇怪,因为
-v
基于 3.4 说)。
关于xcode - c++ 线程本地存储 clang-503.0.40 (Mac OSX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23791060/