android-opencv【 undefined reference to 】

OpenCV废弃函数与C/C++引用问题

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. opencv不同版本废弃的方法

2.3.1版本无此方法!!!

    cvCopyImage(_candiImg, *candiImg);         更改为:  cvCopy

参考文章:OpenCV中的cvCloneImage、cvCopyImage和cvCloneMat、cvCopyMat的误区

你好,为什么我的opencv2.1中使用cvCopyImage显示此函数没有定义??难道是在新的版本里面去除了?

问题解决了,现在只有cvCopy可以用


cvCopy与cvCloneImage的区别

/* Copies source array to destination array */
CVAPI(void)  cvCopy( const CvArr* src, CvArr* dst,
                     const CvArr* mask CV_DEFAULT(NULL) );

/* Creates a copy of IPL image (widthStep may differ) */
CVAPI(IplImage*) cvCloneImage( const IplImage* image );

如果设定了ROI等参数的时候,cvCopy只是复制被设定的区域,复制到一个和所设定参数相吻合的新的IplImage中
而cvCloneImage则是将整个IplImage结构复制到新的IplImage中,其中的ROI等参数也会一同复制。新的IplImage将会和原来的一模一样。

 

cvCopy的原型是:
void cvCopy( const CvArr* src, CvArr* dst, const CvArr* mask=NULL );
在使用这个函数之前,你必须用cvCreateImage()一类的函数先开一段内存,然后传递给dst。cvCopy会把src中的数据复制到dst的内存中。

cvCloneImage的原型是:
IplImage* cvCloneImage( const IplImage* image );
在使用函数之前,不用开辟内存。该函数会自己开一段内存,然后复制好image里面的数据,然后把这段内存中的数据返回给你。

clone是把所有的都复制过来,也就是说不论你是否设置Roi,Coi等影响copy的参数,clone都会原封不动的克隆过来。
copy就不一样,只会复制ROI区域等。用clone复制之后,源图像在内存中消失后,复制的图像也变了,而用copy复制,源图像消失后,复制的图像不变


2.opencv C和 C++ 引用文件的不同~~~ 以及extern "C"知识的引申


应该为 https://www.oschina.net/code/explore/OpenCV-2.1.0/interfaces/swig/filtered/cv.h

extern "C" void cvLine( CvArr* img, CvPoint pt1, CvPoint pt2,
	                     CvScalar color, int thickness = 1,
	                     int line_type = 8, int shift = 0 );
	 



编译出问题的几个函数均为C语言版本的,

C: int cvNamedWindow(const char* name, int flags)

C: void cvShowImage(const char* winname, const CvArr* image)
C: int cvWaitKey(int delay=0 )

C: void cvDestroyWindow(const char* name)


函数原型分别位于 

#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/core/core_c.h>
#include <opencv2/highgui/highgui_c.h>


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

SIOX.cpp:(.text._ZN3org4siox4Siox17extractForegroundERKNS0_9SioxImageEjP9_IplImagePS6_+0xace): undefined reference to `cvSmooth'
SIOX.cpp:(.text._ZN3org4siox4Siox17extractForegroundERKNS0_9SioxImageEjP9_IplImagePS6_+0xb72): undefined reference to `cvConvexHull2'
SIOX.cpp:(.text._ZN3org4siox4Siox17extractForegroundERKNS0_9SioxImageEjP9_IplImagePS6_+0xc2a): undefined reference to `cvLine'


这几个函数该如何链接呢???

在 <opencv2/core/core_c.h>中,

CVAPI(void)  cvLine( CvArr* img, CvPoint pt1, CvPoint pt2,
                     CvScalar color, int thickness CV_DEFAULT(1),
                     int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0) );
解嵌套宏之后,CVAPI(void)就是用extern "C" CV_EXPORTS void __cdecl替换就行,其中,CV_EXPORTS因为定义的时候为空,所以在这里只是占位,没有用到,可以不考虑。

(小插曲:http://hi.baidu.com/%C1%EE%BA%FC%B1%CF%BA%E8/blog/item/4dfaefdec3686d245882dd7e.html

在cv.h中看到如下这样一些函数声明 :

CVAPI(void) cvGetHuMoments( CvMoments* ...)
CVAPI(int) cvSampleLine( const CvArr* ...)
CVAPI(double) cvGetSpatialMoment( CvMoments* ...)

不知道这个CVAPI(type)是什么类型,转到定义

在cxtypes.h中看到它的定义是一个比较奇怪的宏:

#ifndef CVAPI
    #define CVAPI(rettype) CV_EXTERN_C CV_EXPORTS rettype CV_CDECL
#endif



类似问题描述(但并未解决):

"undefined reference to" is a linker error. You probably forgot to pass a library to link in on the command line. –  Daniel Fischer Jul 19 at 13:28


LOCAL_STATIC_LIBRARIES: The list of static libraries modules (built with BUILD_STATIC_LIBRARY) that should be linked to this module. This only makes sense in shared library modules.

LOCAL_SHARED_LIBRARIES: The list of shared libraries modules this module depends on at runtime. This is necessary at link time and to embed the corresponding information in the generated file.


怀疑:


Smooth
Smooths the image in one of several ways.
C: void cvSmooth(const CvArr* src, CvArr* dst, int smoothtype=CV_GAUSSIAN, int param1=3, int
param2=0, double param3=0, double param4=0)


Note: The function is now obsolete.过时的,废弃的,不使用的)Use GaussianBlur(), blur(), medianBlur() or bilateralFilter().


++++++++++++++++++++++++++


真相大白解决方案:(2012.7.29晚)

Android.mk文件中:

LOCAL_STATIC_LIBRARIES += libsioxdetection opencv_calib3d opencv_features2d opencv_flann opencv_imgproc opencv_core opencv_highgui

。。。include $(BUILD_SHARED_LIBRARY)

需要手动添加opencv的静态库!!!


反思:

siox文件已成功编译,故不应是此部分的编译错误。

本应立刻确定为链接问题。故应考虑是否是编译好的库未能正常设置!!!

灵感参考:

OpenCV undefined references


【原创】“collect2: ld returned 1 exit status”错误

      今天在编译android时碰到个问题,错误log如下:

      “out/target/product/xxxx/obj/lib/xxxxxx.so: undefined reference to `xxxxxxxx'” 

      “collect2: ld returned 1 exit status”,这个错误时因为在使用xxxxxx.so库时,库中的xxxxxxxx函数需要其他库文件或者没有实现。所以只需要将相应的库文件加到编译的路徑中.

++++++++++++++++++++++++++




++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

普通情况下解答:

/cygdrive/f/Class/CV/Exercise/test_opencv/main.cpp:13: undefined reference to `_cvLoadImage'
/cygdrive/f/Class/CV/Exercise/test_opencv/main.cpp:19: undefined reference to `cv::Mat::Mat(_IplImage const*, bool)'
/cygdrive/f/Class/CV/Exercise/test_opencv/main.cpp:35: undefined reference to `cv::cvtColor(cv::Mat const&, cv::Mat&, int, int)'
/cygdrive/f/Class/CV/Exercise/test_opencv/main.cpp:93: undefined reference to `cv::cvtColor(cv::Mat const&, cv::Mat&, int, int)'
/cygdrive/f/Class/CV/Exercise/test_opencv/main.cpp:96: undefined reference to `cv::namedWindow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/cygdrive/f/Class/CV/Exercise/test_opencv/main.cpp:100: undefined reference to `_cvShowImage'
/cygdrive/f/Class/CV/Exercise/test_opencv/main.cpp:104: undefined reference to `cv::waitKey(int)'

这个是一个常见的错误,通常就是由于没有包含一些库文件引起的,下面的步骤将解决这个问题


http://stackoverflow.com/questions/7816607/opencv-2-3-compiling-issue-undefined-refence-ubuntu-11-10

I am guessing that at least some of the libraries in the output of

pkg-config opencv --libs

are archive libraries. It is incorrect to put archive libraries before sources that need them (test_1.cpp in this case): the order of sources and libraries on the link linematters.

Try

g++ -o test_1 test_1.cpp `pkg-config opencv --cflags --libs` 
cvNamedWindow cvShowImage等也被更改~~~
### 关于 'undefined reference to CRsdk::' 的解决方案 编译错误 `'undefined reference to CRsdk::'` 是一种典型的链接阶段错误,通常表明在构建过程中缺少必要的库文件或者配置不正确。以下是可能的原因以及对应的解决办法: #### 1. **未正确指定库路径** 如果目标函数或类定义位于某个动态或静态库中,而该库未被正确链接到项目中,则会出现此问题。 - 确保已将包含 `CRsdk` 定义的库路径传递给编译器。 - 使用 `-L<library_path>` 参数来指定库所在的目录,并通过 `-l<lib_name>` 来链接特定的库[^1]。 例如,在命令行中可以尝试以下操作: ```bash g++ source_file.cpp -o output_binary -L/path/to/library -lCRsdk ``` #### 2. **头文件与实现分离** 某些情况下,虽然包含了正确的头文件,但如果实际的函数实现在另一个库中,也需要显式地链接那个库。 - 如果 `CRsdk` 函数由多个模块组成,请确认所有相关联的库都被成功加载并链接。 #### 3. **编译器版本差异引起的ABI兼容性问题** 当使用的工具链(如 GCC 或 Clang)版本不同步时,可能会引发 ABI 不匹配的情况,从而导致类似的链接失败现象[^5]。 - 验证当前开发环境中的编译器版本是否一致;如果不是同一版本系列的话,考虑升级至相同的大版本号以便获得更好的一致性支持。 #### 4. **Android平台下的特殊处理** 如果是基于 Android Studio 平台遇到此类问题,需注意 SDK 版本设置的影响因素[^2]: - 调整项目的最低 API 级别 (`minSdk`) 至满足 OpenCV 所需水平; - 同样也要同步调整 Native 层次上的 API LEVEL(`OpenCV_ANDROID_NATIVE_API_LEVEL`) ,使之相适应后再执行清理重建流程。 #### 5. **VSCode环境下OpenCV项目的额外注意事项** 对于那些利用 VS Code 开发且涉及 OpenCV 库调用的应用来说,还需要特别留意其构建脚本里是否有遗漏掉的关键参数设定[^4]: 添加 pkg-config 工具查询得到的相关标志位信息作为输入参数之一参与最终组装过程之中。 --- ### 示例代码片段展示如何修正上述情况 假设存在这样一个简单的测试案例需要修复其中存在的链接错误: ```cpp #include <iostream> #include "crsdk.h" int main(){ CRsdk::initialize(); std::cout << "Initialized CRSdk successfully." << std::endl; return 0; } ``` 那么完整的编译指令应当类似于下面这样写法: ```bash g++ test_cr_sdk.cpp -o cr_test -I/usr/include/crsdk -L/usr/lib -lcrsdk -std=c++17 ``` 这里假定 `/usr/include/crsdk` 存储着所需的头文件集合,而共享对象形式的 `.so` 文件则存放在标准位置即 `/usr/lib`. ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值