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

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

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

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

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

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

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

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

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

8.代码如下:

 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

 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

 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

 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

 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

总结:

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

extern EXT_MYLIB int sum(int a, int b);

2.对于函数("function")而言,关键字extern可以去掉("variable"去掉extern会报错);

3.EXT_MYLIB放置于函数返回类型前可能会报warning[browsing operations around this macro may fail,consider adding it to hint file],但这看似并不影响dll的编译以及被调用。

4.类的成员函数(包括静态函数)定义时候可以不加EXT_MYLIB,这样类的实例化对象(类名可以直接访问类的公有静态函数)依旧可以正常引用成员函数(猜测可能是定义类时候已经加入EXT_MYLIB);

How to Create DLL(Dynamic link library)的更多相关文章

  1. [DLL] Dynamic link library (dll) 的编写和使用教程

    前一阵子,项目里需要导出一个DLL,但是导出之后输出一直不怎么对,改了半天才算改对...读了一些DLL教程,感觉之后要把现在的代码导出,应该还要花不少功夫...下面教程参照我读的3个教程写成,所以内容 ...

  2. Walkthrough: Creating and Using a Dynamic Link Library (C++)

    Original Link: http://msdn.microsoft.com/zh-cn/library/ms235636.aspx Following content is only used ...

  3. DYNAMIC LINK LIBRARY - DLL

    https://www.tenouk.com/ModuleBB.html MODULE BB DYNAMIC LINK LIBRARY - DLL Part 1: STORY What do we h ...

  4. Walkthrough: Create and use your own Dynamic Link Library (C++)

    参考网站:https://docs.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-librar ...

  5. How to Use the Dynamic Link Library in C++ Linux (C++调用Delphi写的.so文件)

    The Dynamic Link Library (DLL) is stored separately from the target application and shared among dif ...

  6. Custom Action : dynamic link library

    工具:VS2010, Installshield 2008 实现功能: 创建一个C++ win32 DLL的工程,MSI 工程需要调用这个DLL,并将Basic MSI工程中的两个参数,传递给DLL, ...

  7. 动态链接库(Dynamic Link Library)

    DLL INTRODUCTION A DLL is a library that contains code and data that can be used by more than one pr ...

  8. 动态链接库(Dynamic Link Library)学习笔记(附PE文件分析)

    转载:http://www.cnblogs.com/yxin1322/archive/2008/03/08/donamiclinklibrary.html 作者:EricYou 转载请注明出处   注 ...

  9. Linux Dynamic Shared Library && LD Linker

    目录 . 动态链接的意义 . 地址无关代码: PIC . 延迟版定(PLT Procedure Linkage Table) . 动态链接相关结构 . 动态链接的步骤和实现 . Linux动态链接器实 ...

随机推荐

  1. 浅谈Meet in the middle——MITM

    目测观看人数 \(0+0+0=0\) \(\mathrm{Meet\;in\;the\;middle}\)(简称 \(\rm MITM\)),顾名思义就是在中间相遇. 可以理解为就是起点跑搜索树基本一 ...

  2. 利用Docker挂载Nginx-rtmp(服务器直播流分发)+FFmpeg(推流)+Vue.js结合Video.js(播放器流播放)来实现实时网络直播

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_75 众所周知,在视频直播领域,有不同的商家提供各种的商业解决方案,其中比较靠谱的服务商有阿里云直播,腾讯云直播,以及又拍云和网易云 ...

  3. 日志(logging模块)

    1. 为什么要使用日志(作用) 在学习过程中,写一个小程序或小demo时,遇到程序出错时,我们一般会将一些信息打印到控制台进行查看,方便排查错误.这种方法在较小的程序中很实用,但是当你的程序变大,或是 ...

  4. MySQL表操作过程的基础代码解析

    GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. MySQL 的表有很多种,对表的操作主要是增删改查,今天来浅谈一下这些操作的底层代码和流程,以下以 tmp table为例 ...

  5. 我分析30w条数据后发现,西安新房公摊最低的竟是这里?

    前两天一个邻居发出了灵魂质问:"为什么我买的180平和你的169平看上去一样大?" "因为咱俩的套内面积都是138平......" 我们去看房子,比较不同楼盘的 ...

  6. luogu1486 [NOI2004]郁闷的出纳员 (平衡树)

    加的注释,都流着泪... 胡了一种做法,样例都没过,翻题解发现一神仙Remove操作,妙啊! #include <iostream> #include <cstdio> #in ...

  7. HDU 1542/POJ 1151 Atlantis (scaning line + segment tree)

    A template of discretization + scaning line + segment tree. It's easy to understand, but a little di ...

  8. Luogu1527 [国家集训队]矩阵乘法 (整体二分)(Unfinished)

    全线RE的代码... 先搁这吧,下次再说.flag //#include <iostream> #include <cstdio> //#include <cstring ...

  9. 红黑树以及JAVA实现(一)

    目录 前言 一. B树 1.1 概念 1.2 2-3-4树 1.3 2-3-4树的插入 节点分类 1.4 2-3-4树的删除 1.4.1 当删除节点是叶子节点 1.4.1.1 当删除节点为非2节点 1 ...

  10. MySQL入门笔记一

    MySQL应用笔记   一MySQL关系型数据库.开源,中小型公司常用类型的数据库Oracle 大型公司常用数据库 MySQL基本的命令一. 创建.删除.查看数据库(database)创建库creat ...