首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ndk clang3.4/3.5编译__thread没有成功

使用ndk clang3.4/3.5编译__thread没有成功
EN

Stack Overflow用户
提问于 2014-11-28 14:54:02
回答 1查看 2K关注 0票数 1

我试图在这个小程序中使用__thread,没有运气。您知道ndk 10c clang 3.4/3.5中是否支持此TLS吗?这个程序在ndk gcc 4.8/4.9和本地clang/gcc编译器中编译得很好。

这是程序和编译行-

代码语言:javascript
复制
__thread int counter;
int main () { counter=20; return 0; }

[armeabi] Compile++ thumb: test <= test.cpp
/Users/padlar/android/android-ndk-r10c/toolchains/llvm-3.5/prebuilt/darwin-x86/bin/clang++ -MMD -MP -MF ./obj/local/armeabi/objs/test/test.o.d -gcc-toolchain /Users/padlar/android/android-ndk-r10c/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86 -fpic -ffunction-sections -funwind-tables -fstack-protector -Wno-invalid-command-line-argument -Wno-unused-command-line-argument -no-canonical-prefixes -fno-integrated-as  -target armv5te-none-linux-androideabi -march=armv5te -mtune=xscale -msoft-float -fno-exceptions -fno-rtti -mthumb -Os -g -DNDEBUG -fomit-frame-pointer -fno-strict-aliasing -I/Users/padlar/android/android-ndk-r10c/sources/cxx-stl/stlport/stlport -I/Users/padlar/android/android-ndk-r10c/sources/cxx-stl//gabi++/include -Ijni -DANDROID  -Wa,--noexecstack -Wformat -Werror=format-security -fPIE  -frtti     -I/Users/padlar/android/android-ndk-r10c/platforms/android-19/arch-arm/usr/include -c  jni/test.cpp -o ./obj/local/armeabi/objs/test/test.o
[armeabi] Executable     : test
/Users/padlar/android/android-ndk-r10c/toolchains/llvm-3.5/prebuilt/darwin-x86/bin/clang++ -Wl,--gc-sections -Wl,-z,nocopyreloc --sysroot=/Users/padlar/android/android-ndk-r10c/platforms/android-19/arch-arm -Wl,-rpath-link=/Users/padlar/android/android-ndk-r10c/platforms/android-19/arch-arm/usr/lib -Wl,-rpath-link=./obj/local/armeabi ./obj/local/armeabi/objs/test/test.o /Users/padlar/android/android-ndk-r10c/sources/cxx-stl/stlport/libs/armeabi/thumb/libstlport_static.a -lgcc  -gcc-toolchain /Users/padlar/android/android-ndk-r10c/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86 -no-canonical-prefixes -target armv5te-none-linux-androideabi  -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -fPIE -pie  -L/Users/padlar/android/android-ndk-r10c/platforms/android-19/arch-arm/usr/lib -llog -lc -lm -o ./obj/local/armeabi/test
jni/test.cpp:2: error: undefined reference to '__aeabi_read_tp'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [obj/local/armeabi/test] Error 1

ndk-which objdump -S obj/local/armeabi/objs/test/test.o

代码语言:javascript
复制
obj/local/armeabi/objs/test/test.o:     file format elf32-littlearm
Disassembly of section .text.main:

00000000 <main>:
__thread int counter;
int main () { counter=20; return 0; }
   0:   b580        push    {r7, lr}
   2:   4904        ldr r1, [pc, #16]   ; (14 <main+0x14>)
   4:   f7ff fffe   bl  0 <__aeabi_read_tp>
   8:   1840        adds    r0, r0, r1
   a:   2114        movs    r1, #20
   c:   6001        str r1, [r0, #0]
   e:   2000        movs    r0, #0
  10:   bd80        pop {r7, pc}
  12:   46c0        nop         ; (mov r8, r8)
  14:   00000000    .word   0x00000000

gcc4.6解散同一程序

ndk-which objdump -S obj/local/armeabi/objs/test/test.o

代码语言:javascript
复制
obj/local/armeabi/objs/test/test.o:     file format elf32-littlearm
Disassembly of section .text.startup.main:

00000000 <main>:
__thread int counter;
int main () { counter=20; return 0; }
   0:   b508        push    {r3, lr}
   2:   4804        ldr r0, [pc, #16]   ; (14 <main+0x14>)
   4:   4478        add r0, pc
   6:   f7ff fffe   bl  0 <__emutls_get_address>
   a:   2314        movs    r3, #20
   c:   6003        str r3, [r0, #0]
   e:   2000        movs    r0, #0
  10:   bd08        pop {r3, pc}
  12:   46c0        nop         ; (mov r8, r8)
  14:   0000000c    .word   0x0000000c
EN

回答 1

Stack Overflow用户

发布于 2014-11-28 16:27:06

首先,您应该避免使用__thread,因为它可能是不可移植的。

下面是gcc在这里说的话,https://gcc.gnu.org/onlinedocs/gcc-4.8.3/gcc/Thread-Local.html

GCC用于实现这一功能的运行时模型起源于特定于IA-64处理器的ABI,但后来也被迁移到其他处理器上。它需要来自链接器(ld)、动态链接器(ld.so)和系统库(libc.so和libpthread.so)的大量支持,因此它并非无处不在。

来自维基百科storage#C.2B.2B

C++0x引入了thread_local关键字。除此之外,各种C++编译器实现提供了声明线程局部变量的特定方法:

代码语言:javascript
复制
Sun Studio C/C++, IBM XL C/C++, GNU C and Intel C/C++ (Linux systems) use the syntax:

    __thread int number;
Visual C++, Intel C/C++ (Windows systems), Borland C++ Builder and Digital Mars C++ use the syntax:

    __declspec(thread) int number;
 Borland C++ Builder also supports the syntax:

    int __thread number;

因此,它可能适用于thread_local关键字。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27191214

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档