NDK call function from native code - android

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.

Related

OpenCV for Android: undefined reference to std::ios_base::Init::Init() while linking

I have seen the similar questions undefined reference to std::ios_base::Init::Init() and undefined reference to `std::ios_base::Init::Init()' but I'm not sure whether I have the same situation.
I'm configuring opencv for android 3.4.7 on Android Studio 3.5. I imported libs of opencv by editing CMakeList.txt and build.gradle:
set(opencv_version OpenCV3-android-sdk)
set(OpenCV_STATIC ON)
set(OpenCV_DIR /home/lynx/Android/Proj/${opencv_version}/sdk/native/jni)
find_package(OpenCV REQUIRED)
if (OpenCV_FOUND)
message(WARNING "opencv libs: ${OpenCV_LIBS}")
else (OpenCV_FOUND)
message(WARNING "opencv not found!")
endif(OpenCV_FOUND)
target_link_libraries(
${OpenCV_LIBS})
sourceSets {
main {
jniLibs.srcDirs = ['/home/lynx/Android/Proj/OpenCV3-android-sdk/sdk/native/libs']
}
}
and my native-lib.cpp is
#include <jni.h>
#include <string>
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_stitch_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from OpenCV " + (std::string)CV_VERSION;
return env->NewStringUTF(hello.c_str());
}
When building the project it shows many undefined reference errors from the file of opencv. It seems not the problem of linking cpp files with c linker: it called clang++ to do the task and when not using opencv, the project can be build successful.
So is it because that I didn't correctly configured opencv?
This is usually a case of using a library built against a different STL than you're using. I can't tell with the information you've given, but there are a lot of other questions on SO where people build their app with libc++ but build opencv with libstdc++. These are not compatible. See NDK - problems after GNUSTL has been removed from the NDK (revision r18).

Calling function from .so file created in android in linux C++ code

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

Issue porting Cocos2d-x to Android

I've written a basic game using Cocos2D-x. It works fine on the iPhone. Now I'm trying to port it to Android. When I run the script, it gives me the following error:
jni/../../Classes/MemoryModeLayer.cpp: In member function 'void MemoryModeLayer::startNewGame()':
jni/../../Classes/MemoryModeLayer.cpp:109:25: error: 'time' is not a member of 'std'
jni/../../Classes/MemoryModeLayer.cpp:109:25: note: suggested alternative:
/Users/abc/android-ndk-r9d/platforms/android-8/arch-arm/usr/include/time.h:40:17: note: 'time'
jni/../../Classes/MemoryModeLayer.cpp:111:5: error: 'random_shuffle' is not a member of 'std'
jni/../../Classes/MemoryModeLayer.cpp:112:5: error: 'random_shuffle' is not a member of 'std'
make: *** [obj/local/armeabi/objs/cocos2dcpp_shared/__/__/Classes/MemoryModeLayer.o] Error 1
make: Leaving directory `/Users/anil/cocos2d-x-2.2.3/projects/Game/proj.android'
In MemoryModeLayer.cpp I have the following:
std::srand(unsigned(std::time(0)));
std::random_shuffle(_xCod, _xCod + _numberOfRows);
std::random_shuffle(_yCod, _yCod + _numberOfColumns);
I've included the following headers as well:
#include <string>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <iomanip>
Also added using namespace std in the header file. Is there anything else that I should do?
you need to #include <random>
There was a problem with the Application.mk file. Had to change the STL library lookup like so:
Add APP_STL := stlport_static to the Application.mk file. It may already have APP_STL := gnustl_static.
Following blog post was very helpful!
http://blog.codetactics.com/2013/06/05/porting-a-cocos2d-x-ios-game-to-android/

Linking .a compiled library and own shared library Android NDK r8b

I'm encountering an issue with linking C libraries in my Android project using the Android NDK r8b.
I built with the toolchain for ARM the cURL library (no SSL) and it provided me a libcurl.a file and a libcurl.so.4.2.0 one.
I also created a C file to provide functions to my Java code and to use the cURL library.
Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:= libcurl
LOCAL_SRC_FILES := libcurl.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := curljni
LOCAL_SRC_FILES := curljni.c
LOCAL_STATIC_LIBRARIES := libcurl
include $(BUILD_SHARED_LIBRARY)
and now my C file
#include <string.h>
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
jstring Java_test_curljni_MainActivity_stringFromJNI(JNIEnv* env, jobject thiz)
{
CURL *curl;
CURLcode
......
}
When I try to build with the ndk-build command from my project folder, here's the output error :
Compile thumb : curljni <= curljni.c
jni/curljni.c: In function 'Java_test_curljni_MainActivity_stringFromJNI':
jni/curljni.c:8:3: error: unknown type name 'CURL'
jni/curljni.c:9:3: error: unknown type name 'CURLcode'
jni/curljni.c:11:8: warning: assignment makes pointer from integer without a cast [enabled by default]
jni/curljni.c:13:28: error: 'CURLOPT_URL' undeclared (first use in this function)
jni/curljni.c:13:28: note: each undeclared identifier is reported only once for each function it appears in
/cygdrive/c/android-ndk-r8b/build/core/build-binary.mk:252: recipe for target `obj/local/armeabi/objs/curljni/curljni.o' failed
make: *** [obj/local/armeabi/objs/curljni/curljni.o] Error 1
I've tried many things within the Android.mk file, nothing successful. Using a PREBUILT_SHARED_LIBRARY doesn't help because it requires a .so file only and changing the extension from .so.4.2.0 to .so just tells me that the format is not supported when building ...
One thing I'm not sure about if whether I have to include the headers in my C file, but when I do it, it ofcourse don't find them.
As a note, when I build with only the curl library declared in my Android.mk file (the shared library with the C file is ommitted) then nothing is created in the libs folder !
Thanks in advance for your help !
EDIT ::::
Actually it did work mbrenon (I've tried it millions time and now it works, no idea what's different), but now I have
c:/android-ndk-r8b/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6.x-google/../../../. ./arm-linux-androideabi/bin/ld.exe: cannot find ./obj/local/armeabi/libcurl.a: Permission denied
I'm using Cygwin, could that be why I encounter permission issues ?
I manage to get around it when the .so and the .a are created, returning the permission error, chmod on it and running the ndk-build again, but that seems a bit rough !
Plus now none of my curl functions can be reached ("undefined reference to (function)").
$ ../../../ndk-build
SharedLibrary : libcurljni.so
./obj/local/armeabi/objs/curljni/curljni.o: In function `Java_test_curljni_MainActivity_stringFromJNI':
C:\android-ndk-r8b\apps\curljni\project/jni/curljni.c:12: undefined reference to `curl_easy_init'
C:\android-ndk-r8b\apps\curljni\project/jni/curljni.c:14: undefined reference to `curl_easy_setopt'
C:\android-ndk-r8b\apps\curljni\project/jni/curljni.c:15: undefined reference to `curl_easy_perform'
C:\android-ndk-r8b\apps\curljni\project/jni/curljni.c:17: undefined reference to `curl_easy_cleanup'
collect2: ld returned 1 exit status
/cygdrive/c/android-ndk-r8b/build/core/build-binary.mk:378: recipe for target `obj/local/armeabi/libcurljni.so' failed
make: *** [obj/local/armeabi/libcurljni.so] Error 1
and my curljni.c is the following
jstring Java_test_curljni_MainActivity_stringFromJNI(JNIEnv* env, jobject thiz)
{
CURL *curl;
CURLcode res;
char buffer[10];
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "yahoo.com");
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if(res == 0)
return (*env)->NewStringUTF(env, "0 response");
else
sprintf(buffer,"code: %i",res);
return (*env)->NewStringUTF(env, buffer);
}
else {
return (*env)->NewStringUTF(env, "no curl");
}
}
I made sure every needed function is declared in the curl.h
Yes, you do have to include the Curl headers in your C file. To allow ndk-build to find these header files at link time, you'll have to add their path using LOCAL_EXPORT_C_INCLUDES:
include $(CLEAR_VARS)
LOCAL_MODULE:= libcurl
LOCAL_SRC_FILES := libcurl.a
LOCAL_EXPORT_C_INCLUDES := /* put the path to the Curl headers here */
include $(PREBUILT_STATIC_LIBRARY)
This will automatically add the path to the headers in all the targets requiring libcurl, and it should build and link.

Error with memset when compiling c++ code for threads in android ndk

I have a Problem. I want to create a thread in Android 2.3.4 with the NDK r7. When I compile this code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "System.h"
#include <jni.h>
#include <pthread.h>
unsigned int CreateThread(void* function(void*) , void * context)
{ int ret;
pthread_t thread;
ret = pthread_create(&thread, NULL, function, context);
if(ret != 0)
{ return 0;
}
return (unsigned int) thread;
}
I get these error messages:
E:/Code/Android/android-ndk-r7/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.4.3/include/stdarg.h:102: error: expected ';' before 'typedef'
In file included from E:/Code/Android/android-ndk-r7/platforms/android-14/arch-arm/usr/include/pthread.h:32, from jni/system.cpp:13:
E:/Code/Android/android-ndk-r7/platforms/android-14/arch-arm/usr/include/signal.h: In function 'int sigemptyset(sigset_t*)':
E:/Code/Android/android-ndk-r7/platforms/android-14/arch-arm/usr/include/signal.h:84: error: 'memset' was not declared in this scope
E:/Code/Android/android-ndk-r7/platforms/android-14/arch-arm/usr/include/signal.h: In function 'int sigfillset(sigset_t*)':
E:/Code/Android/android-ndk-r7/platforms/android-14/arch-arm/usr/include/signal.h:90: error: 'memset' was not declared in this scope
Did I miss to include or define something to get the memset errors ?
I am really not sure what the problem is and why the memset errors apear. I also tried including cstring but then I got a huge number of errors.
I found the problem. I have my own class that has a filename that is called "string.h" and resides within my jni directory. Therefore the "string.h" that resides within the ndk and has the declaration of the necessary memset function (among others) is not being included.
The options were to either change the filename/name of my class or directly include the string.h from the ndk with a direct path.
I chose the first option and renamed my string class and try to not use standard file names for my classes from now on...

Categories

Resources