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

本文详细介绍了如何在DLL中创建导出函数,并在主程序中加载和使用这些函数。通过具体的代码示例,展示了宏定义在DLL导出与导入过程中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值