首先创建个DLL工程(win32控制台——DLL工程)
添加头文件和CPP文件,如下:
#ifndef __DLL_TEST_H__
#define __DLL_TEST_H__
class __declspec(dllexport)
CTest
{
public:
CTest();
~CTest();
void print();
protected:
private:
};
#endif#include "DLL_Test.h"
#include <stdio.h>
CTest::CTest()
{
}
CTest::~CTest()
{
}
void CTest::print()
{
printf("DLL Test\n");
}再新建一个测试工程,在工程中设置好附加依赖生成的lib文件,添加测试代码:
#include<stdio.h>
#include "DLL_Test.h"
int main()
{
CTest test;
test.print();
getchar();
}完成。
做完后,分析一下,其实关键就在于这两句:
class __declspec(dllexport) CTest{};
MSDN中的说明是:They enable you to export and import functions, data, and objects to and from a DLL.
另:Declaring functions as dllexport eliminates the need for a module-definition (.DEF) file,
也就是说使用dllexport后,就不用在dll工程中建立.def文件作声明了,因为它代替了__export 关键字
1079

被折叠的 条评论
为什么被折叠?



