VS2019 try dll works with the console FBI

How to make yourself write dynamic link library project (Dynamic-Link Library Project) and test engineering (such as Console) debugging in the same solution?

Take the following VS2019 DLL project with joint debugging console, set about:

Create a simple DLL dynamic link library project, where we ta named "Dll1".

 

 

 In Dll1 in, add a header file, add dynamic library function export function to be head inside, where we added two very simple Add and Sub functions, but in a new version of VS compiler environment, we must remember to include pch this pre-compiled .h file.

 

1 //Dll1.h
2 #pragma once
3 #include "pch.h"
4 
5 int Add(int a, int b);
6 int Sub(int a, int b);

 

 Then in the "Source Files" folder and add the source file of the same name of the head, which has realized the function header file declarations.

 1 //Dll1.cpp
 2 #include "pch.h"
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 int Add(int a, int b) {
 8     cout << "Starting the function Add: " << endl;
 9     return a + b;
10 }
11 
12 int Sub(int a, int b) {
13     cout << "Starting the function Sub:" << endl;
14     return a - b;
15 }

 

下一步为了将我们刚刚写好的函数以库文件的形式导出,我们在“源文件”文件夹下添加一个.def的文件使用EXPORTS命令用于导出刚刚写好的两个函数。记得在第一行的LIBRARY后面加上以咱们这个dll工程为名编译生成的dll动态库文件(这里就是Dll1.dll)注意!:library的名字一定要写对。这样一个.lib的静态库就在该工程的文件夹下生成了。

1 //Dll1.def
2 LIBRARY "Dll1.dll"
3 
4 EXPORTS
5 
6 Add
7 Sub

 

不过这时大家可能会发现单单这样的Dll的工程在编译的时候可能会报错,显示“xxx.dll is not a valid Win32 application.”这是因为在编译的时候,生成的xxx.dll文件并不是一个可执行的程序,vs无法将生成的dll调起来。但细心的同学可能会发现,虽然此时编译报错了,但点开dll工程的debug文件夹会发现此时的xxx.dll 动态库文件已经如愿的生成了。 

于是接下来,咱们再创建一个简单的控制台程序,在这里我们把ta命名为Dll1_test,将刚刚生成的dll动态库加载进去,并实现联合调试:

 

 咱们在同一个解决方案(solution)下,右键解决方案的名称,点击添加,选择一个新工程,或一个现有已写好的测试工程。这里我们选择一个新的控制台工程,然后设好名字以后点击创建,这里我们将ta命名为Dll1_test,这样就在同一个解决方案下又添加了一个工程。

 

 

 为了能在新的测试工程中使用先前在dll工程中写好的函数,需要在测试工程中包含dll工程的头文件(这样工程在编译的时候才不会报错),这里有两种方法将dll工程中包含函数声明的头文件包含进来的方法:

1. 将dll工程中的头文件直接复制到新的测试工程中;

2. 使用相对路径的方法找到dll工程的头文件。

这里我们选择相对路径的方法: ..\ 在相对路径中表示前一级目录。

本次实例中两个工程的文件夹是同级的文件夹。 加入了头文件以后,再使用#pragma comment命令将dll工程生成的动态链接库包含进来(那里面有具体的函数实现,虽然看不到)。由于#pragma命令的使用方法很复杂,这里我们就不再细说了。下面就是在测试工程的main函数下写具体的函数调用了。这里我们只调用了一个Add函数作为示例。

 1 // Dll1_test.cpp : This file contains the 'main' function. Program execution begins and ends there.
 2 //
 3 
 4 #include <iostream>
 5 #include "..\Dll1\Dll1.h"//包含dll工程的头文件 来获取函数声明
 6 #pragma comment(lib, "Dll1")//将生成的动态库调进来
 7 using namespace std;
 8 
 9 int main()
10 {
11     int a, b;
12     cin >> a >> b;
13     int c;
14     c = Add(a, b);
15     cout << "The result of the function is :" << c << endl;
16     return 0;
17 }

 

心急的同学们如果在此时就点击的编译,可能会发现编译出错了,而且和之前的错误一模一样,这是因为,这是整个解决方案的启动工程仍是咱们最开始创建的dll工程,需要将test测试程序设置为此时解决方案的启动工程。这里我们先右键解决方案,在Project Dependency(项目依赖/独立)中选择test测试工程depends on咱们的dll工程。确定后再右键test测试工程选择“设置为启动项目”("Set as StartUp Project")

 

 

 

而就算这样在编译的时候还会发现竟然还会报错!"Cannot open file xxx.lib" 原因是找不到函数具体实现的静态库的位置,还需要我们手动的告诉编译器此次测试的库到底在哪。于是我们右键测试工程,在最下面的属性里面,找到Linker->general,在additional library directories里面加入我们dll工程生成静态库.lib文件的地址,同样这里使用相对路径的方法。

 

 设置好以后确定,这时再点击调试,就可以实现dll工程与控制台工程在同一个解决方案中联合调试啦!

Guess you like

Origin www.cnblogs.com/genius-tintin/p/12083089.html
try