Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] opencv – How to compile and share two c++ libraries on an android build using CMake

Posted on November 11, 2022 By

Solution 1 :

the error you get is telling you that it cannot find opencv include file:

#include <opencv2/core.hpp>

you need to locate the opencv2 folder, and pass it to cmake as an additional include directory:

https://cmake.org/cmake/help/v3.0/command/include_directories.html

example: Adding OpenCV to Native C code through CMake on Android Studio

cmake_minimum_required(VERSION 3.4.1)
set(OpenCV_DIR "src/sdk/native/jni")
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV libraries: ${OpenCV_LIBS}")
include_directories(${OpenCV_INCLUDE_DIRS})

or you can use this approach:
https://github.com/ahasbini/Android-OpenCV/blob/master/app/CMakeLists.txt

Solution 2 :

It looks like your cmake project couldn’t find the installed opencv, so either you set your path to ${complete_path_to_opencv_installation}/apt or you install opencv in common installation folder

Either way, you’ll need to propagate this to your project:
In case you install this in typical place (e.g. /usr/local/ in linux you’ll need to set export PATH=$PATH:${opencv_installation_path} or c:Path in Windows, then set Path in Environment Variable.

By doing so, the find_package(OpenCV) should be able to find the opencv library and OpenCV_INCLUDE_DIRS is set.
This CMakeLists.txt should work:

cmake_minimum_required(VERSION 3)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

or you could also set OpenCV_INCLUDE_DIRS "./app/opencv2" before include_directories
to propagate opencv2/core.hpp.

You could also interested to this older post about using opencv with android.

Problem :

I have custom C++ classes which I want to run on my android app. I am successfully binded my C++ files using CMakeLists. But as my classes uses opencv, I am getting an issue which is obvious

fatal error: 'opencv2/core.hpp' file not found

Then I tried to add opencv library on my android app, downloaded the open cv android sdk and tired to add it on my project. Below is the folder structure

android 
   -> app
       -> opencv2
          All the opencv2 c++ files and folders
       ->folder1
          My custom c++ classes which will import opencv2.

The following is my CMakeLists.txt file

cmake_minimum_required(VERSION 3.4.1)  # for example

set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")

PROJECT(tag)


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

 add_library( tag_native
              # Sets the library as a shared library.
              SHARED
              # Provides a relative path to your source file(s).
              "./folder1/tag_native.cpp" )


add_library( opencv2
             # Sets the library as a shared library.
             SHARED
             # Provides a relative path to your source file(s).
             "./opencv2/" )

set_target_properties(opencv2 PROPERTIES LINKER_LANGUAGE CXX)

This is the sample of my cpp file

#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <vector>
#include <iostream>
#include <opencv2/core.hpp>

Everything from c++ compiles well but I am still getting the same issue

* What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
  Error while executing process C:UsersBrainants with arguments {TechnologyAppDataLocalAndroidSdkcmake3.10.2.4988404binninja.exe -C E:SqutagSqutag Mobileandroidapp.cxxcmakedebugarmeabi-v7a opencv2 squtag_native}
  ninja: Entering directory `E:SqutagSqutag Mobileandroidapp.cxxcmakedebugarmeabi-v7a'
  [1/2] Building CXX object CMakeFiles/squtag_native.dir/E_/Squtag/Squtag_Mobile/ios/Runner/Squtag_Native/squtag_native.cpp.o
  FAILED: CMakeFiles/squtag_native.dir/E_/Squtag/Squtag_Mobile/ios/Runner/Squtag_Native/squtag_native.cpp.o 
  C:UsersBRAINA~1AppDataLocalAndroidSdkndk210~1.611TOOLCH~1llvmprebuiltWINDOW~1binCLANG_~1.EXE --target=armv7-none-linux-androideabi24 --gcc-toolchain="C:/Users/Brainants Technology/AppData/Local/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/windows-x86_64" --sysroot="C:/Users/Brainants Technology/AppData/Local/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/windows-x86_64/sysroot"  -Dsqutag_native_EXPORTS  -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -Wformat -Werror=format-security  -fsanitize=address -fno-omit-frame-pointer -std=c++11 -O0 -fno-limit-debug-info  -fPIC -MD -MT CMakeFiles/squtag_native.dir/E_/Squtag/Squtag_Mobile/ios/Runner/Squtag_Native/squtag_native.cpp.o -MF CMakeFilessqutag_native.dirE_SqutagSqutag_MobileiosRunnerSqutag_Nativesqutag_native.cpp.o.d -o CMakeFiles/squtag_native.dir/E_/Squtag/Squtag_Mobile/ios/Runner/Squtag_Native/squtag_native.cpp.o -c "E:/Squtag/Squtag Mobile/ios/Runner/Squtag Native/squtag_native.cpp"
  E:/Squtag/Squtag Mobile/ios/Runner/Squtag Native/squtag_native.cpp:6:10: fatal error: 'opencv2/core.hpp' file not found
  #include <opencv2/core.hpp>
           ^~~~~~~~~~~~~~~~~~
  1 error generated.
  ninja: build stopped: subcommand failed.

Comments

Comment posted by 김선달

Forget about the guideline in your link. It’s totally about using opencv in Java. It’s simple like you did with your C++ files using CMakeLists. 1. Build OpenCV libraries for android devices 2. Copy the libraries and headers to your Android project and link that with CMakeLists.txt

READ  [FIXED] java - how to add coordinates in the segmentation of the watershed?
Powered by Inline Related Posts

Comment posted by Aawaz Gyawali

I tried that as well, but my import system didnt work. Basically I had to change the important statement by excluding the library name. The thing I wanted to know was how to bind the existing c++ code to a new library and use it on my original c++ file.

Comment posted by 김선달

The problem seems more likely about linking libraries with CMake. If that’s a problem, whether it’s Android or not isn’t important since you succeed binding main C++ to Android projects. I’m not very good at CMake, so why don’t you try searching linking libraries with CMake or reask with CMake tag?

Comment posted by sisik.eu/blog/android/ndk/opencv-without-java

If you’re using

Comment posted by 김선달

What I meant a

Comment posted by Aawaz Gyawali

This worked amazingly. Thanks for all the references.

Android Tags:android-ndk, c#, cmake, opencv

Post navigation

Previous Post: [FIXED] Android Studios error : Unable to locate adb location
Next Post: [FIXED] android – Here Maps Navigation samples seem to be missing the Navigation Library

Related Posts

[FIXED] android – Can a listener method run when activity is dead? Android
[FIXED] How to get data in Retrofit from nested JSON array using view binding android Android
[FIXED] java – Firebase recycleradapter with filter using searchview Android
[FIXED] java – Android Studio crashes when trying to google search words from TextEdit Android
[FIXED] android – UI not loading until I resize the app window or move it to floating window Android
[FIXED] android – “Lost connection to device” on specific hardware during web authentication Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme