本文介绍下将class类封装成dll/lib库的方法
关键点:添加declspec(dllexport)
一、封装成动态库 test.dll
步骤1: 写代码
----- test.h
#pragma once
class declspec(dllexport) CTest
{
public:
CTest();
~Ctest();
void Test();
}
---- test.cpp
void Test()
{
printf("hello world");
}
步骤2:编译生成dll
编译器生成或命令行生成
步骤3:调用 需要test.h+test.lib+test.dll
---main.cpp
#include "test.h"
#pragma comment(lib, "test.lib")
void main()
{
CTest test;
test.Test();
}
步骤4:编译测试 main.cpp
二、封装成静态库 test.lib
关键点:不要declspec(dllexport)
其步骤同方法一
其中 步骤1 需要需改: class CTest
步骤3需要需改:调用 需要test.h+test.lib
参考:http://blog.youkuaiyun.com/bodybo/article/details/39054239#comments