環境:VS2010
例如有MyClass類,創建DLL工程,創建.h檔案和.cpp檔案,我這里以myclass.h,myclass.cpp為例。
myclass.h檔案
class _declspec (dllexport) MyClass
{
public:
void show();
};
myclass.cpp檔案:類成員函式的具體實作
void MyClass::show()
{
cout<<"自定義類MyClass"<<endl;
}
生成dll檔案和lib檔案(工具列--》生成--》生成解決方案),至此,在你的工程檔案夾下面的debug檔案夾下會看到生成的dll檔案與lib檔案。
DLL隱式呼叫:
創建控制臺應用,建立一個空專案就可以,添加.h檔案和.cpp檔案,我這里以test.h和test.cpp為例。
為工程添加完test.h和test.cpp后,先編譯一下,生成debug檔案(注意要寫main()函式)
將DLL工程生成的dll檔案和lib檔案添加到test工程中,具體方法如下:
把你生成的dll添加到debug目錄下(有.exe檔案的目錄),lib檔案添加到工程中(工程檔案右擊--》添加--》現有項--》找到你的lib檔案--》添加),.h檔案也需要添加到程式運行目錄下(不是debug目錄,是你工程存放h檔案與cpp檔案的目錄),但是.h檔案需要做修改,將
class _declspec (dllexport) MyClass改為class _declspec (dllimport) MyClass
test.h檔案:
#ifndef _TEST_H__
#define _TEST_H__
這里包含頭檔案
#include<iostream>
此處需要包含#include“myclass.h”
using namespace std;
#endif
test,cpp檔案:
包含#include“test.h”
int main(int argc,char* argv[])
{
此處可以使用MyClass類了
MyClass mc;
mc. show();
getchar();
return 0;
}
注意:dll檔案、lib檔案、.h檔案添加是非常重要的,不能放錯位置,.h檔案一定要做修改
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/27883.html
標籤:系統維護與使用區
