Static library and export function, export class

Source: Internet
Author: User
Tags export class

1. Static Link Library

Create a "Win32 project" and select Lib in "application settings. Create the Lib. h and Lib. cpp files. The source code of Lib. h and Lib. cpp is as follows:

// File: Lib. h # ifndef lib_h # define lib_hextern "C" int add (int x, int y); // declare as an external function of C compilation and connection mode # endif // file: Lib. CPP # include "Lib. H "int add (int x, int y) {return X + Y ;}

After compilation, the DLL and Lib files are generated. The following describes how to use them. The source code is as follows:

# Include <stdio. h> # include ".. \ Lib. H "# pragma comment (Lib ,".. \ debug \ libtest. lib ") // specify to connect int main (INT argc, char * argv []) {printf (" 2 + 3 = % d ", add (2, 3 ));}

2. Export Functions

Create a Win32 project and select DLL in application settings. Create two files: commendll. h and commendll. cpp. In commendll. h

namespace CommenDLL{         class MathFun         {         public:                   static __declspec(dllexport)double Add(double a, double b);                   static __declspec(dllexport)double Minus(double a, double b);         };}

In commendll. cpp

namespace CommenDLL{         double MathFun::Add(double a, double b)         {                   return a+b;         }         double MathFun::Minus(double a, double b)         {                   return a-b;         }}

3. Export class

In the header file of the same project

class __declspec(dllexport) Circle{public:         Circle(){};         ~Circle(){};         void   SetCenter(Point point);         void   SetRadius(int r);         double GetZhouChang();         double GetMianJi();private:         Point center;         int radius;};

CPP File

void Circle::SetCenter(Point point){    this->center.x = point.x;    this->center.y = point.y;}void Circle::SetRadius(int r){    this->radius = r;}double Circle::GetZhouChang(){    const double pi = 3.1415926;    return this->radius*pi*2;}double Circle::GetMianJi(){    const double pi = 3.1415926;    return pi*this->radius*this->radius;}

When calling, import the generated library and directly use its functions or 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.