今天工作闲下来, 就试看DLL编程, 好久没玩DLL了,结果花了一个半小时才写出这个很简单的例子.
起初直接把子__declspec(dllexport) 放在类声明的地方. 发生 warning C4273 DLL 链接不一致错误.
想了挺长时间才意识到是生成lib时并头文件和原文件中对函数的名字解析名字不一样而产生的. 才想到要定义一个宏.
有点感叹知识不用会也会老的. 下边是代码
//头文件:testClass.h
- #ifndef TEST_CLASS_H__
- #define TEST_CLASS_H__
- #include <iostream>
- using namespace std;
- #ifdef SERVERDLL_EXPORTS //在创建产生DLL的工程中先把 SERVERDLL_EXPORTS 预定义上
- #define SERVERDLL_API __declspec(dllexport)
- #else
- #define SERVERDLL_API __declspec(dllimport)
- #endif
- class SERVERDLL_API TestClass
- {
- public:
- virtual void VirtualFunction(void);
- void NormalFunction(void);
- };
- SERVERDLL_API void func(void);
- #endif
#ifndef TEST_CLASS_H__ #define TEST_CLASS_H__ #include <iostream> using namespace std; #ifdef SERVERDLL_EXPORTS //在创建产生DLL的工程中先把 SERVERDLL_EXPORTS 预定义上 #define SERVERDLL_API __declspec(dllexport) #else #define SERVERDLL_API __declspec(dllimport) #endif class SERVERDLL_API TestClass { public: virtual void VirtualFunction(void); void NormalFunction(void); }; SERVERDLL_API void func(void); #endif
//实现文件.testClass.cpp
- #include "testClass.h"
- void TestClass::VirtualFunction(void)
- {
- cout<< "this is VirtualFunction()!" <<endl;
- return;
- }
- void TestClass::NormalFunction(void)
- {
- cout<< "this is NormalFunction()!" <<endl;
- return;
- }
- void func(void)
- {
- cout << "xixi haha" <<endl;
- return;
- }
#include "testClass.h" void TestClass::VirtualFunction(void) { cout<< "this is VirtualFunction()!" <<endl; return; } void TestClass::NormalFunction(void) { cout<< "this is NormalFunction()!" <<endl; return; } void func(void) { cout << "xixi haha" <<endl; return; }
//调试代码
- #include <iostream>
- #include <windows.h>
- #include <e://myself//MyProject//网络程序//ServerDLL//testClass.h>
- using namespace std;
- #pragma comment(lib, "E://myself//MyProject//网络程序//ServerDLL//Debug//ServerDLL.lib")
- //TestClass类为DLL导出的类
- class MY : public TestClass
- {
- public:
- virtual void VirtualFunction()
- {
- cout<< "my VirtualFunction()" <<endl;
- return;
- }
- void NormalFunction()
- {
- cout<< "my NormalFunction()" <<endl;
- return;
- }
- };
- int main(void)
- {
- MY my;
- TestClass *tc = &my;
- tc->NormalFunction();
- tc->VirtualFunction();
- //调用导出的函数
- func();
- cout<<"liu zhiliang"<<endl;
- system("PAUSE");
- return 0;
- }
#include <iostream> #include <windows.h> #include <e://myself//MyProject//网络程序//ServerDLL//testClass.h> using namespace std; #pragma comment(lib, "E://myself//MyProject//网络程序//ServerDLL//Debug//ServerDLL.lib") //TestClass类为DLL导出的类 class MY : public TestClass { public: virtual void VirtualFunction() { cout<< "my VirtualFunction()" <<endl; return; } void NormalFunction() { cout<< "my NormalFunction()" <<endl; return; } }; int main(void) { MY my; TestClass *tc = &my; tc->NormalFunction(); tc->VirtualFunction(); //调用导出的函数 func(); cout<<"liu zhiliang"<<endl; system("PAUSE"); return 0; }
输出:
- this is NormalFunction()!
- my VirtualFunction()
- xixi haha
- liu zhiliang
- 请按任意键继续. . .
转载于:https://blog.51cto.com/rosehacker/803649