VC++ dll 创建,使用头文件和lib库加载

1.创建

dlltest.h

// 下列 ifdef 块是创建使从 DLL 导出更简单的
// 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 DLLTEST_EXPORTS
// 符号编译的。在使用此 DLL 的
// 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将
// DLLTEST_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的
// 符号视为是被导出的。
#ifdef DLLTEST_EXPORTS
#define DLLTEST_API __declspec(dllexport)
#else
#define DLLTEST_API __declspec(dllimport)
#endif

DLLTEST_API int __stdcall Add(int a, int b);
DLLTEST_API int __stdcall Sub(int a, int b);

dlltest.cpp

// dllTest.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"
#include "dllTest.h"

DLLTEST_API int __stdcall Add(int a, int b)
{
	return a + b;
}
DLLTEST_API int __stdcall Sub(int a, int b)
{
	return a - b;
}

2.加载

// ConsoleApplication12.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "../../dllTest/dllTest/dllTest.h"
#pragma comment(lib,"../../dllTest/debug/dllTest.lib")
int main()
{
	int sum = Add(3, 4);
	int sub = Sub(5, 2);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_24127015/article/details/87784785