Use VS2017 to create a dynamic library dll and static library lib and links to other projects

Reprint:

Static library: https://jingyan.baidu.com/album/f0e83a256230ea22e4910163.html?picindex=14

DLL: https://blog.csdn.net/cynophile/article/details/79749524

This is a dynamic library content

  1. Start VS2017, click on the menu bar "File -> New -> Project" to create a new development project; 
    Write pictures described here
  2. In the pop-up "New Project window", select "Windows Desktop" in the left side of the "Visual C ++" list, then select the type of project the right is "dynamic-link library (DLL)", and then set the project name and storage location solution name. Once configured, click on the "OK" OK to create dynamic link library project; 
    Write pictures described here
    After this step, the folder generation DLL1 folder contains the following 
    Write pictures described here
  3. After the project is created, click on the VS2017 interface menu bar "Build> Build Solution" project code compilation newly created, to confirm if there are problems (very few problems occur); 
    Write pictures described here
    After the Solution, produces the following in the Debug folder file 
    Write pictures described here
  4. After the compilation, you can see the output information compiled successful in the output window in VS2017; 
    Write pictures described here
  5. In VS2017 development interface, the "header files" directory under the right-click "solution" inside "Dll1" project, in the pop-up menu, select "Add> New Item"; 
    Write pictures described here
  6. In the pop-up "Add New Item" dialog box, select the "header files (.h)", and then enter the name "dll1.h" header files, then click on the "Add button" OK to add a file called "dll1.h" header; 
    Write pictures described here
    corresponding header file multiple dll1.h 
    Write pictures described here
  7. 在Windows中,定义在dll中的变量、函数和类,如果希望让别的程序能够访问。必须通过manifest文件指定导出目标(变量、函数或类)或者通过_declspec(dllimport)关键字指定需要导出的目标,然后在使用dll的程序中通过_declspec(dllimport)关键字指定导入的目标。在开发中使用_declspec()定义导出/导入目标是最方便的做法,因此,可以继续向“dll1项目”中添加一个头文件 “export.h”,然后添加自适应导出/导入目标的宏;(图中第一个应该为_declspec(dllimport)第二个为_declspec(dllexport),存在图中错误) 
    Write pictures described here
    文件中反应出来的是,在如下目录下生成export.h文件 
    Write pictures described here
  8. 鼠标选中DLL1项目右键“ 属性”,打开Dll1项目的属性页窗口; 
    Write pictures described here
  9. 在弹出的“Dll1属性页窗口”中,将配置设置为”所有配置”,然后选中“C/C++ > 预处理器”,接着在“预处理器定义”右侧的属性值中增加“EXPORT_DLL”。设置完毕后,点击“确定按钮”确定属性设置; 
    Write pictures described here
  10. 在属性页中定义了EXPORT_DLL宏之后,export.h文件中EXPORT_API宏对应的值就变成了__declspec(dllexport),对于Dll1项目而言,只要使用EXPORT_API修饰的对象,都将变成导出目标。相对而言,在引用Dll1的另一项目中,默认是没有定义EXPORT_DLL宏的,那么用EXPORT_API修饰的对象,则都是导入目标; 
    Write pictures described here
  11. 打开 “dll1.h”文件,使用#include包含“export.h”头文件,然后使用EXPORT_API声明一个名为printHello()的DLL导出函数; 
    Write pictures described here
  12. 打开“Dll1.cpp”,包含“stdio.h”头文件并写入printHello()函数的实现; 
    Write pictures described here
  13. 生成解决方案(F7),可以在输出窗口中见到所有代码均编译成功; 
    Write pictures described here
    相应的在Debug文件夹下,多处如下几个文件 
    Write pictures described here
  14. 右键单击左侧列表中“解决方案”,然后在弹出菜单中选择“添加 > 新建项目”,向解决方案中添加一个新的控制台项目,用于测试Dll1中导出的printHello()函数是否可以正常访问; 
    Write pictures described here
  15. 在弹出的“添加新项目窗口”中,选择“Windows桌面 > Windows控制台应用程序”,然后输入新项目的名称并点击“确定按钮”创建新项目; 
    Write pictures described here
    相应文件中表现如下 
    Write pictures described here
    包含文件如下 
    Write pictures described here
  16. 右键单击“解决方案”列表中新创建的控制台项目,在弹出菜单中选择“设置为启动项目”,将控制台项目设置为启动项目。这样,每当运行程序时,启动的就是这个新创建的控制台项目(DLL项目是无法独立运行的); 
    Write pictures described here
  17. 点击VS2017界面中的“本地Windows调试器”按钮,编译并运行控制台项目,会发现有一个黑屏幕一闪而过。这是由于控制台程序在执行完main()函数后,直接退出了(DevCPP中则会自动暂停,防止控制台直接退出)。 
    Write pictures described here
    文件中反应如下 
    Write pictures described here
  18. 为了让控制台程序执行完毕后依然保持开启状态,可以打开 ConsoleApplication1.cpp文件,然后包含stdlib.h文件并在main()函数体的return语句之上添加程序暂停语句”system(“pause”);” 
    Write pictures described here
  19. 写好代码之后,再次调试运行程序,可以见到控制出现并暂停。在控制台窗口中,点任意键可以退出控制台程序; 
    Write pictures described here
  20. 回到VS2017开发界面,右键单击“解决方案管理器”列表中的控制台项目,在弹出菜单中选择“生成依赖项 > 项目依赖项”打开“项目依赖项窗口”; 
    Write pictures described here
  21. 在弹出的“项目依赖项窗口”中,勾选依赖于“Dll1”,表示控制台项目ConsoleApplication1依赖Dll1项目。这样可以保证每次编译ConsoleApplication1时,编译器总会自动先编译Dll1项目,保证Dll1总是最新的。设置完毕后,点击“确定按钮”关闭“项目依赖项窗口”; 
    Write pictures described here
  22. 右键单击VS2017工作区中的“Dll1.cpp选项卡页”,在弹出菜单中选择“打开所在的文件夹”; 
    Write pictures described here
  23. 在打开的Dll1.cpp文件所在的文件夹中,点击返回按钮,重新进入到Dll1项目的Debug输出目录中。在该目录中可以见到Dll1项目生成的符号链接库“Dll1.lib”和动态链接库“Dll1.dll”。 如果需要在另一个项目中加载“Dll1.dll”文件,那么通过链接“Dll1.lib”是最简便的方式(否则就要通过LoadLibrary()及相关函数通过代码加载动态库了); 
    Write pictures described here
  24. 返回VS2017开发界面中,右键单击“解决方案列表”中的ConsoleApplication1,在弹出菜单中,选择“属性页”,打开控制台项目的属性页; 
    Write pictures described here
  25. 在弹出的ConsoleApplication1属性页窗口中,将配置设置为“所有配置”,然后在左侧“配置属性”列表中,选择“链接器 > 常规”,接着在右侧属性列表中选择“附加库目录”属性右方的编辑框,在弹出的下拉列表中选择“编辑”; 
    Write pictures described here
  26. 在弹出的“附加库目录窗口“中,点击”宏(M) >>“按钮,展开VS2017的宏列表; 
    Write pictures described here
  27. 在展开的VS2017宏列表中,搜索“(Out”即可看到列表中显示宏“(Out”即可看到列表中显示宏“(OutDir)”表示解决方案Dll1的输出目录。由于ConsoleApplication1和Dll1项目均位于解决方案Dll1下,因此在默认配置下,这两个项目的输出文件均位于该输出目录下。只要将$(OutDir)充当静态库的查找目录,就可以方便的找到“Dll1.lib”。记住这个宏名称后,点击“宏(M) <<”按钮隐藏宏列表页; 
    Write pictures described here
  28. Return "Additional Library Directories window", click on "New Folder icon" and then fill in the "$ (OutDir)" and click "OK" at the end of the additional libraries set up additional new directory entry that appears; 
    Write pictures described here
  29. After additional library is set up, you can see the "Additional Library Directories" in the project properties page property has been populated with the right set of values; 
    Write pictures described here
  30. Select the list on the left of the "input" "ConsoleApplication1 property page", and then fill in the right side of the "Additional Dependencies" in "dll1.lib;", tells the compiler needs to link dll1.lib, and then load "Dll1 we need .dll ". After setting, click "OK" to close the property page; 
    Write pictures described here
  31. In VS2017 workspace, open the "ConsoleApplication1.cpp" file, then include "dll1.h" in the code (note the relative path here, the starting point for the directory where the directory ConsoleApplication1.cpp), then add the function main () calling the printHello () function code; 
    Write pictures described here
  32. Build the solution (or F7), when everything is normal, you can see the output information compiled successful in VS2017 output window. If an error occurs, the regenerated according to the prompts to modify the project configuration or code solutions until it succeeds; 
    Write pictures described here
    the file update 
    Write pictures described here
  33. ConsoleApplication1 run the program, the pop-up console interface can be seen in the output of "Hello" string that represents the Dll developed. Enjoy! 
    Write pictures described here
    Write pictures described here

Guess you like

Origin blog.csdn.net/qq_38409301/article/details/90579753
Recommended