title: Android NDK linker Error:error: undefined reference to std::basic_string
body: <p>I am currently trying to build a static library of some source c++ files requiring boost. I have been following the <a href="https://github.com/googlesamples/android-ndk/tree/master/hello-libs" rel="nofollow">hello-libs</a> example and have successfully linked the boost static file dependencies as is done in the sample example.Now said that, during while building the module I get these erros :</p>
  
  <blockquote>
    <p>Error:error: undefined reference to 'std::basic_string, std::allocator >::basic_string(char const*, std::allocator const&amp;)'</p>
    
    <p>Error:error: undefined reference to 'std::runtime_error::runtime_error(std::string const&amp;)'</p>
    
    <p>Error:error: undefined reference to 'std::_Rb_tree_decrement(std::_Rb_tree_node_base*)'</p>
    
    <p>Error:error: undefined reference to 'std::string::_Rep::_S_empty_rep_storage'</p>
    
    <p>Error:error: undefined reference to 'std::basic_string, std::allocator >::~basic_string()'</p>
    
    <p>Error:error: undefined reference to 'std::string::_Rep::_M_dispose(std::allocator const&amp;)'</p>
    
    <p>Error:error: undefined reference to 'std::runtime_error::runtime_error(std::string const&amp;)'</p>
    
    <p>Error:error: undefined reference to 'std::string::_Rep::_M_destroy(std::allocator const&amp;)'</p>
    
    <p>Error:error: undefined reference to 'std::basic_string, std::allocator >::basic_string(std::string const&amp;)'</p>
    
    <p>Error:error: undefined reference to 'std::_Rb_tree_increment(std::_Rb_tree_node_base*)'</p>
    
    <p>Error:error: undefined reference to 'std::string::_Rep::_S_empty_rep_storage'</p>
    
    <p>Error:error: undefined reference to 'std::string::_Rep::_M_destroy(std::allocator const&amp;)'</p>
    
    <p>Error:error: undefined reference to 'std::basic_string, std::allocator >::basic_string(std::string const&amp;)'</p>
    
    <p>Error:error: undefined reference to 'std::string::_Rep::_M_destroy(std::allocator const&amp;)'</p>
    
    <p>Error:error: undefined reference to 'std::_Rb_tree_increment(std::_Rb_tree_node_base const*)'</p>
    
    <p>Error:error: undefined reference to 'std::_Rb_tree_rebalance_for_erase(std::_Rb_tree_node_base*, std::_Rb_tree_node_base&amp;)'</p>
    
    <p>Error:error: undefined reference to 'std::string::_Rep::_S_empty_rep_storage'</p>
    
    <p>Error:error: undefined reference to 'std::string::_Rep::_S_empty_rep_storage'</p>
    
    <p>Error:error: undefined reference to 'std::basic_string, std::allocator >::basic_string(char const*, std::allocator const&amp;)'</p>
    
    <p>Error:error: undefined reference to 'std::basic_string, std::allocator >::basic_string(std::string const&amp;)'</p>
    
    <p>Error:error: linker command failed with exit code 1 (use -v to see invocation)</p>
    
    <p>Error:error: undefined reference to 'std::basic_string, std::allocator >::basic_string(char const*, std::allocator const&amp;)'</p>
    
    <p>Error:error: undefined reference to 'std::string::_Rep::_M_destroy(std::allocator const&amp;)'</p>
    
    <p>Error:error: undefined reference to 'std::_Rb_tree_rebalance_for_erase(std::_Rb_tree_node_base*, std::_Rb_tree_node_base&amp;)'</p>
    
    <p>Error:error: undefined reference to 'std::string::_Rep::_M_dispose(std::allocator const&amp;)'</p>
    
    <p>Error:error: undefined reference to 'std::basic_string, std::allocator >::basic_string(char const*, std::allocator const&amp;)'</p>
    
    <p>Error:error: undefined reference to 'std::_Rb_tree_insert_and_rebalance(bool, std::_Rb_tree_node_base*, std::_Rb_tree_node_base*, std::_Rb_tree_node_base&amp;)'</p>
    
    <p>Error:Execution failed for task ':dummy:linkDummyArmeabiDebugSharedLibrary'.</p>
    
    <p>A build operation failed.
          Linker failed while linking libdummy.so.
      See the complete log at: file:///home/cran-cg/Downloads/Hello-libs-boost/name/build/tmp/linkDummyArmeabiDebugSharedLibrary/output.txt</p>
  </blockquote>
  
  <p>I had been trying to resolve this and had come upon many solutions on stackoverflow itself, suggesting that I should amend my linker path and include libc++.so library </p>
  
  <pre><code>ndk{
          moduleName = 'p2psp'
          stl = "c++_shared"
         // ldLibs.addAll('-L'+ file("/home/cran-cg/Android/Sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi").absolutePath)
          ldLibs.addAll(['android', 'log','atomic'])/*"${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi",'gnustl_static'*/
          toolchain = "clang"
          cppFlags.add("-std=c++11")
          cppFlags.add('-frtti')
          cppFlags.add('-fexceptions')
          cppFlags.addAll('-I' + file("/home/cran-cg/Android/Sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libcxx/include").absolutePath ,'-DANDROID', ' -DANDROID_NDK', '-DAWS_CUSTOM_MEMORY_MANAGEMENT', '-fPIC', '-fexceptions')
          //cppFlags.add("-I${lib_distribution_root}/boost/include".toString())
          //cppFlags.add("-I${file("src/main/jni/inc")}".toString())
  
          abiFilters.addAll(['armeabi'])//, 'armeabi-v7a', 'x86'])
      }
  </code></pre>
  
  <p>And I am confused of what should be done. Please suggest me ways as to how can I resolve this issue. Thanks :))</p>