zoukankan      html  css  js  c++  java
  • windows库的创建和使用:静态库+动态库

    windows库的创建和使用:静态库+动态库

     

    一、静态库的创建和使用

    1. 静态库创建

    1)首先创建projecttest,測试代码例如以下:

    1) test.h

    void test_print();

    2) test.cpp

    #include "test.h"

    #include <stdio.h>

    void test_print()

    {

         printf("test_print in static lib.");

    }

    3)右击projecttest:属性——>配置属性——>常规——>配置类型:选择静态库lib

    4)编译project(build),然后会在解决方式里的Debug下会生成test.lib

     

    2. 使用静态库 = lib文件 头文件

    1)建一个新的projectuselib

    2)点击资源文件——>加入已有项——>加入刚才生成的库test.lib

    3)右击属性——>VC++文件夹——>包括文件夹:将test.h所在文件夹加入进来

    project代码例如以下:

    1)main.cpp

    #include "test.h"

    #include<stdio.h>

    int main()

    {

          test_print();

         getchar();

    }

    调试就能看到调用成功。

     

    二、 动态库的创建与使用

    1. 静态载入动态库

    1)创建projectstatic_load

    static_dll.h

    //导出类

    class __declspec(dllexport) static_test

    {

    public:

             void print();

    };

    //导出函数

    __declspec(dllexportvoid static_function();

    //导出对象

    extern __declspec(dllexport) static_test  static_ObjTest;

    static_dll.cpp

    #include "static_dll.h"

    #include <stdio.h>

    void static_test::print()

    {

          printf("static_test::print() in static load dll. ");

    }

    void static_function()

    {

         printf("static_function in static load dll. ");

    }

    static_test  static_ObjTest;

     

     

    __declspec(dllexport)表示作为动态库的导出项。否则会找不到变量或者函数或者类等。

    配置类型选择:动态库(dll).

     

    2)静态载入的方式使用动态库

    首先创建一个dll.h

    //导

    class __declspec(dllimport) static_test

    {

    public:

             void print();

    };

    //导函数

    __declspec(dllimportvoid static_function();

    //导对象

    extern __declspec(dllimport) static_test  static_ObjTest;

    __declspec(dllimport)表明该项是从动态库导入的外部导入项。

    创建一个測试projectuselib

    main.cpp

    #include "dll.h"

    #include<stdio.h>

    int main()

    {

            static_test st;

            st.print();

           static_function();

           static_ObjTest.print();

           getchar();

    }

    同一时候须要将static_load.libstatic_load.dll放到同一文件夹下,.lib文件导入到资源文件下。

     

    2 动态载入动态库

    1. 为差别对待这里建立projectdynamic_load

    dynamic_dll.h

    //导出类

    class __declspec(dllexport) dynamic_test

    {

    public:

            void print();

    };

    //导出函数

    __declspec(dllexportvoid dynamic_function();

    //导出对象

    extern __declspec(dllexport) dynamic_test  dynamic_ObjTest;

    dynamic_dll.cpp

    #include "dynamic_dll.h"

    #include <stdio.h>

    void dynamic_test::print()

    {

             printf("dynamic_test::print() in dynamic load dll.");

    }

    void static_function()

    {

             printf("dynamic_function in dynamic load dll.");

    }

    dynamic_test  dynamic_ObjTest;

    配置类型选择:动态库(dll).然后编译(build)

     

    创建一个projectuselib

    #include<stdio.h>

    #include<Windows.h>

    int main()

    {

            HINSTANCE hDLL = LoadLibraryA("dynamic_load.dll"); 

            if(hDLL==NULL)

            {

                    FreeLibrary(hDLL);

                    return 0;

             }

             typedef void(_stdcall*func)();

             func f;

             f = (func)GetProcAddress(hDLL,"dynamic_function");

            if(f) (*f)();

            getchar();

    }

    注意我们必须在LoadLibraryA中传入dll的准确路径,且若使用char*的路径。则使用LoadLibraryA,否则若是unicode则使用LoadLibraryW。至于动态导入类有点麻烦,故在此先不讨论。

    有一篇非常好地资料:http://www.cnblogs.com/skynet/p/3372855.html

    动态载入dll中的类:http://www.cppblog.com/codejie/archive/2009/09/24/97141.html

  • 相关阅读:
    linux嵌入式终端ssh无法输入中文以及删除中文异常
    DNS解析错误导致无法ping通网络
    嵌入式linux 打开ping端口
    嵌入式Linux vim编辑器支持中文
    常见BUG-Web
    考勤项目
    功能测试报告的编写
    04_postman环境变量和身份验证
    03_postman的collection管理
    02_postman断言
  • 原文地址:https://www.cnblogs.com/llguanli/p/8882279.html
Copyright © 2011-2022 走看看