DLL encapsulation and invocation of "C + +" multiple classes

Source: Internet
Author: User

 DLL encapsulation and invocation of "C + +" multiple classesCategory: "Programming language"2012-07-11 18:53 6399 people read review (a) collection report dllc++includebuild2010

Directory (?) [+]

Most of the online tutorial is to encapsulate functions as DLLs, and the encapsulation of classes is basically similar.

Creating DLLs

Create a new Win32->dll project in the VS2010. As I set up the project named Facedll

Add facedll.h header file (the interface that defines the DLL, which is used when called)

[CPP]View Plaincopy
  1. #pragma once
  2. #ifdef Facelibdll
  3. #define FACEAPI _declspec (dllexport)
  4. #else
  5. #define FACEAPI _declspec (dllimport)
  6. #endif
  7. You can include header files that you need to use
  8. #include <opencv2/opencv.hpp>
  9. Class Faceapi Facerecognizer
  10. {
  11. Public
  12. Facerecognizer ();
  13. ~facerecognizer ();
  14. /////////////////////////////////////
  15. Functions of the class
  16. };
The function implementation is then written in Facedll.cpp and is defined as Facelibdll

[CPP]View Plaincopy
    1. #define  FaceLIBDLL  
    2.   
    3. #include   "stdafx.h"    
    4. #include   "facedll.h" &NBSP;&NBSP;
    5. #include  <opencv2/opencv.hpp>  
    6.   
    7. //////////the implementation of functions in the header file   
    8. Facerecognizer::facerecognizer ()    
    9. {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
    10.   
    11. }   
    12.   
    13. facerecognizer::~facerecognizer ()    
    14. {      
    15. &NBSP;&NBSP;
    16. }   
Build project, the corresponding DLL and LIB files will be generated in the Debug folder: Facedll.dll facedll.lib

Multiple classes encapsulate DLLs

After encapsulating a class, the following class can invoke the DLL generated by the class, and then encapsulate the DLL for the new class.

You need to add header files, such as facedll.h, that need to be referenced in your project. Copy the Facedll.lib file in Debug. In properties->linker->input-> Additional dependecies add facedll.lib (or write full path: "... \debug\facedll.lib ")

And then the same way to encapsulate the new class is the ~

[CPP]View Plaincopy
  1. #pragma once
  2. #ifdef Heartlibdll
  3. #define HEARTAPI _declspec (dllexport)
  4. #else
  5. #define HEARTAPI _declspec (dllimport)
  6. #endif
  7. #include <opencv2/opencv.hpp>
  8. #include "facedll.h"
  9. #include "datadll.h"
  10. Class Heartapi Hrmeasure
  11. {
  12. };

Calling DLL calls requires the. h,. dll,. lib files for each DLL. Add a header file to your project and include it where you need it. The Lib file is copied to the project and written in Properties->linker->input-> Additional dependecies: Facedll.lib;heartdll.lib. or write in the program: [CPP]View Plaincopy
    1. #pragma comment (lib, "Facedll.lib")
    2. #pragma comment (lib, "Heartdll.lib")
Classes that are encapsulated into DLLs can then be used directly in the program: [CPP]View Plaincopy
    1. Hrmeasure *hrmea=New Hrmeasure ();

DLL encapsulation and invocation of "C + +" multiple classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.