Sometimes we want to write our own library of functions to avoid repeating code, and this article describes how to write your own dynamic-link library using Visual Studio.
0, Experimental environment description:
Integrated development environment: Visual Studio 10.0
Operating system: Windows 7
Language: C + +
1. Write your own DLL
In two parts we describe how to complete writing your own DLL.
1.1, Environment construction
Describes how to use Visual Studio to build a DLL project that has additional configuration.
First, create an empty project for a DLL, as shown in:
Add two files, a header file (mydll.h), a source file (mydll.cpp), name the filename according to your needs, there is no need to write a dead file name.
Pay particular attention to whether there is something similar to what is shown in the project properties, and if there are no red box-labeled macros, add the appropriate macro, such as Mydll_export.
1.2, Code description
Add the following to the. h file:
#ifndef __my_dll_h__#define __my_dll_h__#if (defined WIN32 | | defined _WIN32 | | defined WINCE) && defined MYDLL_EX ports# define Ab_exports __declspec (dllexport) #else # define Ab_exports#endifab_exports void func (); #endif
In the above code, Func is the function we define ourselves.
Add the following code to the. cpp file:
#include "mydll.h" #include <iostream>void func () { std::cout << "Hello func \ n";}
After the build command is executed, the following appears next to the build directory:
2. Test your own DLL
Add a Win32 Consol application empty project, add the CPP code file to the new project, and include the following code:
#include "mydll.h" #include <iostream>using namespace Std;int main () { func (); return 0;}
Then configure as shown.
Build Ctrl + F5 Execution! Execute the results as shown:
Summarize
This article mainly discusses how to use Visual Studio to create your own DLL, with examples to illustrate the process, the results show that the method in the paper is feasible.
If there is insufficient support, you are welcome to criticize correct.
Visual Studio writes its own dynamic-link library (DLL)