Issue
Here's my toy implementation of android ndk using android studio. The native C template generated projects runs fine. A Cmakelists.txt is also generated. I am aware custom libraries have to be declared under the cmakelists.txt, so that compiled libraries are generated during run time. I am not sure how to avoid decorating all my C functions for jni and simply generate a .so library since they will not be call directly from java.
Inside the sample native-lib.cpp, I tried to insert foo.cpp and foo.h files. I assumed that since function Add()
will be called inside the JNI macros, decorating Add() should not be necessary. How can I generate .so files for this libraries?
I want to import 4-5 cpp and hpp files later, but I don't want to decorate all the functions, except the ones that will be called from android. I am using Android Studio 3.5.3 and latest Cmake and ndk.
native-lib.cpp
#include <jni.h>
#include <string>
#include "foo.h"
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_testndk1_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
int c = Add(10,100);
std::string hello = "Hello from my C++";
return env->NewStringUTF(hello.c_str());
}
foo.h
int Add(int a, int b);
#include "foo.h"
foo.cpp
int Add(int a, int b) {
return a + b;
}
Error Log:
Build command failed. Error
while executing process D:\Apps\AndroidStudioSDK\cmake\3.10.2.4988404\bin\ninja.exe with arguments {-C D:\Android_Projects\TestNDK1\app.cxx\cmake\debug\arm64-v8a native-lib} ninja: Entering directory
D:\Android_Projects\TestNDK1\app\.cxx\cmake\debug\arm64-v8a' [1/2] Building CXX object CMakeFiles/native-lib.dir/native-lib.cpp.o [2/2] Linking CXX shared library D:\Android_Projects\TestNDK1\app\build\intermediates\cmake\debug\obj\arm64-v8a\libnative-lib.so FAILED: D:/Android_Projects/TestNDK1/app/build/intermediates/cmake/debug/obj/arm64-v8a/libnative-lib.so cmd.exe /C "cd . && D:\Apps\AndroidStudioSDK\ndk\21.1.6352462\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=aarch64-none-linux-android24 --gcc-toolchain=D:/Apps/AndroidStudioSDK/ndk/21.1.6352462/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=D:/Apps/AndroidStudioSDK/ndk/21.1.6352462/toolchains/llvm/prebuilt/windows-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -std=c++14 -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,--no-undefined -Qunused-arguments -shared -Wl,-soname,libnative-lib.so -o D:\Android_Projects\TestNDK1\app\build\intermediates\cmake\debug\obj\arm64-v8a\libnative-lib.so CMakeFiles/native-lib.dir/native-lib.cpp.o -llog -latomic -lm && cd ." CMakeFiles/native-lib.dir/native-lib.cpp.o: In function
Java_com_example_testndk1_MainActivity_stringFromJNI': D:/Android_Projects/TestNDK1/app/src/main/cpp/native-lib.cpp:10: undefined reference to `Add(int, int)' clang++: error: linker command failed with exit code 1 (use -v to see invocation) ninja: build stopped: subcommand failed.
Solution
Adding the following under Cmakelists.txt solved the issue. I don't have to redecorate my functions.
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp
foo.cpp
)
Answered By - ark1974
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.