zoukankan      html  css  js  c++  java
  • 动态链接库

    参考他人的基础上自己的总结 

    一、创建动态链接库项目: 

    1、打开Microsoft Visual Studio 2010,选择File->New->Project。 

    2、在New Project中选择Installed Templates->Visual C++->Win32。 

    3、选择Win32 Console Application,设置名称:simpledll,设置解决方案名:zdddll。 

    4、单击OK,在出现的Win32 Application Wizard的Overview对话框中点击Next。 

    5、在Application Settings中,选择Application type下的DLL。 

    6、勾选Additional options下的Empty project。 

    7、单击Finish创建项目。 


    向动态链接库添加类: 

    1、添加新类头文件。右键单击simpledll项目,Add->New Item,选择Header File(.h),设置名称为simpledll,单击Add。 

    2、添加新类源文件。右键单击simpledll项目,Add->New Item,选择C++ File(.cpp),设置名称为simpledll,单击Add。 

    3、为新类添加内容。内容如下: 

    头文件simpledll.h: 

    //------------------ simpledll.h ---------------- 
    #pragma once; 

    //该宏完成在dll项目内部使用__declspec(dllexport)导出 

    //在dll项目外部使用时,用__declspec(dllimport)导入 

    //宏DLL_IMPLEMENT在simpledll.cpp中定义 

    #ifdef DLL_IMPLEMENT 

    #define DLL_API __declspec(dllexport) 

    #else 

    #define DLL_API __declspec(dllimport) 

    #endif 

    namespace zdd 


    //导出类 

    class DLL_API SimpleDll 


    public: 

    SimpleDll(); 

    ~SimpleDll(); 

    int add(int x, int y); //简单方法 

    }; 


    源文件simpledll.cpp: 

    //------------------ simpledll.cpp ---------------- 

    //注意此处的宏定义需要写在#include "simpledll.h"之前 

    //以完成在dll项目内部使用__declspec(dllexport)导出 

    //在dll项目外部使用时,用__declspec(dllimport)导入 

    #define DLL_IMPLEMENT 
    #include "simpledll.h" 
    namespace zdd 


    SimpleDll::SimpleDll() 



    SimpleDll::~SimpleDll() 


    int SimpleDll::add(int x, int y) 

    return x+y; 




    4、完成后点击Build->Build Solution,生成解决方案。可在~zdddllDebug下查看生成的simpledll.lib和simpledll.dll.文件。 

    二、创建引用动态链接库的应用程序: 

    1、选择File->New->Project。 

    2、在New Project中选择Installed Templates->Visual C++->Win32。 

    3、选择Win32 Console Application,设置名称:usesimpledll。选择Add to solution。 

    4、单击OK,在出现的Win32 Application Wizard的Overview对话框中点击Next。 

    5、在Application Settings中,选择Application type下的Console application。 

    6、取消Additional options下的Precompiled header,勾选Empty project。 

    7、单击Finish创建项目。 


    在控制台应用程序中使用类库的功能: 

    1、为控制台应用程序添加main.cpp。右键单击usesimpledll项目,Add->New Item,选择C++ File(.cpp),设置名称为main,单击Add。 

    2、为main.cpp添加内容。如下所示: 

    //------------------ main.cpp ------------------- 

    #include "simpledll.h" 
    using namespace zdd; 
    #include 
    using namespace std; 

    int main(char argc, char**argv) 

    // 
    cout << "----------------------" <<endl; <br=""> SimpleDll sd; 

    cout << "sd.add: 3+5=" << sd.add(3, 5)<<endl; <br=""> 
    cout << "sd.getConst(): "<<sd.getconst()<<endl; <br=""> 
    SimpleDll *psd = new SimpleDll; 

    cout << "psd->add: 5+5=" << psd->add(5, 5)<<endl; <br=""> 
    cout << "psd->getConst(): "<<endl; <br=""> 
    cout << "----------------------" <<endl; <br=""> 
    cout << "please press Enter exit."<<endl; <br=""> 
    getchar(); 

    return 0; 


    3.在工程目录下建立Include目录,将动态链接库的那个头文件拷入。建立lib目录,将生成的那个.lib文件拷入。然后将生成的.dll文件拷入生成.exe文件的那个目录(一般是项目下的Debug下)。 

    4.程序中要包含那个头文件,注意路径要写正确。Include “..Includesimpledll.h”,或者右击工程,property,Configuration Properties,c/c++,General,在Additional Include Directories中加入“;..Include”,这样包含头文件时直接写头文件名,不需要考虑路径,因为当在工程目录下找不到文件时,就会从添加的那个目录查找文件。 

    5.添加.lib文件 

    右击工程,property,Configuration Properties,Linker,Input,在Additional Dependencies中添加.lib路径(一般是..libxxxxx.lib)。 


    另外,lib引用有两种方法: 
    1.#pragma comment(lib,”opengl32.lib”) 
    2.选择project –> XX properties… –> linker –> Input –> Additional dependences,在其中加入lib文件名即可。 


    总结: 
    首先建立生成DLL的工程,生成.dll,.lib文件。需要用到的还有.h文件。 
    建立应用DLL的工程。要包含头文件,把3个文件拷入相应的目录。 
    在附加依赖项Additional Dependencies中添加.lib的路径,告诉程序调用的外部导入函数的地址,否则找不到函数,链接出错。 

  • 相关阅读:
    将python 中的变量保存到本地
    Docker 安装
    docker 守护进程
    迁移及配置 Windows 7 的用户账号 UserProfile 默认目录位置
    Bitwise Operation位操作(逻辑运算)
    博客园客服端上传文章测试
    vnc连接远端linux服务器
    linux7.5服务器 安装oracle12c
    linux服务器 jboss-7安装
    day11__函数名的应用,python新特f-strings格式化输出、迭代器
  • 原文地址:https://www.cnblogs.com/xzh1993/p/5389962.html
Copyright © 2011-2022 走看看