Search code examples
c++opencvandroid-ndkandroid.mk

Android.mk - Include OpenCV directory for native C++ compliation with NDK


I'm importing a C++/OpenCV code to an Android app with NDK. First of all, I have to say that I don't have studies in programming, I'm just an amateur developer, so there're many things that I don't manage (specially, things out of coding).

I use QtCreator for my C++/OpenCV code and I wrote this line in my Project.pro:

INCLUDEPATH += path/to/OpenCV/main/dir #OpenCV-3.1.0
LIBS += -lopencv_core -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc

Now, I'm trying to make a "Hello World!" app only to be sure that I can compile with my C++ sources.

According to https://developer.android.com/ndk/guides/android_mk.html, I wrote this simple Android.mk file:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := myModule
LOCAL_CFLAGS += -I /path/to/OpenCV/main/dir
LOCAL_LDLIBS := -lopencv_core
LOCAL_SRC_FILES := bar.cpp

include $(BUILD_SHARED_LIBRARY)

In bar.cpp, I have:

#include <opencv2/core/core.hpp>

Just for checking that the compiler can found the sources. When I try to build my C++/OpenCV native code using ndk-build, I get:

non-system libraries in linker flags: -lopencv_core
Android NDK:     This is likely to result in incorrect builds. Try using LOCAL_STATIC_LIBRARIES or LOCAL_SHARED_LIBRARIES instead to list the library dependencies of the current module

I tryed to use LOCAL_STATIC_LIBRARIES and LOCAL_SHARED_LIBRARIES, but no succed. Any ideas?


P.S.:

It's important to say that I compiled a simple Hello world using NDK (wthout including any OpenCV header) following this example https://gist.github.com/gnuanu/252fd406f48f7da2c1c7.

There is a post here Android.mk: how to include ffmpeg and Opencv, but it has not answers and I can't solve with the info...


Solution

  • First, download OpenCV for Android. If you are only using OpenCV nativ, you have to set the following in your Android.mk file:

    LOCAL_PATH := $(call my-dir)
    
    CVROOT := path_to_opencv/OpenCV-android-sdk/sdk/native/jni
    
    include $(CLEAR_VARS)
    OPENCV_INSTALL_MODULES:=on
    OPENCV_LIB_TYPE:=STATIC
    include $(CVROOT)/OpenCV.mk
    
    LOCAL_MODULE += myModule
    
    LOCAL_C_INCLUDES += path_to_your_code/bar.h
    LOCAL_SRC_FILES += path_to_your_code/bar.cpp 
    
    LOCAL_CFLAGS += -std=c++11 -frtti -fexceptions -fopenmp -w
    LOCAL_LDLIBS += -llog -L$(SYSROOT)/usr/lib
    LOCAL_LDFLAGS += -fopenmp
    
    include $(BUILD_SHARED_LIBRARY)
    

    The cool thing is that OpenCV provides the OpenCV.mk makefile and you have not to do anything ;)