使用为 android 交叉编译的 opencv 共享库中的函数的问题
Issue using functions from opencv shared libraries cross compiled for android
我正在尝试构建和执行 link 我为 android 交叉编译的几个 opencv 共享库的 cpp 代码,但在尝试利用命名空间 cv 或尝试使用opencv的功能,它说未声明的标识符。
我确实在此处尝试了此文档:https://developer.android.com/ndk/guides/prebuilts 但无法使其正常工作。
此外,我还提到了另一个 Whosebug 问题以供参考:OpenCV with Android NDK Undefined References。
任何关于如何 link 它们并正确导入 opencv 函数的指导都会非常有帮助,我可能在这里错过了。
trial_onnx.cpp 文件
#include <iostream>
#include <fstream>
#include <cstring>
#include <opencv2/ml/ml.hpp>
#include <opencv2/dnn/dnn.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/core/mat.hpp>
#include "trial_onnx.h"
using namespace std;
void execute_main() {
std::cout << "Hello World"<<std::endl;
cv::Mat mat1;
}
trial_onnx.h 文件
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void execute_main();
#ifdef __cplusplus
}
#endif // __cplusplus
Android.mk 文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := opencv_ml
LOCAL_SRC_FILES := /home/ubuntu/trial/opencv-4.5.5-android-sdk/OpenCV-android-sdk/sdk/native/libs/arm64-v8a/libopencv_ml.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := opencv_dnn
LOCAL_SRC_FILES := /home/ubuntu/trial/opencv-4.5.5-android-sdk/OpenCV-android-sdk/sdk/native/libs/arm64-v8a/libopencv_dnn.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
#LOCAL_PATH := $(call my-dir)
LOCAL_MODULE := opencv_imgcodecs
LOCAL_SRC_FILES := /home/ubuntu/trial/opencv-4.5.5-android-sdk/OpenCV-android-sdk/sdk/native/libs/arm64-v8a/libopencv_imgcodecs.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
#LOCAL_PATH := $(call my-dir)
LOCAL_MODULE := opencv_imgproc
LOCAL_SRC_FILES := /home/ubuntu/trial/opencv-4.5.5-android-sdk/OpenCV-android-sdk/sdk/native/libs/arm64-v8a/libopencv_imgproc.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
#LOCAL_PATH := $(call my-dir)
LOCAL_MODULE := opencv_core
LOCAL_SRC_FILES := /home/ubuntu/trial/opencv-4.5.5-android-sdk/OpenCV-android-sdk/sdk/native/libs/arm64-v8a/libopencv_core.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := opencv_highgui
LOCAL_SRC_FILES := /home/ubuntu/trial/opencv-4.5.5-android-sdk/OpenCV-android-sdk/sdk/native/libs/arm64-v8a/libopencv_highgui.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_SHARED_LIBRARIES = opencv_ml opencv_dnn opencv_imgcodecs opencv_highgui opencv_imgproc opencv_core opencv
LOCAL_ARM_MODE := arm
LOCAL_MODULE := libtrial
LOCAL_SRC_FILES := inc/trial_onnx.h src/trial_onnx.cpp
LOCAL_C_INCLUDES := ${LOCAL_PATH}/inc
LOCAL_LDLIBS += -llog -ldl
输出
[arm64-v8a] Install : libopencv_core.so => libs/arm64-v8a/libopencv_core.so
[arm64-v8a] Install : libopencv_dnn.so => libs/arm64-v8a/libopencv_dnn.so
[arm64-v8a] Install : libopencv_highgui.so => libs/arm64-v8a/libopencv_highgui.so
[arm64-v8a] Install : libopencv_imgcodecs.so => libs/arm64-v8a/libopencv_imgcodecs.so
[arm64-v8a] Install : libopencv_imgproc.so => libs/arm64-v8a/libopencv_imgproc.so
[arm64-v8a] Install : libopencv_ml.so => libs/arm64-v8a/libopencv_ml.so
[arm64-v8a] Compile++ : trial <= trial_onnx.cpp
[arm64-v8a] SharedLibrary : lib_trial.so
./obj/local/arm64-v8a/objs/trial/src/trial_onnx.o: In function `execute_main':
/home/ubuntu//trial/./src/trial_onnx.cpp:19: undefined reference to `cv::Mat::Mat()'
/home/ubuntu/trial/./src/trial_onnx.cpp:21: undefined reference to `cv::Mat::~Mat()'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [obj/local/arm64-v8a/lib_trial.so] Error 1
如果我跳过在 cpp 文件中使用 cv::,会弹出以下输出,这确实有意义,但它能够识别 cv::Mat 类型存在于 core/mat.hpp 那么为什么无法识别 cv::mat 在其他情况下让我感到困惑:
[arm64-v8a] Install : libopencv_core.so => libs/arm64-v8a/libopencv_core.so
[arm64-v8a] Install : libopencv_dnn.so => libs/arm64-v8a/libopencv_dnn.so
[arm64-v8a] Install : libopencv_highgui.so => libs/arm64-v8a/libopencv_highgui.so
[arm64-v8a] Install : libopencv_imgcodecs.so => libs/arm64-v8a/libopencv_imgcodecs.so
[arm64-v8a] Install : libopencv_imgproc.so => libs/arm64-v8a/libopencv_imgproc.so
[arm64-v8a] Install : libopencv_ml.so => libs/arm64-v8a/libopencv_ml.so
[arm64-v8a] Compile++ : trial <= trial_onnx.cpp
[arm64-v8a] SharedLibrary : lib_trial.so
./src/trial_onnx.cpp:19:5: error: unknown type name 'Mat'; did you mean
'cv::Mat'?
Mat mat1;
^~~
cv::Mat
./opencv2/core/mat.hpp:801:18: note: 'cv::Mat' declared here
class CV_EXPORTS Mat
^
1 error generated.
我自己解决了这个问题,这是一个链接问题以及相应地修改 Android.mk 文件。对于可能遇到此问题的其他人:
修复后我的 Android.Mk 文件看起来像
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_ARM_MODE := arm
include $(CLEAR_VARS)
LOCAL_MODULE := opencv_core
LOCAL_SRC_FILES := /home/ubuntu/opencv/modules/core/libopencv_core.so
LOCAL_EXPORT_C_INCLUDES := /home/ubuntu/opencv/modules/core/include
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := opencv_imgcodecs
LOCAL_SRC_FILES := /home/ubuntu/opencv/modules/imgcodecs/include/opencv2/libopencv_imgcodecs.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := lib_trial
LOCAL_SRC_FILES := inc/trial_onnx.h src/trial_onnx.cpp
LOCAL_C_INCLUDES := ${LOCAL_PATH}/inc
LOCAL_LDLIBS += -llog -ldl
LOCAL_SHARED_LIBRARIES := opencv_core opencv_imgcodecs
include $(BUILD_SHARED_LIBRARY)
我的 CPP 文件:trial_onnx.cpp 看起来像:
#include <iostream>
#include <fstream>
#include <cstring>
#include </home/ubuntu/opencv/modules/core/include/opencv2/core.hpp>
#include </home/ubuntu/opencv/modules/imgcodecs/include/opencv2/imgcodecs.hpp>
#include </home/ubuntu/opencv/modules/core/include/opencv2/core/mat.hpp>
#include "trial_onnx.h"
using namespace std;
void execute_main() {
std::cout << "Hello World"<<std::endl;
cv::Mat imageBGR;
imageBGR = cv::imread("cat.jpeg", 0);
std::cout << "Width : " << imageBGR.cols << endl;
std::cout << "Height: " << imageBGR.rows << endl;
}
其余文件保持不变。
我正在尝试构建和执行 link 我为 android 交叉编译的几个 opencv 共享库的 cpp 代码,但在尝试利用命名空间 cv 或尝试使用opencv的功能,它说未声明的标识符。 我确实在此处尝试了此文档:https://developer.android.com/ndk/guides/prebuilts 但无法使其正常工作。 此外,我还提到了另一个 Whosebug 问题以供参考:OpenCV with Android NDK Undefined References。 任何关于如何 link 它们并正确导入 opencv 函数的指导都会非常有帮助,我可能在这里错过了。
trial_onnx.cpp 文件
#include <iostream>
#include <fstream>
#include <cstring>
#include <opencv2/ml/ml.hpp>
#include <opencv2/dnn/dnn.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/core/mat.hpp>
#include "trial_onnx.h"
using namespace std;
void execute_main() {
std::cout << "Hello World"<<std::endl;
cv::Mat mat1;
}
trial_onnx.h 文件
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void execute_main();
#ifdef __cplusplus
}
#endif // __cplusplus
Android.mk 文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := opencv_ml
LOCAL_SRC_FILES := /home/ubuntu/trial/opencv-4.5.5-android-sdk/OpenCV-android-sdk/sdk/native/libs/arm64-v8a/libopencv_ml.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := opencv_dnn
LOCAL_SRC_FILES := /home/ubuntu/trial/opencv-4.5.5-android-sdk/OpenCV-android-sdk/sdk/native/libs/arm64-v8a/libopencv_dnn.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
#LOCAL_PATH := $(call my-dir)
LOCAL_MODULE := opencv_imgcodecs
LOCAL_SRC_FILES := /home/ubuntu/trial/opencv-4.5.5-android-sdk/OpenCV-android-sdk/sdk/native/libs/arm64-v8a/libopencv_imgcodecs.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
#LOCAL_PATH := $(call my-dir)
LOCAL_MODULE := opencv_imgproc
LOCAL_SRC_FILES := /home/ubuntu/trial/opencv-4.5.5-android-sdk/OpenCV-android-sdk/sdk/native/libs/arm64-v8a/libopencv_imgproc.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
#LOCAL_PATH := $(call my-dir)
LOCAL_MODULE := opencv_core
LOCAL_SRC_FILES := /home/ubuntu/trial/opencv-4.5.5-android-sdk/OpenCV-android-sdk/sdk/native/libs/arm64-v8a/libopencv_core.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := opencv_highgui
LOCAL_SRC_FILES := /home/ubuntu/trial/opencv-4.5.5-android-sdk/OpenCV-android-sdk/sdk/native/libs/arm64-v8a/libopencv_highgui.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_SHARED_LIBRARIES = opencv_ml opencv_dnn opencv_imgcodecs opencv_highgui opencv_imgproc opencv_core opencv
LOCAL_ARM_MODE := arm
LOCAL_MODULE := libtrial
LOCAL_SRC_FILES := inc/trial_onnx.h src/trial_onnx.cpp
LOCAL_C_INCLUDES := ${LOCAL_PATH}/inc
LOCAL_LDLIBS += -llog -ldl
输出
[arm64-v8a] Install : libopencv_core.so => libs/arm64-v8a/libopencv_core.so
[arm64-v8a] Install : libopencv_dnn.so => libs/arm64-v8a/libopencv_dnn.so
[arm64-v8a] Install : libopencv_highgui.so => libs/arm64-v8a/libopencv_highgui.so
[arm64-v8a] Install : libopencv_imgcodecs.so => libs/arm64-v8a/libopencv_imgcodecs.so
[arm64-v8a] Install : libopencv_imgproc.so => libs/arm64-v8a/libopencv_imgproc.so
[arm64-v8a] Install : libopencv_ml.so => libs/arm64-v8a/libopencv_ml.so
[arm64-v8a] Compile++ : trial <= trial_onnx.cpp
[arm64-v8a] SharedLibrary : lib_trial.so
./obj/local/arm64-v8a/objs/trial/src/trial_onnx.o: In function `execute_main':
/home/ubuntu//trial/./src/trial_onnx.cpp:19: undefined reference to `cv::Mat::Mat()'
/home/ubuntu/trial/./src/trial_onnx.cpp:21: undefined reference to `cv::Mat::~Mat()'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [obj/local/arm64-v8a/lib_trial.so] Error 1
如果我跳过在 cpp 文件中使用 cv::,会弹出以下输出,这确实有意义,但它能够识别 cv::Mat 类型存在于 core/mat.hpp 那么为什么无法识别 cv::mat 在其他情况下让我感到困惑:
[arm64-v8a] Install : libopencv_core.so => libs/arm64-v8a/libopencv_core.so
[arm64-v8a] Install : libopencv_dnn.so => libs/arm64-v8a/libopencv_dnn.so
[arm64-v8a] Install : libopencv_highgui.so => libs/arm64-v8a/libopencv_highgui.so
[arm64-v8a] Install : libopencv_imgcodecs.so => libs/arm64-v8a/libopencv_imgcodecs.so
[arm64-v8a] Install : libopencv_imgproc.so => libs/arm64-v8a/libopencv_imgproc.so
[arm64-v8a] Install : libopencv_ml.so => libs/arm64-v8a/libopencv_ml.so
[arm64-v8a] Compile++ : trial <= trial_onnx.cpp
[arm64-v8a] SharedLibrary : lib_trial.so
./src/trial_onnx.cpp:19:5: error: unknown type name 'Mat'; did you mean
'cv::Mat'?
Mat mat1;
^~~
cv::Mat
./opencv2/core/mat.hpp:801:18: note: 'cv::Mat' declared here
class CV_EXPORTS Mat
^
1 error generated.
我自己解决了这个问题,这是一个链接问题以及相应地修改 Android.mk 文件。对于可能遇到此问题的其他人: 修复后我的 Android.Mk 文件看起来像
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_ARM_MODE := arm
include $(CLEAR_VARS)
LOCAL_MODULE := opencv_core
LOCAL_SRC_FILES := /home/ubuntu/opencv/modules/core/libopencv_core.so
LOCAL_EXPORT_C_INCLUDES := /home/ubuntu/opencv/modules/core/include
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := opencv_imgcodecs
LOCAL_SRC_FILES := /home/ubuntu/opencv/modules/imgcodecs/include/opencv2/libopencv_imgcodecs.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := lib_trial
LOCAL_SRC_FILES := inc/trial_onnx.h src/trial_onnx.cpp
LOCAL_C_INCLUDES := ${LOCAL_PATH}/inc
LOCAL_LDLIBS += -llog -ldl
LOCAL_SHARED_LIBRARIES := opencv_core opencv_imgcodecs
include $(BUILD_SHARED_LIBRARY)
我的 CPP 文件:trial_onnx.cpp 看起来像:
#include <iostream>
#include <fstream>
#include <cstring>
#include </home/ubuntu/opencv/modules/core/include/opencv2/core.hpp>
#include </home/ubuntu/opencv/modules/imgcodecs/include/opencv2/imgcodecs.hpp>
#include </home/ubuntu/opencv/modules/core/include/opencv2/core/mat.hpp>
#include "trial_onnx.h"
using namespace std;
void execute_main() {
std::cout << "Hello World"<<std::endl;
cv::Mat imageBGR;
imageBGR = cv::imread("cat.jpeg", 0);
std::cout << "Width : " << imageBGR.cols << endl;
std::cout << "Height: " << imageBGR.rows << endl;
}
其余文件保持不变。