vs2019 + opencv generates its own dll dynamic library for others to use

vs2019 + opencv generates its own dll dynamic library for others to use

    • 1. Generate dll and lib files
    • 2. The new project calls the generated dll and lib

Recently, I needed to compile the engineering code I wrote into a dynamic library for others to use, so I read a lot of blogs, but found that it was not very detailed, so after I succeeded, I wanted to save the method for myself to use later.

Using dynamic libraries requires the use of both dll and lib files. The compiler used is Vs2019, and opencv is also used. If you don’t have opencv installed, you can also refer to the reference and delete the code involving opencv directly. .

1. Generate dll and lib files

1. Open vs2019 and create a dll file project.

2. Create a math project in the Dll folder. **Note: **I have not ticked the solution section, and the following configurations are based on this basis.

The created folder is as shown below:

As shown below, after creation, vs2019 has these 4 files by default. One method is to directly paste the code in those .h and .cpp. When using the generated dll, you must include the header file #include “pch.h” , if there are multiple dll projects, each project will have “pch.h”, which will result in not knowing which header file it is. Therefore, this method is not considered here, just ignore them and go directly to point 5 below.

5.Key points: Under debug x64 (also available for release), create a class directly in the source file and header file. This class name is the name of the header file to be used later. I named them “MathAlgorithm.h” and “MathAlgorithm.cpp” here, and just paste the code you wrote. The code is at the back, just take it if you need it.

__declspec(dllexport) means that functions or classes can be exported to be called by other projects. The macro SimpleCal_API is defined here to replace it.

.h code

// MathAlgorithm.h

#pragma once
#include<iostream>
#include<opencv2/opencv.hpp>


#define SimpleCal_API ;
#ifdef SimpleCal_API
#define SimpleCal_API __declspec(dllexport)
#else
#define SimpleCal_API __declspec(dllimport)
#endif

// The global function uses the opencv third-party library. If it is not installed, you can remove this function.
SimpleCal_API bool testBuildOpencvDLL(cv::Mat & amp; imgInput, cv::Mat & amp; imgOutput);

class SimpleCal_API Mycal
{<!-- -->
public:
int my_add(int & amp; a, int & amp; b);

int my_minus(int a, int b);

int my_multiply(int a, int b);

double my_divide(int a, int b);
};


.cpp file

// MathAlgorithm.cpp

#include "pch.h"
#include "MathAlgorithm.h"

bool testBuildOpencvDLL(cv::Mat & amp; imgInput, cv::Mat & amp; imgOutput)
{<!-- -->
cv::cvtColor(imgInput, imgOutput, cv::COLOR_BGR2GRAY);
return true;
}


int Mycal::my_add(int & amp; a, int & amp; b) {<!-- -->
return a + b;
}

int Mycal::my_minus(int a, int b)
{<!-- -->
return a - b;
}

int Mycal::my_multiply(int a, int b)
{<!-- -->
return a * b;
}

double Mycal::my_divide(int a, int b)
{<!-- -->
double m = (double)a / b;
return m;
}


6. Generate the solution and you can see that the generation is successful.

You can see the successfully generated dll and lib files in x64\Debug of the project folder. If opencv is used, vs will also copy the relevant dlls that need to be used.

2. The new project calls the generated dll and lib

1. Create an empty project, which I named math_use.

2. Generate the solution. There is an x64\Debug folder under the math_use folder.

Remember this folder:

3. Copy the math.dll file generated in the previous step 6 to the folder just mentioned.

4.Key points: As shown below, click Project Properties 》C/C++ 》General 》Additional Include Directory, click Edit to add “E:\Dll\math\math” and confirm and apply. This directory is the path to the .h and .cpp files in the math project.
Note: If the directory is not added, the MathAlgorithm.h header file written by you cannot be found!

5. Linker >> Input >> Additional dependencies, click Edit, manually add “math.lib” and confirm.

6. Linker >> General >> Additional Library Directory >> Edit, add “E:\Dll\math\x64\Debug”, confirm and apply. This directory is the path where the dll project contains lib and dll files. The purpose of this step is to link to math.lib. Therefore, the math.dll file in the “E:\Dll\math\x64\Debug” directory can be deleted, because math.dll has been copied to the current project previously.

7. Add code and find that you can find your own header file under the math project #include “MathAlgorithm.h”

#include<iostream>
#include"MathAlgorithm.h"
#include<opencv2/opencv.hpp> //There is no opencv to delete

int main()
{<!-- -->
// + - * /
Mycal cal;
int a = 8, b = 2;
int res1 = cal.my_add(a, b);
int res2 = cal.my_minus(a, b);
int res3 = cal.my_divide(a, b);
int res4 = cal.my_multiply(a, b);
std::cout << res1 << " " << res2 << " " << res3 << " " << res4 << std::endl;

// Without opencv, you can delete the following 9 lines
cv::Mat src = cv::imread("00.bmp"), dst;
testBuildOpencvDLL(src, dst);
cv::namedWindow("src", cv::WINDOW_NORMAL);
cv::namedWindow("dst", cv::WINDOW_NORMAL);
cv::resizeWindow("src", 800, 600);
cv::resizeWindow("dst", 800, 600);
cv::imshow("src", src);
cv::imshow("dst", dst);
cv::waitKey(0);

return 0;
}

8. Run the code, the console outputs the calculation results of addition, subtraction, multiplication and division, and converts the color image into grayscale image.

To summarize: the purpose of generating a dynamic library is mainly to make the engineering code you write easy to use for other departments or companies. However, the code you write uses third-party libraries such as opencv, eigen3, arma, etc., so the premise is that other people’s computers are also configured. Only the same library can use the dynamic library we generated. Then can others use the code they wrote without installing opencv, eigen3, arma and other third-party libraries? The answer is yes. Due to time constraints, I will write another one if I have time.
I refer to Microsoft’s official website:
Create and use your own dynamic link library (C++)

This is the reference blog:
Two ways to compile and generate dynamic link library dll in VS2019
Code written in vs2017 + opencv generates dll for use in another project