c++ 流本地存储 clang-503.0.40 /Mac OSX/

在我以这种方式声明一个变量之后:


#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.


如果我尝试编译相同的代码 Linux 从 GCC 4.8.1 使用相同的标志,我将收到一个运行的可执行文件。 我用 clang-503.0.40 /那个附带的那个 Xcode 5.1.1/ 在 MacBook Pro 被...统治 OSX 10.9.3. 有谁可以向我解释我做错了什么?
谢!!
</thread>
已邀请:

小明明

赞同来自:

尝试
clang++ -stdlib=libc++ -std=c++11

. 过时 libstdc++ OS X 不支持 TLS.

编辑

好的,它适用于通常的版本 clang, 但不是版本 Xcode.

我做了 diff vs.
https://opensource.apple.com/s ... 0.38/

http://llvm.org/releases/3.4/clang-3.4.src.tar.gz
并发现下一个差异:


.Case/"cxx_thread_local",
- LangOpts.CPlusPlus11 && PP.getTargetInfo//.isTLSSupported// &&
- !PP.getTargetInfo//.getTriple//.isOSDarwin///
+ LangOpts.CPlusPlus11 && PP.getTargetInfo//.isTLSSupported///


所以我认为这是版本中的错误 Apple clang /或者他们专门举行它 - 但仍然奇怪,因为
-v

说它是基于 3.4/.

詹大官人

赞同来自:

此外,您还可以使用编译器扩展,例如
https://gcc.gnu.org/onlinedocs ... .html
/GCC/Clang/ 或者
https://msdn.microsoft.com/en- ... .aspx
/Visual Studio/.

将其包装到宏,您可以在不同编译器和语言版本之间轻松传输代码:


#if HAS_CXX11_THREAD_LOCAL
#define ATTRIBUTE_TLS thread_local
#elif defined /__GNUC__/
#define ATTRIBUTE_TLS __thread
#elif defined /_MSC_VER/
#define ATTRIBUTE_TLS __declspec/thread/
#else // !C++11 && !__GNUC__ && !_MSC_VER
#error "Define a thread local storage qualifier for your compiler/platform!"
#endif

...

ATTRIBUTE_TLS static unsigned int tls_int;

八刀丁二

赞同来自:

编译器 clang, 包含在beta. Xcode 8 和 GM, 支持关键字 C++11
thread_local

像S.
-std=c++11

, 所以和s。
-std=c++14

/以及选择 GCC/.

早期的版本 Xcode, 显然,支持的本地存储流风格 C 使用关键字
__thread

或者
_Thread_local

, 根据
https://developer.apple.com/vi ... /405/
/查看开始的讨论 5:50/.

卫东

赞同来自:

看起来您可能需要建立最小目标版本。 OS X 在 10.7 或更高。

要回复问题请先登录注册