2

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.
Kevin
  • 16,549
  • 8
  • 60
  • 74
arnobpl
  • 1,126
  • 4
  • 16
  • 32
  • do not place code (other than prototypes) in a header file. do not place instances of data in a header file definitions of structs, prototypes, extern statements go into header files – user3629249 Jul 19 '20 at 03:17

1 Answers1

3

Your native-lib library fails during linking because you haven't told it where to find the definitions for get_hello(). You need to link the library containing the definition of get_hello() to the native-lib library.

Your code has this line:

target_link_libraries( hihi-lib
                       hello-lib
                       native-lib
                       ${log-lib} )

which links all of the other libraries to the hihi-lib library. This is probably not what you intend to do.

Looking at your code, native-lib depends on hello-lib, and hello-lib depends on hihi-lib. So, you'll need to specify these dependencies using the target_link_libraries() command:

# Link hihi-lib to hello-lib.
target_link_libraries( hello-lib PUBLIC hihi-lib )

# Link hello-lib (and others) to native-lib, hihi-lib will be propagated via hello-lib.
target_link_libraries( native-lib PRIVATE hello-lib ${log-lib} )

Note, you should always use the scoping operator (e.g. PUBLIC, PRIVATE, etc.) to specify how CMake should link libraries when using target_link_libraries.

Kevin
  • 16,549
  • 8
  • 60
  • 74
  • Thanks for the answer! However, I still get the same error. I replaced my `target_link_libraries` lines with the lines you have provided in the same order (i.e., `hello-lib` first, then `native-lib`) – arnobpl Jul 18 '20 at 03:41
  • 2
    @arnobpl Since you are mixing C and C++ code, you may still be receiving the undefined reference errors due to the issue documented [here](https://stackoverflow.com/a/18879053/3987854). To try to resolve this, you might, for example, wrap the `const char *get_hello();` declaration in an `extern "C" { ... }` block. – Kevin Jul 18 '20 at 03:50