Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

android studio cmake里同时加入opencv和ncnn后报错:use of typeid requires -frtti #3231

Closed
ljmkimqx opened this issue Sep 17, 2021 · 4 comments

Comments

@ljmkimqx
Copy link

ljmkimqx commented Sep 17, 2021

报的错误:

In file included from /Users/ljm/Desktop/ProjectAndroid/JXA/app/src/main/cpp/mtcnnFace/tracking-lib.cpp:4:
In file included from /Users/ljm/Desktop/ProjectAndroid/JXA/app/src/main/cpp/include/opencv2/opencv.hpp:65:
In file included from /Users/ljm/Desktop/ProjectAndroid/JXA/app/src/main/cpp/include/opencv2/flann.hpp:48:
In file included from /Users/ljm/Desktop/ProjectAndroid/JXA/app/src/main/cpp/include/opencv2/flann/flann_base.hpp:40:
In file included from /Users/ljm/Desktop/ProjectAndroid/JXA/app/src/main/cpp/include/opencv2/flann/params.h:33:
/Users/ljm/Desktop/ProjectAndroid/JXA/app/src/main/cpp/include/opencv2/flann/any.h:58:63: error: use of typeid requires -frtti

之前已经成功集成ncnn在项目里了,并且可以正常使用。
然后我想尝试其他模型,并且需要opencv,我就按照下面的cmake进行配置,但报了上面的错,我尝试cmake里加上这句

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -frtti")

但也报相同的错误

cmake:

project(mtcnnFace)

cmake_minimum_required(VERSION 3.4.1)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -frtti")

add_library(lib_opencv_flann SHARED IMPORTED)
set_target_properties(lib_opencv_flann PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libopencv_flann.a)

add_library(lib_opencv SHARED IMPORTED)
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libopencv_java4.so)

set(ncnn_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../ncnnFace/${ANDROID_ABI}/lib/cmake/ncnn)
find_package(ncnn REQUIRED)

include_directories(mtcnn.h LandmarkTracking.h)

add_library(mtcnnFace SHARED mtcnn.cpp tracking-lib.cpp)

#C++日志
find_library(log-lib log)

target_link_libraries(mtcnnFace
        ncnn
        lib_opencv
        lib_opencv_flann
        ${log-lib}
        android
        jnigraphics
        )

ndk: 21.3.65
android studio: 2020.3.1

请问这可能是什么问题,弄了很久

@ljmkimqx ljmkimqx changed the title android studio cmake里同时假如opencv和ncnn后报错:use of typeid requires -frtti android studio cmake里同时加入opencv和ncnn后报错:use of typeid requires -frtti Sep 17, 2021
@nihui
Copy link
Member

nihui commented Sep 17, 2021

方案1:自行编译ncnn库,设置cmake选项 -DNCNN_DISABLE_RTTI=OFF
方案2:使用 https://github.com/nihui/opencv-mobile 替换官方的opencv

@ljmkimqx
Copy link
Author

采用了方案1,并添加了 -DNCNN_DISABLE_RTTI=OFF(环境信息: ndk:21.4.7075529 ncnn: ncnn-20210720源码)
放到项目后rebuild,不报 error: use of typeid requires -frtti 错误了
但报了另一个错误:cannot use 'throw' with exceptions disabled

In file included from /Users/ljm/Desktop/ProjectAndroid/JXA/app/src/main/cpp/mtcnnFace/tracking-lib.cpp:4:
In file included from /Users/ljm/Desktop/ProjectAndroid/JXA/app/src/main/cpp/include/opencv2/opencv.hpp:65:
In file included from /Users/ljm/Desktop/ProjectAndroid/JXA/app/src/main/cpp/include/opencv2/flann.hpp:48:
In file included from /Users/ljm/Desktop/ProjectAndroid/JXA/app/src/main/cpp/include/opencv2/flann/flann_base.hpp:40:
In file included from /Users/ljm/Desktop/ProjectAndroid/JXA/app/src/main/cpp/include/opencv2/flann/params.h:33:
/Users/ljm/Desktop/ProjectAndroid/JXA/app/src/main/cpp/include/opencv2/flann/any.h:274:42: error: cannot use 'throw' with exceptions disabled

按照网上说的在build.gradle里添加“-fexceptions”也不行

我在APP的build.gradle里android的defaultConfig配置如下

defaultConfig {
        applicationId "加密加密加密"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        externalNativeBuild {
            cmake {
                arguments "DANDROID_TOOLCHAIN=clang"
                cppFlags "-std=c++11 -fexceptions"
                abiFilters "armeabi-v7a", "arm64-v8a"
            }
        }
    }

cmake文件内容和上面的一样
opencv: 4以上
ndk: 21.3.65
android studio: 2020.3.1

@nihui
Copy link
Member

nihui commented Sep 19, 2021

-DNCNN_DISABLE_EXCEPTION=OFF

@ljmkimqx
Copy link
Author

可以了 谢谢nihui

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants