title: Linking STL into a standalone Android NDK executable
body: <p>The following code causes a segmentation fault after printing the string:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int main()
  {
      cout &lt;&lt; "ndktest" &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <ul>
  <li>If I change cout to printf, it doesn't crash. </li>
  <li>If I call cout in a different function other than main, and that
  function is never accessed, it still crashes.</li>
  <li>It crashes on Froyo, but not ICS.</li>
  </ul>
  
  <p>For reference, here's my Android.mk:</p>
  
  <pre><code>LOCAL_PATH:= $(call my-dir)
  
  include $(CLEAR_VARS)
  
  LOCAL_MODULE := ndktest
  LOCAL_SRC_FILES := main.cpp
  
  include $(BUILD_EXECUTABLE)
  </code></pre>
  
  <p>and Application.mk:</p>
  
  <pre><code>APP_STL := stlport_static
  </code></pre>
  
  <p>Here's the crash from logcat:</p>
  
  <pre><code>I/DEBUG   (  872): pid: 4234, tid: 4234  &gt;&gt;&gt; /data/ndktest &lt;&lt;&lt;
  I/DEBUG   (  872): signal 11 (SIGSEGV), fault addr 00024004
  I/DEBUG   (  872):  r0 00033824  r1 00001000  r2 00024005  r3 bea42cfc
  I/DEBUG   (  872):  r4 40008090  r5 0000000a  r6 40008000  r7 afd42328
  I/DEBUG   (  872):  r8 00000000  r9 00000000  10 00000000  fp 00000000
  I/DEBUG   (  872):  ip 40008084  sp bea42ce8  lr afd192dd  pc 00024004  cpsr 60000030
  I/DEBUG   (  872):  d0  0000000000000000  d1  0000000000000000
  I/DEBUG   (  872):  d2  0000000000000000  d3  0000000000000000
  I/DEBUG   (  872):  d4  0000000000000000  d5  0000000000000000
  I/DEBUG   (  872):  d6  0000000000000000  d7  0000000000000000
  I/DEBUG   (  872):  d8  0000000000000000  d9  0000000000000000
  I/DEBUG   (  872):  d10 0000000000000000  d11 0000000000000000
  I/DEBUG   (  872):  d12 0000000000000000  d13 0000000000000000
  I/DEBUG   (  872):  d14 0000000000000000  d15 0000000000000000
  I/DEBUG   (  872):  scr 00000000
  I/DEBUG   (  872): 
  I/DEBUG   (  872):          #00  pc 00024004  
  I/DEBUG   (  872):          #01  pc 000192da  /system/lib/libc.so
  I/DEBUG   (  872):          #02  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #03  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #04  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #05  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #06  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #07  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #08  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #09  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #10  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #11  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #12  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #13  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #14  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #15  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #16  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #17  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #18  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #19  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #20  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #21  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #22  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #23  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #24  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #25  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #26  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #27  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #28  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #29  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #30  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872):          #31  pc 0001972c  /system/lib/libc.so
  I/DEBUG   (  872): 
  I/DEBUG   (  872): code around pc:
  I/DEBUG   (  872): 00023fe4 ffffffff ffffffff ffffffff ffffffff 
  I/DEBUG   (  872): 00023ff4 ffffffff ffffffff ffffffff ffffffff 
  I/DEBUG   (  872): 00024004 ffffffff ffffffff ffffffff ffffffff 
  I/DEBUG   (  872): 00024014 ffffffff ffffffff ffffffff ffffffff 
  I/DEBUG   (  872): 00024024 ffffffff ffffffff ffffffff ffffffff 
  I/DEBUG   (  872): 
  I/DEBUG   (  872): code around lr:
  I/DEBUG   (  872): afd192bc f8cde018 f7f4e00c b930ec56 0c0cf844 
  I/DEBUG   (  872): afd192cc 46302201 f7f49900 9803ec4e 47909a05 
  I/DEBUG   (  872): afd192dc f1153c0c d5d735ff 2e006836 463cd1ce 
  I/DEBUG   (  872): afd192ec b9b99901 c03cf8df 300cf857 f8471e58 
  I/DEBUG   (  872): afd192fc b978000c 587a490c e0046810 68059900 
  I/DEBUG   (  872): 
  I/DEBUG   (  872): stack:
  I/DEBUG   (  872):     bea42ca8  b000c9b8  
  I/DEBUG   (  872):     bea42cac  00000014  
  I/DEBUG   (  872):     bea42cb0  b00093e0  /system/bin/linker
  I/DEBUG   (  872):     bea42cb4  b001e9b8  
  I/DEBUG   (  872):     bea42cb8  b000c9b8  
  I/DEBUG   (  872):     bea42cbc  b0001c78  /system/bin/linker
  I/DEBUG   (  872):     bea42cc0  b001e724  
  I/DEBUG   (  872):     bea42cc4  00000005  
  I/DEBUG   (  872):     bea42cc8  b000c9b8  
  I/DEBUG   (  872):     bea42ccc  b0001de3  /system/bin/linker
  I/DEBUG   (  872):     bea42cd0  b000c9b8  
  I/DEBUG   (  872):     bea42cd4  b0002c77  /system/bin/linker
  I/DEBUG   (  872):     bea42cd8  00000000  
  I/DEBUG   (  872):     bea42cdc  b001ffe8  
  I/DEBUG   (  872):     bea42ce0  df002777  
  I/DEBUG   (  872):     bea42ce4  e3a070ad  
  I/DEBUG   (  872): #01 bea42ce8  00001000  
  I/DEBUG   (  872):     bea42cec  00000000  
  I/DEBUG   (  872):     bea42cf0  bea42cfc  
  I/DEBUG   (  872):     bea42cf4  00033824  
  I/DEBUG   (  872):     bea42cf8  00000000  
  I/DEBUG   (  872):     bea42cfc  00024005  
  I/DEBUG   (  872):     bea42d00  00033824  
  I/DEBUG   (  872):     bea42d04  00000000  
  I/DEBUG   (  872):     bea42d08  00000000  
  I/DEBUG   (  872):     bea42d0c  00000000  
  I/DEBUG   (  872):     bea42d10  b000c9b8  
  I/DEBUG   (  872):     bea42d14  00000000  
  I/DEBUG   (  872):     bea42d18  00000000  
  I/DEBUG   (  872):     bea42d1c  afd19731  /system/lib/libc.so
  I/BootReceiver(  946): Copying /data/tombstones/tombstone_09 to DropBox (SYSTEM_TOMBSTONE)
  </code></pre>
  
  <p>Running this through the stack trace tool didn't yield any symbolic information.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>The pc value in the crash dump (0001972c in /system/lib/libc.so) is in an exit routine, which is consistent with crashing after the string is printed:</p>
  
  <pre><code>00019728 &lt;exit&gt;:
     19728:   4604        mov r4, r0
     1972a:   2000        movs    r0, #0
     1972c:   f7ff fd8c   bl  19248 &lt;__cxa_finalize&gt;
     19730:   4620        mov r0, r4
     19732:   f7f3 ee96   blx d460 &lt;_exit&gt;
     19736:   bf00        nop
  </code></pre>