Opencv extra-modules link failed

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 :

[Edit]: error log text version:

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:

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:

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

#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

try to follow opencv’s module folder structure

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 !

Hi @berak ,
it looks same with my modules, here is mine: folder structure.

I find that the cmake instr from opencv/opencv_contrib

 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?

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.

imo, your CMakeLists.txt

only needs 2 lines:

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) !

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

ocv_define_module(matlib core imgproc WRAP python) 

into

ocv_define_module(matlib opencv_core opencv_imgproc WRAP python)

and every thing is working!

1 Like