小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

C/C++(2)VS2015生成并使用自定義動態(tài)庫dll/靜態(tài)庫lib

 imelee 2017-09-24

動態(tài)庫:

1.新建win32控制臺應用程序DLLTEST,選中DLL(D)、預編譯頭(P)、導出符號(X)、自動生成了如下文件:

  //DLLTEST.h文件
  // 下列 ifdef 塊是創(chuàng)建使從 DLL 導出更簡單的
    // 宏的標準方法。此 DLL 中的所有文件都是用命令行上定義的 DLLTEST_EXPORTS
    // 符號編譯的。在使用此 DLL 的
    // 任何其他項目上不應定義此符號。這樣,源文件中包含此文件的任何其他項目都會將
    // DLLTEST_API 函數(shù)視為是從 DLL 導入的,而此 DLL 則將用此宏定義的
    // 符號視為是被導出的。
    #ifdef DLLTEST_EXPORTS
    #define DLLTEST_API __declspec(dllexport)
    #else
    #define DLLTEST_API __declspec(dllimport)
    #endif

    // 此類是從 DLLTEST.dll 導出的
    class DLLTEST_API CDLLTEST {
    public:
        CDLLTEST(void);
        // TODO:  在此添加您的方法。
    };

    extern DLLTEST_API int nDLLTEST;

    DLLTEST_API int fnDLLTEST(void);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  //DLLTEST.cpp文件
    // DLLTEST.cpp : 定義 DLL 應用程序的導出函數(shù)。
    //

    #include "stdafx.h"
    #include "DLLTEST.h"


    // 這是導出變量的一個示例
    DLLTEST_API int nDLLTEST=0;

    // 這是導出函數(shù)的一個示例。
    DLLTEST_API int fnDLLTEST(void)
    {
        return 42;
    }

    // 這是已導出類的構造函數(shù)。
    // 有關類定義的信息,請參閱 DLLTEST.h
    CDLLTEST::CDLLTEST()
    {
        return;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

2.類比著添加自定義的變量、函數(shù)和類,具體如下:

  //DLLTEST.h文件
    // 下列 ifdef 塊是創(chuàng)建使從 DLL 導出更簡單的
    // 宏的標準方法。此 DLL 中的所有文件都是用命令行上定義的 DLLTEST_EXPORTS
    // 符號編譯的。在使用此 DLL 的
    // 任何其他項目上不應定義此符號。這樣,源文件中包含此文件的任何其他項目都會將
    // DLLTEST_API 函數(shù)視為是從 DLL 導入的,而此 DLL 則將用此宏定義的
    // 符號視為是被導出的。
    #ifdef DLLTEST_EXPORTS
    #define DLLTEST_API __declspec(dllexport)
    #else
    #define DLLTEST_API __declspec(dllimport)
    #endif

    #include <iostream>

    using namespace std;

    // 此類是從 DLLTEST.dll 導出的
    class DLLTEST_API CDLLTEST {            //導出的類
    public:
        CDLLTEST(void);
        // TODO:  在此添加您的方法。
        int mul(int x, int y);
        double b;
    };

    class DLLTEST_API MyDLL {
    public:
        int a;
        MyDLL();
        int add(int x, int y);
    };

    extern DLLTEST_API int nDLLTEST;        //導出的變量
    extern DLLTEST_API double mydll;

    DLLTEST_API int fnDLLTEST(void);        //導出的函數(shù)
    DLLTEST_API int sub(int x, int y);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  //DLLTEST.cpp文件
    // DLLTEST.cpp : 定義 DLL 應用程序的導出函數(shù)。
    #include "stdafx.h"
    #include "DLLTEST.h"


    // 這是導出變量的一個示例
    DLLTEST_API int nDLLTEST=0;
    DLLTEST_API double mydll = 0.999;

    // 這是導出函數(shù)的一個示例。
    DLLTEST_API int fnDLLTEST(void)
    {
        return 42;
    }

    DLLTEST_API int sub(int x, int y)
    {
        return x - y;
    }

    // 這是已導出類的構造函數(shù)。
    // 有關類定義的信息,請參閱 DLLTEST.h
    CDLLTEST::CDLLTEST()
    {
        b = 0.111;
        return;
    }

    int CDLLTEST::mul(int x, int y)
    {
        return x * y;
    }

    MyDLL::MyDLL()
    {
        a = 555;
        cout << "this is the constructor of my DLL!" << endl;
    }

    int MyDLL::add(int x, int y)
    {
        return x + y;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

3.分別在debug和release模式下生成庫文件DLLTEST.dll和DLLTEST.lib.
4.新建測試工程:win32控制臺應用程序Test,將debug(release下同樣)模式下生成的庫文件DLLTEST.dll復制到測試工程目錄下,
將生成的庫文件DLLTEST.lib和頭文件DLLTEST.h復制到測試工程下的新建目錄DLLTEST下,添加main.cpp文件,
測試代碼如下:

    #include <iostream>
    using namespace std;

    #include "DLLTEST\DLLTEST.h"
    #pragma comment(lib,"DLLTEST\\DLLTEST.lib")

    int main() {
        CDLLTEST cd;
        cout << "CDLLTEST -> b = " << cd.b << endl;
        cout << "CDLLTEST -> mul(2, 3) = " << cd.mul(2, 3) << endl;

        MyDLL md;
        cout << "MyDLL -> a = " << md.a << endl;
        cout << "MyDLL -> add(2, 3) = " << md.add(2, 3) << endl;

        cout << "DLL:mydll = " << mydll << endl;
        cout << "DLL:nDLLTEST = " << nDLLTEST << endl;

        cout << "DLL:fnDLLTEST = " << fnDLLTEST() << endl;
        cout << "DLL:sub(2, 3) = " << sub(2, 3) << endl;

        return 0;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

5.運行,輸出如下ok:
CDLLTEST -> b = 0.111
CDLLTEST -> mul(2, 3) = 6
this is the constructor of my DLL!
MyDLL -> a = 555
MyDLL -> add(2, 3) = 5
DLL:mydll = 0.999
DLL:nDLLTEST = 0
DLL:fnDLLTEST = 42
DLL:sub(2, 3) = -1

靜態(tài)庫:

1.新建靜態(tài)庫工程LIBTEST,添加代碼如下:

//LIBTEST.h
#pragma once
class LIBTEST
{
public:
LIBTEST();
int add(int x, int y);
~LIBTEST();
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
//LIBTEST.cpp
#include "LIBTEST.h"

LIBTEST::LIBTEST()
{
}

int LIBTEST::add(int x, int y)
{
return x + y;
}

LIBTEST::~LIBTEST()
{
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

2.分別在debug和release模式下生成庫文件LIBTEST.lib,將debug下生成的庫文件修改名字為LIBTESTd.lib.
3.將庫文件LIBTEST.lib、LIBTESTd.lib和頭文件LIBTEST.h拷貝到測試工程目錄下,編寫測試程序

// main.cpp
#include <iostream>
#include "LIBTEST/LIBTEST.h"

#ifdef _DEBUG
#pragma comment(lib, "LIBTEST/LIBTESTd.lib")
#else 
#pragma comment(lib, "LIBTEST/LIBTEST.lib")
#endif // 

using namespace std;

int main() {
LIBTEST lt;
cout << lt.add(1, 2) << endl; 

return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

4.分別在debug和release模式下運行即ok.

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多