batuman
batuman

Reputation: 7304

Using opencv in native code for Android app development

I have namespace error building with ndk-build for native code in my Android app. The error sample is

C:/adt-bundle-windows-x86/ndk/sources/cxx-stl/gnu-libstdc++/4.6/include/bits
/allocator.h:54:1: error: unknown type name 'namespace'
C:/adt-bundle-windows-x86/ndk/sources/cxx-stl/gnu-libstdc++/4.6/include/bits
/allocator.h:55:1: error: expected ',' or ';' before '{' token

For OpenCV settings, my Application.mk file is

APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi
APP_PLATFORM := android-10 

That means I am using gnu-libstdc++ in compiling the native code.

My jni.c has c extension. That is I receive from my third party and they prefer in .c extension as if they have to change to .cpp extension, they have to change a lot in their other libraries.

So far is OK, I did all project settings for OpenCV for native development and if I do ndk-build, I can make .so libraries.

The problem of those namespace error happened when I include OpenCV's header file #include <opencv2/contrib/detection_based_tracker.hpp> into jni.c and I got a lot of name space error. If I include that #include <opencv2/contrib/detection_based_tracker.hpp> into cpp file, no error. My questions are

(1)Is the error is because of using .hpp file in .c file?

(2)If I have no choice and have to use that .c file, is there way around to remove that error? Thanks

Upvotes: 0

Views: 695

Answers (1)

Jan R&#252;egg
Jan R&#252;egg

Reputation: 10035

My assumption would be that the file is compiled as a "C" file instead of a "C++" file because of the extension ".c". That means you cannot use any "C++" Code in your jni.c, wike classes or namespaces. These are obviously used however in your file "detection_based_tracker.hpp" that you are using.

So the problem is not that you include a file named ".hpp", but that this file contains "C++" code wich the "C" compiler cannot handle.

One solution to this problem is to only use the "C" functions in opencv (for example "opencv2/imgproc/imgproc_c.h" instead of "opencv2/imgproc/imgproc.hpp"). However, your function "detection_based_tracker.hpp" might not have a "C" version, as far as I can see.

The other option is to add a second file "function.cpp" with the "C++" functions that use opencv. The functions from "function.cpp" can be declared in a file "functions.h" and included in your "jni.c", so you can still use opencv c++ functions. Be careful to only use C style functions then, though (no classes, namespaces, ...) in your "function.h" file, otherwise you will have the same problems as before.

Upvotes: 1

Related Questions