在iOS中如何使用OpenCV框架的自定义路径在CMake中找到包

时间:2021-12-02 04:50:10

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.

我有CMake 3.0和我自己的使用OpenCV作为依赖项的c++ iOS项目。该项目生成一组由应用程序和应用程序加载的库。

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:

在我的CMake中,我尝试寻找OpenCV依赖项,它在windows和linux中是自动的,但是在Android和iOS中,我必须设置正确的包。使用Android,使用以下代码设置${OpenCV_dir}/sdk/native/jni works属性:

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

在iOS中,这行不通。我通常在没有找到OpenCV的情况下创建项目Xcode项目,然后拖放框架并使用自定义路径配置手动变量框架搜索路径

/Users/Piperoman/Libraries/opencv2.4.9IOS

but using the CMake code doesnt find it.

但是使用CMake代码找不到它。

What is the problem locating the framework?

定位框架的问题是什么?

1 个解决方案

#1


9  

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.

查找OpenCVConfig的find_package。cmake文件在框架目录中是不存在的,即OpenCV iOS的设计不是这样的。

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:

与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:

如果您正在寻找CMake友好的解决方案,您可以尝试Hunter package manager,参见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).

由于版本3.5 CMake支持安装universal (multiarch, device + simulator) iOS库(参见cmake_ios_install_combination)。

Hunter use this feature since version 0.13.1.

自0.13.1版本以来,猎人使用了这个特性。

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

选择iOS工具链,如iOS 8-2:

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

CMakeLists.txt will looks like this:

CMakeLists。txt将如下所示:

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:

也可以使用build。py脚本选择工具链和生成器:

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

Build it (build.py variant):

(构建构建它。py变体):

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

#1


9  

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.

查找OpenCVConfig的find_package。cmake文件在框架目录中是不存在的,即OpenCV iOS的设计不是这样的。

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:

与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:

如果您正在寻找CMake友好的解决方案,您可以尝试Hunter package manager,参见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).

由于版本3.5 CMake支持安装universal (multiarch, device + simulator) iOS库(参见cmake_ios_install_combination)。

Hunter use this feature since version 0.13.1.

自0.13.1版本以来,猎人使用了这个特性。

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

选择iOS工具链,如iOS 8-2:

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

CMakeLists.txt will looks like this:

CMakeLists。txt将如下所示:

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:

也可以使用build。py脚本选择工具链和生成器:

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

Build it (build.py variant):

(构建构建它。py变体):

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