Describe in detail the whole process of C++ calling opencv to compile into dll and calling

This article details the whole process of generating and calling a dynamic link library DLL in the Vs2019+OpenCV environment

<1> Generate a dynamic link library:

1. Vs create a new project --> dynamic link library (DLL)

2. Next step --> (fill in your own project name, choose your own project location) create

3. Header file--add--new item--.h file--add

 4. Edit the .h file (for example: the Test.h I generated)

#pragma once
#include <opencv2/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgproc/imgproc_c.h>
#include <iostream>

using namespace std;
using namespace cv;

extern "C" __declspec(dllexport) void thresholdIntegral(Mat inputMat, Mat outputMat);

 Among them, extern "C": Tell the compiler to compile the code modified by it in C language.
            __declspec(dllexport): Tell the compiler and linker that the function or variable modified by it needs to be exported from the DLL for use by other applications; there is another sentence of code that is __declspec(dllimport), the function of this modifier is Tells the compiler and linker that the function or variable decorated by it needs to be imported from the DLL, and it will also be used later.
           void thresholdIntegral(Mat inputMat, Mat outputMat): It is a function that needs to be called by other programs. (The explanation here comes from the original article of CSDN blogger "Aishen1024", the original link: https://blog.csdn.net/qq_30139555/article/details/103621955)

5. Source file--add--new item--.cpp file--add

 6. Edit the .cpp file (for example: the Test.cpp I generated)

        Remember to add #include "pch.h" at the top

 At this point, the DLL code is written.

7. Compile

It will show success. The above prompt information is irrelevant, because this is a dll, not an .exe application, and it cannot be started by itself. Click OK to ignore.

<2> Configure OpenCV:

After the above operations, you will find that the headers about the opencv library are reporting errors, and you will be fine after configuring opencv.

First of all, I have been using the debug x64 platform

1. Project--Properties

 1) VC++ directory--include directory--edit

  Add the two paths under the opencv path you downloaded:

        D:\Program Files\Visual studio 2019\OpenCV\opencv\build\include

        D:\Program Files\Visual studio 2019\OpenCV\opencv\build\include\opencv2

  click OK

2) VC++ directory--library directory--edit

   Add a path under the opencv path you downloaded:

        D:\Program Files\Visual studio 2019\OpenCV\opencv\build\x64\vc15\lib

   click OK

 3) Linker--Input--Additional Items--Edit

   Add the opencv_worldxxxd.lib under the opencv path you downloaded, and its path is the path you just added to the library directory:

        D:\Program Files\Visual studio 2019\OpenCV\opencv\build\x64\vc15\lib

  Click OK all the way

 The above opencv is all configured.

<3> Call the dynamic link library:

1. Create a new C++ project (eg: DllTest), and configure opencv according to <2>

 2. Call the DLL

    1) Copy the "Dll1.dll" and "Dll1.lib" files in the x64\Debug folder under the generated project directory of Dll1 to the " DllTest\DllTest" folder.

At this time, the problem came, and we found that we only generated dll files, but lacked the corresponding lib files, so we need to solve this problem.

PS: Open the previous dll1.sln

Right click on the project -> Add -> New Item -> Select "Module Definition File (.def)" -> Enter a name -> Add

 After the addition is complete, compile directly, and then open the folder, you will find that there is a corresponding lib file

    2) Copy the "Dll1.dll" and "Dll1.lib" files in the x64\Debug folder under the generated project directory of Dll1 to the " DllTest\DllTest" folder.

    3) Copy the "Test.h" file in the Dll1\Dll1 folder in the Dll1 generated project directory to the " DllTest\DllTest" folder.

    4) For the newly created C++ project (Adaptive), header file--add--existing item

After adding Test.h. Edit the Test.h file

#pragma once
#pragma comment(lib,"Dll1.lib")
#include <opencv2/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgproc/imgproc_c.h>
#include <iostream>

using namespace cv;

extern "C" __declspec(dllimport) void thresholdIntegral(Mat inputMat, Mat outputMat);

First, add a line of code: #pragma comment(lib,"xxx.lib"), its function is to link your "xxx.dll" to your project; then, extern "C" __declspec(
dllexport ) modify It is extern "C" __declspec( dllimport ), its function is to tell the compiler and linker that the function or variable modified by __declspec(dllimport) needs to be imported from the DLL.

    5) Write the cpp file that needs to call the dll

Guess you like

Origin blog.csdn.net/weixin_44888196/article/details/127211083