I got a .so file from Android aar package with x86 , x86_64, armv7 etc.
Would it be possible for me to call the function in the .so file?
What I have been tried
created a main.c
#include <stdio.h>
int main() {
printf("Hello World!\n");
printf("%d", Java_com_scaf_android_client_CodecUtils_stringFromJNI());
return 0;
}
execute
$ gcc -o main -lLockCore main.c
and I faced this error
main.c:4:15: warning: implicit declaration of function ‘Java_com_scaf_android_client_CodecUtils_stringFromJNI’ [-Wimplicit-function-declaration]
printf("%d", Java_com_scaf_android_client_CodecUtils_stringFromJNI());
^
/tmp/cc9vP9rb.o: In function `main':
main.c:(.text+0x14): undefined reference to `Java_com_scaf_android_client_CodecUtils_stringFromJNI'
collect2: error: ld returned 1 exit status
Related
CAMERA NDK
I add .h(#include <media/NdkImageReader.h>) to .cpp.
when compile project, the function from .h (#include <media/NdkImageReader.h>)undefined reference.
#include <media/NdkImageReader.h>
#include <media/NdkImage.h>
error:
CMakeFiles/native-camera2-lib.dir/native-camera2-lib.cpp.o: In function Java_com_example_ts_camerandk_NativeCamera_openCamera':
D:\AndroidStudioProjects\camerandk\app\src\main\jni\native-camera2-lib.cpp:(.text+0x348): undefined reference to AImageReader_new'
D:\AndroidStudioProjects\camerandk\app\src\main\jni\native-camera2-lib.cpp:(.text+0x378): undefined reference to AImageReader_setImageListener
CMakeFiles/native-camera2-lib.dir/native-camera2-lib.cpp.o: In function Java_com_example_ts_camerandk_NativeCamera_startPreview':
D:\AndroidStudioProjects\camerandk\app\src\main\jni\native-camera2-lib.cpp:(.text+0x6e4): undefined reference to AImageReader_getWindow
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
enter code here
Check your cmake target libraries line in CMakeLists.txt file. You are forgetting to include mediandk
target_link_libraries(... camera2ndk mediandk ...)
Media NDK is a different library, and is not a part of Camera or Camera2.
I have .so file which I take from apk. With command nm -D libnative-lib.so I took list of functions. I need to call function named Java_com_example_nativelib_utils_Constants_getStringFromJNI . I wrote the code below:
#include <stddef.h>
#include <dlfcn.h>
#include <stdio.h>
int init_library() {
void* hdl = dlopen("./libnative-lib.so", RTLD_LAZY);
if(hdl == 0)
return 0;
else return 1;
}
int main() {
printf("%s\n", (init_library() ? "OK": "ERROR"));
}
and make file:
all: exe
exe: main.c
gcc main.c -fPIC -ldl -o main
but result of program is always "ERROR". I tried libnative-lib.so from x86 and armeabi-v7a architectures (others not present). How to load library and call function from it? C++ or C do not care.
It is not possible to use android .so files in non-android project. similar question is here
I rooted my android phone and installed termux and clang.
When I tried to compile this code:
#include <android/sensor.h>
int main(){
ASensorManager* sensorManager = ASensorManager_getInstance();
}
to access sensors in my phone it gives a bunch of errors like:
undefined reference to 'llvm::Type::getIntegerBitWidth
undefined reference to 'llvm::APFloat::IEEEHalfe
I compiled it on termux using clang:
clang++ main.cpp -o main -landroid
but I cant find the library Im supposed to link with or if this even works at all.
I'm trying to build google android example using my own graph.
I used the quantization method explained here.
I've added "//tensorflow/contrib/quantization:cc_ops" and "//tensorflow/contrib/quantization/kernels:quantized_ops" to the build file.
When I run bazel build //tensorflow/examples/android:tensorflow_demo
I get the following errorERROR: /home/dani/tensorflow/tensorflow/core/BUILD:1065:1: C++ compilation of rule '//tensorflow/core:gpu_runtime' failed: arm-linux-androideabi-gcc failed: error executing command external/androidndk/ndk/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc -fstack-protector-strong -fpic -ffunction-sections -funwind-tables ... (remaining 95 argument(s) skipped): com.google.devtools.build.lib.shell.BadExitStatusException: Process exited with status 1.
tensorflow/core/common_runtime/gpu/gpu_debug_allocator.cc: In member function 'virtual void* tensorflow::GPUNanResetAllocator::AllocateRaw(std::size_t, std::size_t)':
tensorflow/core/common_runtime/gpu/gpu_debug_allocator.cc:169:53: error: 'nanf' is not a member of 'std'
std::vector<float> nans(req_size / sizeof(float), std::nanf(""));
^
tensorflow/core/common_runtime/gpu/gpu_debug_allocator.cc:169:53: note: suggested alternative:
In file included from external/androidndk/ndk/sources/cxx-stl/gnu-libstdc++/4.9/include/cmath:44:0,
from external/androidndk/ndk/sources/cxx-stl/gnu-libstdc++/4.9/include/complex:44,
from ./tensorflow/core/framework/numeric_types.h:19,
from ./tensorflow/core/framework/allocator.h:24,
from ./tensorflow/core/common_runtime/visitable_allocator.h:20,
from ./tensorflow/core/common_runtime/gpu/gpu_debug_allocator.h:23,
from tensorflow/core/common_runtime/gpu/gpu_debug_allocator.cc:16:
external/androidndk/ndk/platforms/android-21/arch-arm/usr/include/math.h:354:7: note: 'nanf'
float nanf(const char *) __NDK_FPABI_MATH__ __pure2;
^
tensorflow/core/common_runtime/gpu/gpu_debug_allocator.cc: In member function 'virtual void tensorflow::GPUNanResetAllocator::DeallocateRaw(void*)':
tensorflow/core/common_runtime/gpu/gpu_debug_allocator.cc:182:53: error: 'nanf' is not a member of 'std'
std::vector<float> nans(req_size / sizeof(float), std::nanf(""));
^
tensorflow/core/common_runtime/gpu/gpu_debug_allocator.cc:182:53: note: suggested alternative:
In file included from external/androidndk/ndk/sources/cxx-stl/gnu-libstdc++/4.9/include/cmath:44:0,
from external/androidndk/ndk/sources/cxx-stl/gnu-libstdc++/4.9/include/complex:44,
from ./tensorflow/core/framework/numeric_types.h:19,
from ./tensorflow/core/framework/allocator.h:24,
from ./tensorflow/core/common_runtime/visitable_allocator.h:20,
from ./tensorflow/core/common_runtime/gpu/gpu_debug_allocator.h:23,
from tensorflow/core/common_runtime/gpu/gpu_debug_allocator.cc:16:
external/androidndk/ndk/platforms/android-21/arch-arm/usr/include/math.h:354:7: note: 'nanf'
float nanf(const char *) __NDK_FPABI_MATH__ __pure2;
^
Target //tensorflow/examples/android:tensorflow_demo failed to build
I've tried using both android-ndk-r10e and android-ndk-r11.
Thanks, Dan
I am trying to call a simple connection function from native C code using the NDK and JNI. The following is my code:
C code - returns a value
freerdp_ext_connect(const char *host, const char *username,
const char *pass, const char *domain,
int width, int height){}
Java code:
#include <jni.h>
#include <stdio.h>
#include <freerdp/freerdp.h>
jstring Java_com_example_freerdpandroid_FreeRDPActivity_test(JNIEnv* env, jobject javaThis)
{
freerdp_ext_connect("10.123.123.12", "admin", "dsadsad", "", 600, 800);
return (*env)->NewStringUTF(env, "dsfsdf");
}
When trying to compile with cygwin it's giving me this error:
./obj/local/armeabi/objs/freerdp/freerdp.o: In function `Java_com_example_freerdpandroid_FreeRDPActivity_test':
C:\Users\stefan.scerri\workspace\freerdp/jni/freerdp.c:11: undefined reference to `freerdp_ext_connect'
collect2: ld returned 1 exit status
/cygdrive/c/android-ndk/android-ndk/build/core/build-binary.mk:378: recipe for target `obj/local/armeabi/libfreerdp.so' failed
make: *** [obj/local/armeabi/libfreerdp.so] Error 1
Any idea why this is so? I believe it is because of the return value of the C function, however I'm not sure, can someone kindly confirm. Thanks in advance.
Is the freerdp_ext_connect in a compiled library, by any chance? If so, are you linking to that library when you build your NDK project? To link with a library, place the following into Android.mk:
LOCAL_LDLIBS := -lfoo
And the linker will link against libfoo.a or libfoo.so, whichever is available.
If freerdp_ext_connect is in a source file - are you compiling that source file? The source file must be listed under LOCAL_SRC_FILES in Android.mk.