title: Android NDK JNI undefined reference to C++ source methods
body: <p>I am starting to learn about the Android NDK. I haven't learned much about C/C++ coding and would like to start. I imported the simple HelloJni sample and have been messing around with this to learn as well as the docs. I am stuck and feel it may be how I am using the Android.mk. Looks like it is right but the build is not working on the second module I am testing. Here is what I have in the </p>
  
  <pre><code>      &lt;project folder&gt;/jni/
                            HelloJni.cpp
                            Android.mk
                            Application.mk
                            mytools/
                                    simplemath.h
                                    simplemath.cpp
  </code></pre>
  
  <p>I would like to use my java to call a method in HelloJni.cpp which in turn calls a method in simplemath.cpp and return a value. Simple test to just carry over number added from simplemath.</p>
  
  <p>Below is all my source for each</p>
  
  <p>---------------------Android.mk</p>
  
  <pre><code>LOCAL_PATH := $(call my-dir)
  include $(CLEAR_VARS)
  
  LOCAL_MODULE    := HelloJni
  LOCAL_SRC_FILES := HelloJni.cpp
  LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog 
  
  include $(BUILD_SHARED_LIBRARY)
  
  include $(CLEAR_VARS)
  
  TOOLS_DIR       := $(LOCAL_PATH)/mytools
  LOCAL_C_INCLUDES  := $(TOOLS_DIR)
  LOCAL_MODULE    := simplemath
  LOCAL_SRC_FILES := $(TOOLS_DIR)/simplemath.cpp
  LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog 
  
  include $(BUILD_SHARED_LIBRARY)
  </code></pre>
  
  <p>---------------------HelloJni.cpp</p>
  
  <pre><code>#include &lt;stl/_istream.h&gt;
  #include &lt;stl/_string.h&gt;
  #include &lt;stl/_string_fwd.h&gt;
  
  #define DEBUG_TAG "JNI Native Code..."
  
  using namespace std;
  
  extern "C" {
  
   JNIEXPORT jstring JNICALL Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz, jstring str){
  
       simplemath mathTools;
       mathTools.add(4, 5);
       .....
       return env-&gt;NewStringUTF(str);
  }
  };
  </code></pre>
  
  <p>---------------------simplemath.h</p>
  
  <pre><code>#ifdef __cplusplus
  extern "C" {
  #endif
  
  #define SIMPLEMATH_H
  
  namespace std {
  
  class simplemath{
  
  public:
      simplemath();
      int add(int, int);
      ~simplemath();
  
  };
  
  }
  
  #ifdef __cplusplus
  }
  #endif
  </code></pre>
  
  <p>-----------------simplemath.cpp</p>
  
  <pre><code>    #include &lt;simplemath.h&gt;
  
  namespace std {
  
  simplemath::simplemath(){
  }
  
  int simplemath::add(int a, int b){
      return a + b;
  }
  
  simplemath::~simplemath(){
  }
  
  }
  </code></pre>
  
  <p>When I do a clean on the project, I get no errors. After a build, I get the following:</p>
  
  <pre><code>"Compile++ thumb : HelloJni &lt;= HelloJni.cpp
  
   Prebuilt       : libstlport_static.a &lt;= &lt;NDK&gt;/sources/cxx-stl/stlport/libs/armeabi/
  
  SharedLibrary  : libHelloJni.so
  
  C:/Users/Geek/android-ndk-r8d/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi/objs/HelloJni/HelloJni.o: in function Java_com_example_hellojni_HelloJni_stringFromJNI:jni/HelloJni.cpp:88: error: undefined reference to 'std::simplemath::simplemath()'
  
  C:/Users/Geek/android-ndk-r8d/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi/objs/HelloJni/HelloJni.o: in function Java_com_example_hellojni_HelloJni_stringFromJNI:jni/HelloJni.cpp:89: error: undefined reference to 'std::simplemath::add(int, int)'
  
  C:/Users/Geek/android-ndk-r8d/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi/objs/HelloJni/HelloJni.o: in function Java_com_example_hellojni_HelloJni_stringFromJNI:jni/HelloJni.cpp:92: error: undefined reference to 'std::simplemath::~simplemath()'
  
  collect2: ld returned 1 exit status
  
  make: *** [obj/local/armeabi/libHelloJni.so] Error 1
  </code></pre>