I have a simple Android NDK code with JNI. Unfortunately, it does not build due to an error:
error: undefined reference to 'get_hello()'
I have checked other Stackoverflow questions with the same error. But none of them are similar to my file structure.
File structure
├── 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
CMakeLists.txt
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} )
native-lib.cpp
#include <jni.h>
#include "my_hello/hello.h"
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_myapplication_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
return env->NewStringUTF(get_hello());
}
my_hello/hello.h
#ifndef MY_APPLICATION_HELLO_H
#define MY_APPLICATION_HELLO_H
const char *get_hello();
#endif //MY_APPLICATION_HELLO_H
my_hello/hello.c
#include "hello.h"
#include "../your_hello/hihi.h"
const char *get_hello() {
return get_your_hello();
}
your_hello/hihi.h
#ifndef MY_APPLICATION_HIHI_H
#define MY_APPLICATION_HIHI_H
const char* get_your_hello();
#endif //MY_APPLICATION_HIHI_H
your_hello/hihi.c
#include "hihi.h"
const char* get_your_hello() {
return "your hello";
}
Cmake error
> 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
: && /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 && :
/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.