How to Creat DLL(Dynamic link library)

该文章属于在YouTube视频上看到的,链接如下:

https://www.youtube.com/watch?v=EmDJsl7C9-k&t=3s

1.创建一个工程并建立一个控制台程序

How to Creat DLL(Dynamic link library)

 

2.Solution-->右键新建dll工程

How to Creat DLL(Dynamic link library)

 

3.Solution-->右键属性,选择依赖项,确定

How to Creat DLL(Dynamic link library)

 

4.CppClient-->右键设置属性$(SolutionDir)myLib\,inherit打勾,确定

How to Creat DLL(Dynamic link library)

 

5.VC++Directories-->Library Directories-->$(SolutionDir)$(IntDir)

 How to Creat DLL(Dynamic link library)

 

 

6.myLib-->右键设置属性-->Command Line-->/DDLL_BUILD

How to Creat DLL(Dynamic link library)

 

 

 

7.myLib添加一个类,再添加一个头文件myLib.h

How to Creat DLL(Dynamic link library)

 

 

 

8.代码如下:

How to Creat DLL(Dynamic link library)
 1 #pragma once
 2 
 3 #ifndef EXT_MYLIB
 4 
 5     #ifdef DLL_BUILD
 6         #define EXT_MYLIB __declspec(dllexport)
 7     #else
 8         #pragma comment(lib, "myLib.lib")
 9         #define EXT_MYLIB __declspec(dllimport)
10     #endif
11 
12 #endif
13 
14 
15 extern int EXT_MYLIB max_size;
16 extern int EXT_MYLIB sum(int a, int b);
myLib.h How to Creat DLL(Dynamic link library)
 1 // myLib.cpp : Defines the exported functions for the DLL application.
 2 //
 3 
 4 #include "stdafx.h"
 5 #include "myLib.h"
 6 
 7 int EXT_MYLIB max_size = 100;
 8 
 9 int EXT_MYLIB sum(int a, int b)
10 {
11     int s = 0;
12     for (int i = 0; i <= b; ++i)
13         s += i;;
14     return s;
15 }
myLib.cpp How to Creat DLL(Dynamic link library)
 1 #pragma once
 2 
 3 #include "myLib.h"
 4 
 5 #include <iostream>
 6 
 7 #include <sstream>
 8 
 9 
10 class EXT_MYLIB Vctr
11 {
12 
13 private:
14 
15     double m_x, m_y, m_z;
16 
17 public:
18     Vctr();
19 
20     Vctr(double i, double j, double k);
21 
22     ~Vctr();
23 
24     int Total(int a, int b);
25 
26 
27     std::string to_string() const
28     {
29         std::ostringstream os;
30         os << "(" << m_x << "," << m_y << "," << m_z << ")";
31 
32         return os.str();
33     }
34     friend EXT_MYLIB std::ostream &operator<<(std::ostream &os, const Vctr &v);
35     friend void EXT_MYLIB TestFriend(const Vctr &v);//the usage of friend fucntion 
36 };
Vctr.h How to Creat DLL(Dynamic link library)
 1 #include "stdafx.h"
 2 #include "Vctr.h"
 3 
 4 
 5 EXT_MYLIB Vctr::Vctr()
 6 {
 7 }
 8 
 9 
10 EXT_MYLIB Vctr::~Vctr()
11 {
12 }
13 
14 
15 
16 EXT_MYLIB Vctr::Vctr(double x, double y, double z): m_x(x) , m_y(y), m_z(z)
17 {
18 
19 }
20 
21 
22 EXT_MYLIB std::ostream& operator<<(std::ostream& os, const Vctr& v)
23 {
24     os << v.to_string();
25 
26     return os;
27 }
28 
29 EXT_MYLIB void TestFriend(const Vctr& v)
30 {
31     std::cout << "hello, this is a friend function!" << std::endl;
32     std::cout << "m_x :" << v.m_x << std::endl;
33 }
34 
35 EXT_MYLIB int Vctr::Total(int a, int b)
36 {
37     return (a+b);
38 }
Vctr.cpp How to Creat DLL(Dynamic link library)
 1 #include "pch.h"
 2 
 3 #include <iostream>
 4 
 5 #include "myLib.h"
 6 
 7 #include "Vctr.h"
 8 
 9 int main()
10 {
11     Vctr v(2, 3, 4);
12     std::cout << v << std::endl;
13     std::cout << "sum is: " << sum(2, 3) << std::endl;
14     TestFriend(v);
15     std::cout << "max_size is: " << max_size << std::endl;
16 }
CppClient.cpp

 

总结:

该视频主要讲解了变量/函数/类如何打包成dll,且其语法并没有严格限制,例如在myLib.h,对函数的声明我们可以这样:extern int EXT_MYLIB sum(int a, int b)或者

extern EXT_MYLIB int sum(int a, int b); 设置去掉extern也可以("variable"去掉extern会报错),EXT_MYLIB放置于函数返回类型前可能会报warning[browsing operations around this macro may fail,consider adding it to hint file],但这看似并不影响dll的编译以及被调用。

上一篇:nacos随记


下一篇:388 文件的最长绝对路径