jjzjj

c++ - "thread-local storage not supported for this target",适合#ifdef?

coder 2024-02-17 原文

由于每个编译器都有自己的线程本地存储版本,我最终为它创建了一个宏。现在唯一的问题是 GCC(关闭了 pthreads),这给了我:

“此目标不支持线程本地存储”

很公平,因为在这种情况下 pthreads 实际上是关闭的。问题是,是否有一种通用的方法可以使用一些宏来检测这一点,例如#ifdef __GCC_XXX_NO_THREADS_XXX ?

编辑:请参阅下面接受的答案。另外,这是我的懒惰解决方案:


$ touch test.c
$ gcc -E -dM test.c > out.1
$ gcc -pthread -E -dM test.c > out.2
$ diff out.*
28a29
> #define _REENTRANT 1

这是在 Mac OS X 上运行的。我不确定它是否可移植或其他...

最佳答案

您的编译命令行有或没有 -lpthread:您也可以在其中包含 -DHAVE_PTHREADS

如果你真的想要 GCC/ELF 特定的运行时检测,你可以求助于弱引用:

#include <pthread.h>

extern void *pthread_getspecific(pthread_key_t key) __attribute__ ((weak));

int
main()
{
    if (pthread_getspecific)
        printf("have pthreads\n");
    else
        printf("no pthreads\n");
}

这是它的样子:

$ gcc -o x x.c
$ ./x
no pthreads
$ gcc -o x x.c -lpthread
$ ./x
have pthreads

关于c++ - "thread-local storage not supported for this target",适合#ifdef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4071673/

有关c++ - "thread-local storage not supported for this target",适合#ifdef?的更多相关文章

随机推荐