title: Android NDK JNI build error: undefined reference to 'function_name'
body: <p>I have a simple Android NDK code with JNI. Unfortunately, it does not build due to an error:</p>
  <pre><code>error: undefined reference to 'get_hello()' 
  </code></pre>
  <p>I have checked other Stackoverflow questions with the same error. But none of them are similar to my file structure.</p>
  <h2>File structure</h2>
  <pre><code>├── app
  │   └── src
  │       ├── main
  │       │   ├── AndroidManifest.xml
  │       │   ├── cpp
  │       │   │   ├── CMakeLists.txt
  │       │   │   ├── native-lib.cpp
  │       │   │   ├── my_hello
  │       │   │   │   ├── hello.c
  │       │   │   │   └── hello.h
  │       │   │   └── your_hello
  │       │   │       ├── hihi.c
  │       │   │       └── hihi.h
  │       │   ├── java
  │       │   │   └── com
  │       │   │       └── example
  │       │   │           └── myapplication
  │       │   │               └── MainActivity.java
  </code></pre>
  <h2>CMakeLists.txt</h2>
  <pre><code>cmake_minimum_required(VERSION 3.4.1)
  
  add_library( native-lib
               SHARED
               native-lib.cpp )
  
  add_library( hello-lib
               STATIC
               my_hello/hello.c )
  
  add_library( hihi-lib
               STATIC
               your_hello/hihi.c )
  
  include_directories( my_hello/ )
  include_directories( your_hello/ )
  
  find_library( log-lib
                log )
  
  target_link_libraries( hihi-lib
                         hello-lib
                         native-lib
  
                         ${log-lib} )
  </code></pre>
  <h2>native-lib.cpp</h2>
  <pre><code>#include &lt;jni.h&gt;
  
  #include &quot;my_hello/hello.h&quot;
  
  extern &quot;C&quot; JNIEXPORT jstring JNICALL
  Java_com_example_myapplication_MainActivity_stringFromJNI(
          JNIEnv* env,
          jobject /* this */) {
  
      return env-&gt;NewStringUTF(get_hello());
  }
  </code></pre>
  <h2>my_hello/hello.h</h2>
  <pre><code>#ifndef MY_APPLICATION_HELLO_H
  #define MY_APPLICATION_HELLO_H
  
  const char *get_hello();
  
  #endif //MY_APPLICATION_HELLO_H
  </code></pre>
  <h2>my_hello/hello.c</h2>
  <pre><code>#include &quot;hello.h&quot;
  #include &quot;../your_hello/hihi.h&quot;
  
  const char *get_hello() {
      return get_your_hello();
  }
  </code></pre>
  <h2>your_hello/hihi.h</h2>
  <pre><code>#ifndef MY_APPLICATION_HIHI_H
  #define MY_APPLICATION_HIHI_H
  
  const char* get_your_hello();
  
  #endif //MY_APPLICATION_HIHI_H
  </code></pre>
  <h2>your_hello/hihi.c</h2>
  <pre><code>#include &quot;hihi.h&quot;
  
  const char* get_your_hello() {
      return &quot;your hello&quot;;
  }
  </code></pre>
  <h2>Cmake error</h2>
  <pre><code>&gt; Task :app:externalNativeBuildDebug FAILED
  Build native-lib_armeabi-v7a
  ninja: Entering directory `/home/myname/AndroidStudioProjects/MyApplication/app/.cxx/cmake/debug/armeabi-v7a'
  [1/1] Linking CXX shared library /home/myname/AndroidStudioProjects/MyApplication/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so
  FAILED: /home/myname/AndroidStudioProjects/MyApplication/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so 
  : &amp;&amp; /home/myname/Android/Sdk/ndk/21.3.6528147/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=armv7-none-linux-androideabi23 --gcc-toolchain=/home/myname/Android/Sdk/ndk/21.3.6528147/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/myname/Android/Sdk/ndk/21.3.6528147/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security   -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libnative-lib.so -o /home/myname/AndroidStudioProjects/MyApplication/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so CMakeFiles/native-lib.dir/native-lib.cpp.o  -latomic -lm &amp;&amp; :
  /home/myname/AndroidStudioProjects/MyApplication/app/src/main/cpp/native-lib.cpp:10: error: undefined reference to 'get_hello()'
  clang++: error: linker command failed with exit code 1 (use -v to see invocation)
  ninja: build stopped: subcommand failed.
  </code></pre>