# Opencv extra-modules link failed

**URL:** <https://forum.opencv.org/t/opencv-extra-modules-link-failed/4502>\
**Category:** C++\
**Tags:** cmake, build\
**Created:** [July 28, 2021, 9:04am UTC](https://forum.opencv.org/t/opencv-extra-modules-link-failed/4502 "2021-07-28T09:04:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![flycat](https://avatars.discourse-cdn.com/v4/letter/f/ed8c4c/32.png) [@flycat](https://forum.opencv.org/u/flycat)\
**Post date:** [July 28, 2021, 9:04am UTC](https://forum.opencv.org/t/opencv-extra-modules-link-failed/4502/1 "2021-07-28T09:04:32Z")

</div>

Hi, I’m trying to learn how to **define my own opencv extra module and build it out of the sources folder**. I have write a simple test code and I meet some **link error** while building on window’s msvc :

 ![UdBeY](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/9/972d879ba3d4664fd1f873ab5e97e2b8230885de.png)

[Edit]: error log text version:

```auto
20>matlib.obj : error LNK2019: 在函式 "public: class cv::Mat __cdecl cv::_InputArray::getMat_(int)const " (?getMat_@_InputArray@cv@@QEBA?AVMat@2@H@Z)"public: void__cdecl cv::matlib::transform::Inc(class cv::_InputArray const &,class cv::_OutputArray const &)" (?Inc@transform@matlib@cv@@QEAAXAEBV_InputArray@3@AEBV_OutputArray@3@@Z) 中參考了無法解析的外部符號
20>opencv_matlib_pch.obj : error LNK2001: 無法解析的外部符號 "public: class cv::Mat __cdecl cv::_InputArray::getMat_(int)const " (?getMat_@_InputArray@cv@@QEBA?AVMat@2@H@Z)
20>matlib.obj : error LNK2019: 在函式 "public: enum cv::_InputArray::KindFlag __cdecl cv::_InputArray::kind(void)const " (?kind@_InputArray@cv@@QEBA?AW4KindFlag@12@XZ)"public: void__cdecl cv::matlib::transform::Inc(class cv::_InputArray const &,class cv::_OutputArray const &)" 

```

The chinese “無法解析的外部符號” is equal to [_unresolved external symbol_]

I try to build this extra module(matlib.cpp) with opencv source in: F:/opencv-master with CMakeLists.txt:

```auto
set(the_description "matlib module")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR})
SET(CMAKE_CXX_FLAGS "/O2")
include_directories(F:/opencv-master/modules/core/include)
include_directories(./include)
set(MODEL_SRCS matlib.cpp)
ocv_glob_module_sources(${MODEL_SRCS})
ocv_define_module(matlib WRAP python)

```

When I run OpenCV.sln and build the ALL\_BUILD I meet the link error above. I know I should add some .lib path or include path in mscv settings but I don’t know which one causing the error. Any Idea?

My custom module:

matlib.cpp:

```auto
#include "precomp.hpp"
#include <iostream>

namespace cv {
namespace matlib {

void transform::Inc(InputArray src, OutputArray dst)
{
    cv::Mat M = src.getMat();
    int image_width = M.cols;
    int image_height = M.rows;

    std::cout << "arr in c++: " << std::endl << " " << M << std::endl << std::endl;

    for(int i = 0; i < M.rows; i++)
    {
        double* Mi = M.ptr<double>(i);
        for(int j = 0; j < M.cols; j++)
            Mi[j] = Mi[j] + 1;
    }

    M.copyTo(dst);
}   

} // Matlib
} // cv

```

matlib.hpp:

```auto
#ifndef __OPENCV_matlib_HPP__
#define __OPENCV_matlib_HPP__

#include "opencv2/core.hpp"

namespace cv {
namespace matlib {

class CV_EXPORTS_W transform 
{
public:
    CV_WRAP transform() {}
    virtual ~transform() {}

    CV_WRAP void Inc(InputArray src, OutputArray dst);
};
} // matlib
} // cv
#endif

```

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [July 28, 2021, 10:01am UTC](https://forum.opencv.org/t/opencv-extra-modules-link-failed/4502/3 "2021-07-28T10:01:14Z")

</div>

try to follow [opencv’s module folder structure](https://github.com/opencv/opencv_contrib/tree/master/modules/dpm)

please also have a look at the CMakeLists.txt there, yours looks far too complicated

and dont forget to rerun cmake, so it can pickup changes !

---

<div class="post-metadata">

**Author:** ![flycat](https://avatars.discourse-cdn.com/v4/letter/f/ed8c4c/32.png) [@flycat](https://forum.opencv.org/u/flycat)\
**Post date:** [July 28, 2021, 10:51am UTC](https://forum.opencv.org/t/opencv-extra-modules-link-failed/4502/4 "2021-07-28T10:51:52Z")

</div>

Hi @berak ,  
it looks same with my modules, here is mine: [folder structure](https://github.com/risiair/pybind/tree/main/python_cpp_bindings/modules/matlib).

I find that the cmake instr from opencv/opencv\_contrib

```auto
 cmake -DOPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules <opencv_source_directory>

```

is different from mine, I don’t have \<opencv\_source\_directory\> this part, does that cause a problem?

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [July 28, 2021, 1:08pm UTC](https://forum.opencv.org/t/opencv-extra-modules-link-failed/4502/5 "2021-07-28T13:08:18Z")

</div>

> [@flycat](#):
>
> I don’t have \<opencv\_source\_directory\> this part, does that cause a problem?

you need it.  
last item in the cmake cmdline must be the opencv top level folder

also, your github shows, that you try to build inside your src folder. DONT !  
(you cant ever clean up again !)

make a seperate build folder, run cmake from there, & point it at your src.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [July 28, 2021, 1:59pm UTC](https://forum.opencv.org/t/opencv-extra-modules-link-failed/4502/6 "2021-07-28T13:59:50Z")

</div>

imo, your [CMakeLists.txt](https://github.com/risiair/pybind/blob/main/python_cpp_bindings/modules/matlib/CMakeLists.txt)

only needs 2 lines:

```auto
set(the_description "matlib module")
ocv_define_module(matlib core imgproc WRAP python)

```

if you think, you need more, then there’s something wrong !  
but you must specify your module’s dependancies (e.g. core, imgproc) !

---

<div class="post-metadata">

**Author:** ![flycat](https://avatars.discourse-cdn.com/v4/letter/f/ed8c4c/32.png) [@flycat](https://forum.opencv.org/u/flycat)\
**Post date:** [August 1, 2021, 8:09am UTC](https://forum.opencv.org/t/opencv-extra-modules-link-failed/4502/7 "2021-08-01T08:09:52Z")

</div>

Thanks! This is **very** helpful, after I comment all other lines of code in CmakeLists.txt and change

```nohighlight
ocv_define_module(matlib core imgproc WRAP python) 

```

into

```nohighlight
ocv_define_module(matlib opencv_core opencv_imgproc WRAP python)

```

and every thing is working!
