I'm trying to use libpcap with android to test it I wrote a simple c program that looks like this
#include <string.h>
#include <pcap.h>
#include <jni.h>
jstring Java_com_example_penguin_AvailableHosts_stringFromJNI(JNIEnv* jobject thiz) {
char *dev, errbuf[PCAP_ERRBUF_SIZE];
dev = pcap_lookupdev(errbuf);
if (dev != NULL)
return (*env)->NewStringUTF(env, dev);
else
return (*env)->NewStringUTF(env, "ERROR");
}
The Android.mk file looks like
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := test.c
include $(BUILD_SHARED_LIBRARY)
In the root of my project I call
ndk-build
which gives me the following error
jni/test.c:2:18: fatal error: pcap.h: No such file or directory
compilation terminated.
Is there anything missing in the Android.mk file?
Related
I'm trying to modify this tutorial to include a prebuilt C library in my Android Studio project (ie. not using the experimental Gradle plugin) http://kvurd.com/blog/compiling-a-cpp-library-for-android-with-android-studio/
The library itself is coming from a client who won't reveal the source code, therefore I have no control over that part of the build process, however they are already following the same tutorial.
The project builds, load-library works and the NDK link (/jni/my-wrapper.c) works fine, until I try to call the actual library function defined in my prebuild header. The error I'm receiving is:
$ ndk-build
[arm64-v8a] Compile : my-wrapper <= my-wrapper.c
[arm64-v8a] SharedLibrary : libmy-wrapper.so
/Users/me/AndroidStudioProjects/MyProject/app/obj/local/arm64-v8a/objs/my-wrapper/my-wrapper.o: In function `Java_com_my_project_SignInActivity_CallFunction':
/Users/me/AndroidStudioProjects/MyProject/app/jni/my-wrapper.c:44: undefined reference to `MyFunction'
collect2: error: ld returned 1 exit status
make: *** [/Users/me/AndroidStudioProjects/MyProject/app/obj/local/arm64-v8a/libmy-wrapper.so] Error 1
Here's my Android.mk:
LOCAL_PATH := $(call my-dir)
# static library info
include $(CLEAR_VARS)
LOCAL_MODULE := libMyLib
LOCAL_MODULE_FILENAME := libMyLib
LOCAL_SRC_FILES := ../prebuild/libMyLib.a
LOCAL_EXPORT_C_INCLUDES := ../prebuild/include
include $(PREBUILT_STATIC_LIBRARY)
# wrapper info
include $(CLEAR_VARS)
LOCAL_C_INCLUDES += ../prebuild/include
LOCAL_MODULE := my-wrapper
LOCAL_SRC_FILES := my-wrapper.c
LOCAL_STATIC_LIBRARIES := libMyLib
include $(BUILD_SHARED_LIBRARY)
And MyLib.h (note that foobar() works fine as it's in the header but as long as I'm calling MyFunction from within my-wrapper.c the ndk-build fails):
#include <math.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int MyFunction(some stuff);
int foobar(){return 1;};
Finally, my-wrapper.c:
#include <MyLib.h>
jbyte Java_com_my_project_SignInActivity_MyFunction(JNIEnv *env, jobject thiz, some other stuff){
// return MyFunction(some other stuff which I cast to C types); //linker fails if uncommented
return foobar(); //works fine
}
That's a C++ mangled name. You can only use it from C++, not from C.
If you really need to call it from C, you might be able to do it like so:
extern int _Z12MyFunctionP9my_structPhS1_S1_(/* whatever the function args are */);
jbyte Java_com_my_project_SignInActivity_MyFunction(
JNIEnv *env, jobject thiz, some other stuff) {
return _Z12MyFunctionP9my_structPhS1_S1_(args);
}
That depends on the code you're calling being compatible as such (if that's the case, you should ask the client to build their APIs as extern "C").
I'd really recommend just moving your code to C++ though.
I need your help because it drives me crazy. What cause my error?
The error is
"jni/algorithm.cpp:4:33: fatal error: opencv2/core/core.hpp: No such file or directory #include <opencv2/core/core.hpp>
^ compilation terminated. make: *** [obj/local/arm64-v8a/objs/algorithm/algorithm.o] Error 1"
My algorithm.cpp is:
#include <jni.h>
#include <string.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
using namespace std;
using namespace cv;
extern "C"
{
JNIEXPORT jlong JNICALL Java_com_example_hematoma_MainActivity_fce(JNIEnv *env, jobject obj, jlong matimage)
{
Mat *jni_image = (Mat*) matimage;
return (jlong)jni_image;
}
}
My Android.mk is:
LOCAL_PATH := $(call my-dir)
include /home/nemesis/adt-bundle-linux-x86_64-20140702/OpenCV-2.4.10-android-sdk/sdk/native/jni/OpenCV.mk
include $(CLEAR_VARS)
LOCAL_MODULE := algorithm
LOCAL_SRC_FILES := algorithm.cpp
LOCAL_C_INCLUDE := /home/nemesis/adt-bundle-linux-x86_64-20140702/OpenCV-2.4.10-android-sdk/sdk/native/jni/include/opencv2/core/core.hpp
LOCAL_C_INCLUDE += /home/nemesis/adt-bundle-linux-x86_64-20140702/OpenCV-2.4.10-android-sdk/sdk/native/jni/include/
include $(BUILD_SHARED_LIBRARY)
The error occurs when ndk try to build .so
Thanks in advance.
LOCAL_C_INCLUDE := /home/nemesis/adt-bundle-linux-x86_64-20140702/OpenCV-2.4.10-android-sdk/sdk/native/jni/include/opencv2
should be
LOCAL_C_INCLUDES := /home/nemesis/adt-bundle-linux-x86_64-20140702/OpenCV-2.4.10-android-sdk/sdk/native/jni/include
ie, it is plural and should point to the location from which the following is a relative path:
#include <opencv2/core/core.hpp>
I solved my problem by add LOCAL_LDLIBS += -llog -ldl to "Android.mk"
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include C:/.../OpenCV-3.1.0-android-sdk/OpenCV-android-sdk/sdk/native/jni/OpenCV.mk
LOCAL_MODULE := Testappcv
LOCAL_SRC_FILES := Testappcv.cpp
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
And added "Application.mk" file to jni folder
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-8
So, I tried to change in Project properties C/C++ Build -> Build command from ${NDKROOT}/ndk-build to ${NDKROOT}/ndk-build.cmd and it solved all my include libs errors. weird
Suppose my Android project uses jni with next structure:
jni__
|__src__
|__first.c
|__second.c
|
|__include__
|__first.h
There are two source files in src directory and one header file in src/include director.
first.h:
#include <string.h>
#ifndef FIRST_H
#define FIRST_H
extern char * text();
#endif
first.c:
#include "first.h"
char * text() {
...
return ...;
}
second.c:
#include "first.h"
#include <jni.h>
jbyteArray Java_com_example_android_MainActivity_getText( JNIEnv* env, jobject this) {
char * str = text();
...
return str;
}
How to compile this source and correctly specify the directory with headers in Android.mk file?
Now my Android.mk file is as follows:
LOCAL_PATH:= $(call my-dir)
LOCAL_SRC_PATH := $(LOCAL_PATH)/../src
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../src/include
include $(CLEAR_VARS)
LOCAL_MODULE := mylib
LOCAL_SRC_FILES := $(LOCAL_SRC_PATH)/first.c \
$(LOCAL_SRC_PATH)/second.c
include $(BUILD_SHARED_LIBRARY)
But when I try to build this sources compilation terminated with error: "first.h: No such file or directory".
LOCAL_C_INCLUDES := $(LOCAL_PATH)/src/include should do it. As stated in ANDROID-MK file in NDK docs that you can find in your NDK directory, $(LOCAL_PATH) refers to the directory that your Android.mk file is located. Assuming that it is located in jni directory, $(LOCAL_PATH)/src/include points to your header directory where the missing file is located.
And I would use #include <first.h> rather than using #include "first.h" according to the general convention of using headers.
I'm trying to build simple android app with some JNI code.
I already try this suggestion, but isn't help
When I press build project in eclipse I get this error:
Description Resource Path Location Type
fatal error: algorithm: No such file or directory Tracker line 56, external location: /home/slani/code/OpenCV-2.4.6-android-sdk/sdk/native/jni/include/opencv2/core/core.hpp C/C++ Problem
make: *** [obj/local/armeabi/objs/detect_jni/detect_jni.o] Error 1 Tracker C/C++ Problem
Line 56 in core.hpp contains the relevant include.
This is my Android.mk file jni folder:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include /home/slani/code/OpenCV-2.4.6-android-sdk/sdk/native/jni/OpenCV.mk
LOCAL_MODULE := detect_jni
LOCAL_SRC_FILES := detect_jni.cpp
include $(BUILD_SHARED_LIBRARY)
This is my Aplication.mk file in jni folder:
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := all
APP_PLATFORM := android-8
This is my .cpp file:
#include <jni.h>
#include <opencv/cv.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
using namespace cv;
extern "C"{
JNIEXPORT void JNICALL Java_com_slani_tracker_OpenCamera_findObject((JNIEnv *env, jlong addRgba, jlong addHsv);
JNIEXPORT void JNICALL Java_com_slani_tracker_OpenCamera_findObject((JNIEnv *env, jlong addRgba, jlong addHsv)
{
Mat& rgba = *(Mat*)addRgba;
Mat& hsv = *(Mat*)addHsv;
cvtColor(rgba, hsv,CV_RGBA2HSV);
}
}
Can someone please help me? What could be causing this problem?
Thanks
See my answer at your thread in answers.opencv.org
Should help
I have a android program that has main logic written in NDK. But now I want to evaluate some python codes as part of main logic. So I tried to use this embed python in c http://docs.python.org/2/extending/embedding.html
I created a simple android project, with native.c like this (all I did was just adding python header)
#include <Python.h>
#include <jni.h>
#include <string.h>
#include <android/log.h>
#include <pthread.h>
#define DEBUG_TAG "NDK_AndroidNDK1SampleActivity"
void Java_com_example_com_test_mytest_MainActivity_helloLog(JNIEnv * env, jobject this, jstring logThis)
{
jboolean isCopy;
const char * szLogThis = (*env)->GetStringUTFChars(env, logThis, &isCopy);
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [%s]", szLogThis);
(*env)->ReleaseStringUTFChars(env, logThis, szLogThis);
}
Here is my Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog `python2.7-config --ldflags`
LOCAL_MODULE := ndk1
LOCAL_SRC_FILES := native.c
LOCAL_CFLAGS := `python2.7-config --cflags`
#LOCAL_SHARED_LIBRARIES := -lpthread
include $(BUILD_SHARED_LIBRARY)
I got following error when compiling (using Mac)
Compile thumb : ndk1 <= native.c
arm-linux-androideabi-gcc: error: i386: No such file or directory
arm-linux-androideabi-gcc: error: x86_64: No such file or directory
arm-linux-androideabi-gcc: error: unrecognized option '-arch'
arm-linux-androideabi-gcc: error: unrecognized option '-arch'
make: *** [obj/local/armeabi/objs/ndk1/native.o] Error 1
Anyone could help please? :D
Python2.7-config was built for a 64 bit i386 system not for Android. It's passing bad arguments to the compiler in the LOCAL_CFLAGS field. Either run one that you built to cross compile on Android or specify the values yourself. Check google for 'Python-For-Android' (Py4A) on how to embed python in an NDK app.