Create DLL in VC and export global variables, functions and Classes

Source: Internet
Author: User
ArticleDirectory
    • 1. Create DLL
    • 2. Test the created DLL
1. Create dll1. create a new Win32 Empty Project mathlib in VC; 2. Add the pre-compiled header file stdafx. h and define the Import and Export Control symbols:
 
1:// Stdafx. h
 
2: # pragma once
 
3: # define mathlib_export
3. Add the header file mathlib. H that contains the global variables, functions, and classes to be exported:
1:// Mathlib. h
 
2: # pragma once
 
3:
 
4: # ifdef mathlib_export
 
5: # define mathlibapi _ declspec (dllexport)
6 :#Else
 
7: # define mathlibapi _ declspec (dllimport)
 
8: # endif
 
9:
 
10:// Macro
11: # define PI 3.14149
 
12:
 
13:// Global variable
 
14:ExternMathlibapiIntGlobalvariable;
 
15:
16:// Function
 
17: mathlibapiIntAdd (IntA,IntB );
 
18:
 
19:// Class
20:ClassMathlibapi math
 
21 :{
 
22:Public:
 
23:IntMultiply (IntA,IntB );
24 :};
4. Add the exported element implementation file mathlib. cpp.
 
1:// Mathlib. cpp
 
2: # include"Stdafx. h"
 
3: # include"Mathlib. h"
4:
 
5:IntGlobalvariable = 100;
 
6:
 
7:IntAdd (IntA,IntB)
 
8 :{
9:ReturnA + B;
 
10 :}
 
11:
 
12:IntMath: multiply (IntA,IntB)
 
13 :{
14:ReturnA * B;
 
15 :}
2. Test the created DLL

TestCode:

 
1: # include"Stdafx. h"
 
2: # include <iostream>
3:Using NamespaceSTD;
 
4:
 
5: # include"../Mathlib. h"
 
6: # pragma comment (Lib ,"../Debug/mathlib. Lib")
7:
 
8:Int_ Tmain (IntArgc, _ tchar * argv [])
 
9 :{
 
10: cout <"Pi ="<PI <Endl;
 
11:
12: cout <"Globalvariable ="<Globalvariable <Endl;
 
13:
 
14:IntA = 20, B = 30;
 
15: cout <"A ="<A <","<"B ="<B <Endl;
16: cout <"A + B ="<Add (a, B) <Endl;
 
17:
 
18: Math math;
 
19: cout <"A * B ="<Math. Multiply (a, B) <Endl;
 
20:
21:Return0;
 
22 :}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.