VS2010创建动态链接库并且使用动态链接库DLL

1.编写动态链接库文件 dll和lib文件

例子:

在新建VS工程时选择DLL 空项目

----------hello.h--------

#include <stdio.h>
#pragma once;

#ifdef DLL_IMPLEMENT

#define DLL_API __declspec(dllexport)

#else

#define DLL_API __declspec(dllimport)

#endif

namespace dayinga
{

//导出类

class DLL_API SimpleDll

{
public:

SimpleDll();

~SimpleDll();

void daying(); //简单方法

};
}

---------hello.cpp--------------

#define DLL_IMPLEMENT
#include "hello.h"
namespace dayinga
{

SimpleDll::SimpleDll()
{
}

SimpleDll::~SimpleDll()
{
}
void SimpleDll::daying()
{
printf("hello,world");
}

}

生成以上工程时 会得到 hello.dll和hello.lib两个文件。

2.调用

新建一个win32工程

我们需要三个文件 需要hello.h这个头文件和hello.dll和hello.lib文件。

在工程属性里包含 hello.dll和hello.lib文件。

在链接器输入里设置lib

------------------usedll.cpp------------------------

#include "stdafx.h"
#include "hello.h"
using namespace dayinga;//使用命名空间

int _tmain(int argc, _TCHAR* argv[])
{
SimpleDll sd;//对象
sd.daying();
return 0;
}

以上。

原文地址:https://www.cnblogs.com/xuandi/p/5362476.html