Machine Vision Series 1: Visual Studio 2019 Dynamic Link Library DLL Establishment

Series Article Directory

Chapter 1: Visual Studio 2019 Dynamic Link Library DLL Establishment


Table of contents

Series Article Directory

foreword

1. What is a DLL?

2. Establishment steps

1. Install VS

2. Create a DLL library

 3. Create a new DLL

 Fourth, compile the DLL

Summarize


foreword

Recently started a medical machine vision project, which needs to use C++ to record some key points.

Environment: Windows10, VS2019, opencv320


1. What is a DLL?

Dynamic link library DLLs are independent files that contain functions that can be called by executable programs or other dlls to complete a certain job. The dll will only work when other modules call the functions in the dll. In actual programming, we can put the function that completes a certain function in a dynamic link library, and then provide it for other programs to call. All functions like Windows API are included in dll, such as Kernel32.dll, User32.dll, GDI32.dll, etc.

2. Establishment steps

1. Install VS

Official website, the latest version is 2022

Visual Studio 2022 IDE - Programming Tools for Software Developers (microsoft.com)

2. Create a DLL library

The code is as follows (example):

1. Create a new project

2. Select Dynamic Link Library (DLL)

 3. Set the project name, do not check if there is no special requirement (put the solution and the project in the same directory)

  4. Generate a default file without modification.

   5. Use the property manager to configure properties. For details, see Chapter 2 of the series.

   6. Modify the precompiled header, property page - C/C++ - precompiled header - does not apply to the precompiled header.

        Note: If the program is not modified, an error will be reported, and the details will be studied later

 3. Create a new DLL

1. In the header file under the project, right-click the new header file (.h) to declare the function interface that needs to be exported.

 2. Right click on the source file under the project to create a new source file (.cpp) to implement the declared function.

The project directory is as follows:

 

 The code can be written below.

.h file

The function of the following code is to declare a callable function "SayHello()", whose return type is void.

#pragma once
#include <iostream>

extern "C" __declspec(dllexport) void SayHello();

Now analyze the code of extern "C" __declspec(dllexport) void SayHello();, where the function of extern "C" is to tell the compiler to compile the code modified by it as C language. The meaning of this is This is not for discussion, if you are interested, you can inquire by yourself. Then analyze __declspec(dllexport), this modifier tells 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 to tell the compiler and linker that the function or variable modified by it needs to be imported from the DLL, and it will also be used later. Finally, there is the function void SayHello(), which is the function that needs to be called by other programs.

.cpp file

Implement the functions declared in the .h file

#include "pch.h"
#include "TestDLL.h"

void SayHello()
{
	std::cout << "Hello!你成功了!" << std::endl;
}

The code is written.

 Fourth, compile the DLL

If there is no problem with the attribute environment, click compile

 Click compile to pop up the following pop-up window, click confirm (DLL will not generate exe, you can ignore the error)

Generate files under the project path - x64 - debug folder.

Compilation is complete.


Summarize

1. The above tutorial is only applicable to the debug mode. If you want to directly start the TestDLLCreated.exe application, you need to copy the "TestDLL.dll" and "TestDLL.lib" files to the same directory as TestDLLCreated.exe to run!

2. You can use the property manager to configure properties, and other items can use properties directly to avoid repeated settings.

Reference: https://blog.csdn.net/elaine_bao/article/details/51784864

Guess you like

Origin blog.csdn.net/weixin_42748604/article/details/122351790