vgonisanz
vgonisanz

Reputation: 11930

How find package in CMake with a custom path for OpenCV framework in iOS

I have CMake 3.0 and my own iOS project in C++ that use OpenCV as dependency. That project generate a group of libraries loaded by and application project.

In my CMake, I try to look for OpenCV dependency, It automatically in windows and linux, but in Android & iOS I have to set the correct package. With Android, setting ${OpenCV_dir}/sdk/native/jni works property with this code:

SET(OpenCV_DIR NOT_FOUND CACHE PATH "Path to use OpenCV")
IF(OpenCV_DIR STREQUAL NOT_FOUND)
  FIND_PACKAGE( OpenCV PATHS ${OpenCV_DIR})
  MESSAGE(FATAL_ERROR "--***-- Warning: Install and configure path to prebuilt OpenCVConfig.cmake")
ENDIF()

In iOS, this doesnt work. I usually create project Xcode project WITHOUT find OpenCV and then I drag and drop the framework and configure manually variable Framework Search Path with a custom path, in

/Users/Piperoman/Libraries/opencv2.4.9IOS

but using the CMake code doesnt find it.

What is the problem locating the framework?

Upvotes: 8

Views: 3328

Answers (1)

user2288008
user2288008

Reputation:

What is the problem locating the framework?

find_package looking for the OpenCVConfig.cmake file which doesn't exist in framework directory, i.e. OpenCV iOS simply is not designed so.

You can verify it by:

> wget http://sourceforge.net/projects/opencvlibrary/files/opencv-ios/3.0.0/opencv2.framework.zip
> unzip opencv2.framework.zip
> find opencv2.framework -type f -name "*.cmake"
# nothing found

Compare to Android:

> wget http://sourceforge.net/projects/opencvlibrary/files/opencv-android/3.0.0/OpenCV-3.0.0-android-sdk-1.zip
> unzip OpenCV-3.0.0-android-sdk-1.zip
> find OpenCV-android-sdk -type f -name "*.cmake"
...
OpenCV-android-sdk/sdk/native/jni/OpenCVConfig.cmake
...

If you're looking for CMake friendly solution you can try Hunter package manager, see pkg.opencv:

hunter_add_package(OpenCV)
find_package(OpenCV REQUIRED)

target_link_libraries(... PRIVATE ${OpenCV_LIBS})

Hunter iOS instructions

Since version 3.5 CMake supports installation of universal (multiarch, device + simulator) iOS libraries (see CMAKE_IOS_INSTALL_COMBINED).

Hunter use this feature since version 0.13.1.

Choose iOS toolchain, e.g. ios-8-2:

> git clone https://github.com/ruslo/polly
> ls polly/ios-8-2.cmake

CMakeLists.txt will looks like this:

cmake_minimum_required(VERSION 3.5)

include("cmake/HunterGate.cmake")
HunterGate(
    URL "https://github.com/ruslo/hunter/archive/v0.13.1.tar.gz"
    SHA1 "bd7711df37a53134e642220d2f649a69cb34dde3"
)

project(TestOpenCV)

hunter_add_package(OpenCV)
find_package(OpenCV REQUIRED)

add_executable(foo foo.cpp)
target_link_libraries(foo PUBLIC ${OpenCV_LIBS})

Build it:

> cmake -H. -B_builds -DCMAKE_TOOLCHAIN_FILE=/.../polly/ios-8-2.cmake -GXcode
> cmake --build _builds --config Release

Alternatively you can use build.py script to pick toolchain and generator:

> git clone https://github.com/ruslo/polly
> export PATH=`pwd`/polly/bin:$PATH
> which build.py

Build it (build.py variant):

> build.py --toolchain ios-8-2 --verbose --config Release

Upvotes: 10

Related Questions