Ask Your Question

dnn::net forward in native C++ Android Studio

asked 2018-10-23 11:21:54 -0500

mats86 gravatar image

updated 2018-10-23 11:36:32 -0500

berak gravatar image


I want entwickelt native c++ App in Android studio but I come cross undefined reference to 'cv::dnn.Net::forward(out, layerNames)".

I need help how can I use net.forward(out, layerNames) in native c++.


hier ist my Code

#include <jni.h>
#include <string>

#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/dnn.hpp>

using namespace std;
using namespace cv;
using namespace cv::dnn;

extern "C" JNIEXPORT jstring JNICALL
    JNIEnv *env,
    jobject /* this */) {
std::string hello = "Hello from C++";

float confThreshold = 0.5;
float nmsThreshold = 0.4;
int newW = 320;
int newH = 320;
String model = "/storage/emulated/0/DCIM/ImageProcessing/tessdata/frozen_east_text_detection.pb";

string imPath = "/storage/emulated/0/DCIM/ImageProcessing/test.jpg";

Mat im = imread(imPath, IMREAD_COLOR);
Mat orig;
int origH = im.rows;
int origW = im.cols;

int rW = (int) (origW / float(newW));
int rH = (int) (origH / float(newH));

resize(im, im, Size(newW, newH));
int H = im.rows;
int W = im.cols;

vector<Mat> outs;
vector<String> layerNames(2);
layerNames[0] = "feature_fusion/Conv_7/Sigmoid";
layerNames[1] = "feature_fusion/concat_3";

Net net = readNet(model);

// construct a blob from the image and then perform a forward pass of
// the model to obtain the two output layer sets
Mat frame, blob;
blobFromImage(frame, blob, 1.0, Size(newW, newH), Scalar(123.68, 116.78, 103.94), true, false);
net.forward(outs, layerNames);

return env->NewStringUTF(hello.c_str());


undefined reference to `cv::dnn::experimental_dnn_34_v7::Net::forward(cv::_OutputArray const&,     std::__ndk1::vector<cv::String, std::__ndk1::allocator<cv::String> > const&)'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

hier is my Cmakelist

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

set(libs "${CMAKE_SOURCE_DIR}/src/main/jniLibs")

add_library(libopencv_java3 SHARED IMPORTED)
set_target_properties(libopencv_java3 PROPERTIES IMPORTED_LOCATION "${libs}/${ANDROID_ABI}/")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -frtti -fexceptions")

add_library( # Sets the name of the library.

    # Sets the library as a shared library.

    # Provides a relative path to your source file(s).

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.

    # Specifies the name of the NDK library that
    # you want CMake to locate.

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library ...
edit retag flag offensive close merge delete


I'm having the same issue. Were you able to resolve it?

JPaul gravatar imageJPaul ( 2018-11-29 14:20:46 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-11-30 09:23:14 -0500

JPaul gravatar image

I was able to get past this issue. The problem appears to have been caused because the NDK I was using was built against a different standard template library (libc++) than OpenCV was built against (gnustl). I changed my Android Studio project to use gnustl and then had to downgrade my NDK to v16 because the gnustl library is no longer part of newer NDKs. Doing this resolved all my linker issues. See the following discussions for more details. These are what helped me:

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-10-23 11:21:54 -0500

Seen: 124 times

Last updated: Oct 23 '18