Liny_@NotePad

沉迷ACG中

【20090915】C++培训日记-模块化编程

YOYO posted @ 2009年9月16日 00:16 in 【C/C++】 with tags dll lib , 2125 阅读
今天学习了lib和dll隐式链接、显式链接 = =。

下面是demo。。使用时请注意VS中包含文件目录和附加库目录的设定,或直接将对应的h、lib、dll拷贝到使用工程的目录下。

【静态库】

声明:

//////////////////////////////////////////////////////////////////////////
//      CopyRight(c) 2009, YOYO, All Rights Reserved.
//      Author: LIN YiQian
//      Created: 2009/09/15
//      Describe: 静态库 声明
//////////////////////////////////////////////////////////////////////////
#ifndef _LIB_CALCULATE_H_
#define _LIB_CALCULATE_H_
 
int     Add(int nNum1, int nNum2);
 
int Minus(int nNum1, int nNum2);
 
#endif  //        end of define _LIB_CALCULATE_H_

定义:

//////////////////////////////////////////////////////////////////////////
//      CopyRight(c) 2009, YOYO, All Rights Reserved.
//      Author: LIN YiQian
//      Created: 2009/09/15
//      Describe: 静态库 定义
//////////////////////////////////////////////////////////////////////////
#include "Calculate.h"
 
int Add(int nNum1, int nNum2)
{
        return nNum1 + nNum2;
}
 
int Minus(int nNum1, int nNum2)
{
        return nNum1 - nNum2;
}

使用:

//////////////////////////////////////////////////////////////////////////
//      CopyRight(c) 2009, YOYO, All Rights Reserved.
//      Author: LIN YiQian
//      Created: 2009/09/15
//      Describe: 静态库 使用
//////////////////////////////////////////////////////////////////////////
#include <iostream>
#include "Calculate.h"
 
#pragma comment(lib, "静态库.lib")
 
int main(void)
{
        std::cout << Add(1, 2) << std::endl;
        std::cout << Minus(1, 2) << std::endl;
        system("pause");
        return 0;
}

【动态库隐式链接】

声明:

//////////////////////////////////////////////////////////////////////////
//      CopyRight(c) 2009, YOYO, All Rights Reserved.
//      Author: LIN YiQian
//      Created: 2009/09/15
//      Describe: 动态库隐式链接 声明
//////////////////////////////////////////////////////////////////////////
#ifndef _DLL_HIDE_CALCULATE_H_
#define _DLL_HIDE_CALCULATE_H_
 
#ifdef CALCULATE_EXPORTS
#define CALCULATE_API __declspec(dllexport)
#else
#define CALCULATE_API __declspec(dllimport)
#endif
 
CALCULATE_API int Add(int nNum1, int nNum2);
CALCULATE_API int Minus(int nNum1, int nNum2);
 
#endif  //        end of define _DLL_HIDE_CALCULATE_H_

定义:

//////////////////////////////////////////////////////////////////////////
//      CopyRight(c) 2009, YOYO, All Rights Reserved.
//      Author: LIN YiQian
//      Created: 2009/09/15
//      Describe: 动态库隐式链接 定义
//////////////////////////////////////////////////////////////////////////
#include "Calculate.h"
 
int Add(int nNum1, int nNum2)
{
        return nNum1 + nNum2;
}
 
int Minus(int nNum1, int nNum2)
{
        return nNum1 - nNum2;
}

使用:

//////////////////////////////////////////////////////////////////////////
//      CopyRight(c) 2009, YOYO, All Rights Reserved.
//      Author: LIN YiQian
//      Created: 2009/09/15
//      Describe: 动态库隐式链接 使用
//////////////////////////////////////////////////////////////////////////
#include <iostream>
#include "Calculate.h"
 
#pragma comment(lib, "动态库隐式链接.lib")
 
int main(void)
{
        std::cout << Add(1, 2) << std::endl;
        std::cout << Minus(1, 2) << std::endl;
 
        system("pause");
        return 0;
}

【动态库显式链接】

声明:

//////////////////////////////////////////////////////////////////////////
//      CopyRight(c) 2009, YOYO, All Rights Reserved.
//      Author: LIN YiQian
//      Created: 2009/09/15
//      Describe: 动态库显式链接 声明
//////////////////////////////////////////////////////////////////////////
#ifndef _DLL_SHOW_CALCULATE_H_
#define _DLL_SHOW_CALCULATE_H_
 
#ifdef CALCULATE_EXPORTS
#define CALCULATE_API __declspec(dllexport)
#else
#define CALCULATE_API __declspec(dllimport)
#endif
 
extern "C" CALCULATE_API int Add(int nNum1, int nNum2);
 
extern "C" CALCULATE_API int Minus(int nNum1, int nNum2);
 
#endif  //        end of define _DLL_SHOW_CALCULATE_H_

定义:

//////////////////////////////////////////////////////////////////////////
//      CopyRight(c) 2009, YOYO, All Rights Reserved.
//      Author: LIN YiQian
//      Created: 2009/09/15
//      Describe: 动态库显式链接 定义
//////////////////////////////////////////////////////////////////////////
#include "Calculate.h"
 
int Add(int nNum1, int nNum2)
{
        return nNum1 + nNum2;
}
 
int Minus(int nNum1, int nNum2)
{
        return nNum1 - nNum2;
}

使用:

//////////////////////////////////////////////////////////////////////////
//      CopyRight(c) 2009, YOYO, All Rights Reserved.
//      Author: LIN YiQian
//      Created: 2009/09/15
//      Describe: 动态库显式链接 使用
//////////////////////////////////////////////////////////////////////////
#include <Windows.h>
#include <iostream>
 
int main(void)
{
        HMODULE hModule = LoadLibrary("动态库显式链接.dll");
        if (NULL != hModule)
        {
                int (*pFun)(int, int) = (int (*)(int, int)) (GetProcAddress(hModule, "Add"));
                std::cout << pFun(1, 2) << std::endl;
 
                pFun = (int (*)(int, int)) (GetProcAddress(hModule, "Minus"));
                std::cout << pFun(1, 2) << std::endl;
        }
        FreeLibrary(hModule);
 
        system("pause");
        return 0;
}

【在dll中使用类】

对外接口:

//////////////////////////////////////////////////////////////////////////
//      CopyRight(c) 2009, YOYO, All Rights Reserved.
//      Author: LIN YiQian
//      Created: 2009/09/15
//      Describe: dll类 接口
//////////////////////////////////////////////////////////////////////////
#ifndef _DLL_INTERFACE_CALCULATE_H_
#define _DLL_INTERFACE_CALCULATE_H_
 
#ifdef CALCULATE_EXPORT
#define CALCULATE_API __declspec(dllexport)
#else
#define CALCULATE_API __declspec(dllimport)
#endif
 
class ICalculate
{
public:
        virtual int Add(int nNum1, int nNum2) = 0;
 
        virtual int Minus(int nNum1, int nNum2) = 0;
};
 
extern "C" CALCULATE_API ICalculate* ModuleCreate();
 
#endif  //        end of define _DLL_INTERFACE_CALCULATE_H_

具体类声明:

//////////////////////////////////////////////////////////////////////////
//      CopyRight(c) 2009, YOYO, All Rights Reserved.
//      Author: LIN YiQian
//      Created: 2009/09/15
//      Describe: dll类 声明
//////////////////////////////////////////////////////////////////////////
#ifndef _DLL_CLASS_CALCULATE_H_
#define _DLL_CLASS_CALCULATE_H_
 
#include "ICalculate.h"
 
class CCalculate: public ICalculate
{
public:
        int Add(int nNum1, int nNum2);
 
        int Minus(int nNum1, int nNum2);
};
 
#endif  //        end of define _DLL_CLASS_CALCULATE_H_

具体类定义:

//////////////////////////////////////////////////////////////////////////
//      CopyRight(c) 2009, YOYO, All Rights Reserved.
//      Author: LIN YiQian
//      Created: 2009/09/15
//      Describe: dll类 实现
//////////////////////////////////////////////////////////////////////////
#include "CCalculate.h"
 
int CCalculate::Add(int nNum1, int nNum2)
{
        return nNum1 + nNum2;
}
 
int CCalculate::Minus(int nNum1, int nNum2)
{
        return nNum1 - nNum2;
}
 
extern "C" ICalculate* ModuleCreate()
{
        return new CCalculate();
}

 使用:

//////////////////////////////////////////////////////////////////////////
//      CopyRight(c) 2009, YOYO, All Rights Reserved.
//      Author: LIN YiQian
//      Created: 2009/09/15
//      Describe: dll类 使用
//////////////////////////////////////////////////////////////////////////
#include <Windows.h>
#include <iostream>
#include "ICalculate.h"
 
void main(void)
{
        HMODULE hModule = LoadLibrary("DLL类.dll");
        if (NULL != hModule)
        {
                ICalculate* (*pFun)() = (ICalculate*(*)())GetProcAddress(hModule, "ModuleCreate");
                ICalculate* pCalculate = (*pFun)();
 
                std::cout << (pCalculate->Add(1, 2)) << std::endl;
                std::cout << (pCalculate->Minus(1, 2)) << std::endl;
        }
        FreeLibrary(hModule);
 
        system("pause");
        return;
}

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter