zoukankan      html  css  js  c++  java
  • 【C++】多个类的DLL封装及调用

     

    【C++】多个类的DLL封装及调用

    分类: 【编程语言】
     

    目录(?)[+]

     

    网上大多是将函数封装成dll的教程,类的封装也是基本相似的。

    创建DLL

    在VS2010中新建一个win32->dll工程。如我建立的工程名为FaceDLL

    添加facedll.h的头文件(里面定义dll的接口,调用时会用到)

    [cpp] view plaincopy
     
    1. #pragma once  
    2. #ifdef FaceLIBDLL  
    3. #define FACEAPI _declspec(dllexport)  
    4. #else  
    5. #define FACEAPI  _declspec(dllimport)  
    6. #endif  
    7. //可以include需要用到的头文件  
    8. #include <opencv2/opencv.hpp>  
    9.   
    10. class FACEAPI  FaceRecognizer  
    11. {  
    12. public:  
    13.     FaceRecognizer();  
    14.     ~FaceRecognizer();  
    15.   
    16. /////////////////////////////////////  
    17. //类的函数  
    18.   
    19. };  
    之后在facedll.cpp中写函数实现,而且要定义为 FaceLIBDLL
    [cpp] view plaincopy
     
    1. #define FaceLIBDLL  
    2.   
    3. #include "stdafx.h"  
    4. #include "facedll.h"  
    5. #include <opencv2/opencv.hpp>  
    6.   
    7. //////////头文件中函数的实现  
    8. FaceRecognizer::FaceRecognizer()   
    9. {     
    10.   
    11. }   
    12.   
    13. FaceRecognizer::~FaceRecognizer()   
    14. {     
    15.   
    16. }   
    生成(Build)工程,在debug文件夹中会生成相应的DLL及LIB文件:facedll.dll   facedll.lib

    多个类封装DLL

    封装好一个类之后,在后面的类可以调用这个类生成的dll,再封装新类的dll。

    需要在工程中添加需要引用的头文件,如facedll.h。在debug中拷贝facedll.lib文件。在 Properties->Linker->Input->  Additional Dependecies中添加facedll.lib(或写全路径:"..debugfacedll.lib")

    然后一样的方法再封装新的类就可以了~

    [cpp] view plaincopy
     
    1. #pragma once  
    2. #ifdef HEARTLIBDLL  
    3. #define HEARTAPI _declspec(dllexport)  
    4. #else  
    5. #define HEARTAPI  _declspec(dllimport)  
    6. #endif  
    7.   
    8. #include <opencv2/opencv.hpp>  
    9. #include "facedll.h"  
    10. #include "datadll.h"  
    11.   
    12. class HEARTAPI  HRMeasure  
    13. {  
    14. };  

    调用DLL

    调用需要各个dll的.h、.dll、.lib文件。
    将头文件添加到工程中,并#include到需要用的地方。将lib文件拷贝到项目中,并在 Properties->Linker->Input->  Additional Dependecies 中写入:facedll.lib;heartdll.lib。
    或者在程序中写入:
    [cpp] view plaincopy
     
    1. #pragma  comment(lib,"facedll.lib")  
    2. #pragma  comment(lib,"heartdll.lib")  
    之后程序中就可以直接使用封装成DLL的类了:
    [cpp] view plaincopy
     
    1. HRMeasure *hrMea=new HRMeasure();  
  • 相关阅读:
    C++虚函数表解析(转)
    学习网址
    css 段落文字换行问题
    移动端fixed兼容问题
    半数集1
    汇编寄存器
    设计模式概述
    Vector用法介绍
    汇编PC硬件基本特征
    android 反编译总结
  • 原文地址:https://www.cnblogs.com/jack-jia-moonew/p/4233073.html
Copyright © 2011-2022 走看看