• c++动态库调用


            在平时的开发中某些情况,动态库和静态库是程序开发的不二法门,例如封装一个库,供别人调用(日志库、字符串处理库、设备信息采集库等),比如接入第三方系统或者平台,等等是非常重要的,笔者最早接触的MFC时就有dll(VC++深入详解)及鸡啄米的MFC环节,后面随着QT的盛行(国产化的推进),QT开始广泛应用,里面也有动态库,就笔者最近的项目为例,这里记录下从库的生成到最后的调用;

    一、生成dll

            1.安装vs开发工具(2017);

            2.新建c++ dll 工程;

            3.实现.h和.cpp,将新建默认的.h和.cpp移除;

            OSCommonDefine.h

    1. #ifndef __BASE_OSCOMMONDEFINE_H__
    2. #define __BASE_OSCOMMONDEFINE_H__
    3. #define AT_DLLEXPORT __declspec(dllexport)
    4. #endif // __BASE_OSCOMMONDEFINE_H__

            CStringUtils.h

    1. //-----------------------------------------------------------------------------
    2. // Copyright (c) 2022, xxx
    3. // All rights reserved.
    4. //
    5. // 摘要: CStringUtils.h 字符串工具类声明
    6. // 当前版本: 1.0
    7. // 作者: Zhang Lei
    8. // 日期:2022.06.28
    9. // 版本说明:类初始版本实现
    10. //-----------------------------------------------------------------------------
    11. #ifndef __BASE_CSTRINGUTILS_H__
    12. #define __BASE_CSTRINGUTILS_H__
    13. #include "OSCommonDefine.h"
    14. #include <string>
    15. #include <vector>
    16. using namespace std;
    17. // CStringUtils类定义
    18. class AT_DLLEXPORT CStringUtils
    19. {
    20. public:
    21. // 字符串转大写
    22. static string& ToUpper(string& strContent);
    23. // 字符串转小写
    24. static string& ToLower(string& strContent);
    25. // 字符串忽略大小写比较
    26. static int CompareNoCase(const string& strContent, const string& strContentCmp);
    27. };
    28. #endif // __BASE_CSTRINGUTILS_H__

     CStringUtils.cpp

    1. //-----------------------------------------------------------------------------
    2. // Copyright (c) 2022, xxx
    3. // All rights reserved.
    4. //
    5. // 摘要: CStringUtils.h 字符串工具类声明
    6. // 当前版本: 1.0
    7. // 作者: Zhang Lei
    8. // 日期:2022.06.28
    9. // 版本说明:类初始版本实现
    10. //-----------------------------------------------------------------------------
    11. #include <string>
    12. #include <algorithm>
    13. #include "../../include/CStringUtils.h"
    14. using namespace std;
    15. //-----------------------------------------------------------------------------
    16. // 功能: 字符串转大写
    17. // 参数:
    18. // strContent: 待转的字符串
    19. // 返回值: 返回转换后字符串的引用对象
    20. // 创建者: Zhang Lei
    21. // 日期:2022.06.28
    22. //-----------------------------------------------------------------------------
    23. string& CStringUtils::ToUpper(string& strContent)
    24. {
    25. transform(strContent.begin(), strContent.end(), strContent.begin(), ::toupper);
    26. return strContent;
    27. }
    28. //-----------------------------------------------------------------------------
    29. // 功能: 字符串转小写
    30. // 参数:
    31. // strContent: 待转的字符串
    32. // 返回值: 返回转换后字符串的引用对象
    33. // 创建者: Zhang Lei
    34. // 日期:2022.06.28
    35. //-----------------------------------------------------------------------------
    36. string& CStringUtils::ToLower(string& strContent)
    37. {
    38. transform(strContent.begin(), strContent.end(), strContent.begin(), ::tolower);
    39. return strContent;
    40. }
    41. //-----------------------------------------------------------------------------
    42. // 功能: 字符串忽略大小写比较
    43. // 参数: strContent 字符串 strContentCmp 比较的字符串
    44. // 返回值: 比较结果
    45. // 创建者: 2022.06.28
    46. // 创建日期: 2022.06.28
    47. //-----------------------------------------------------------------------------
    48. int CStringUtils::CompareNoCase(const string& strContent, const string& strContentCmp)
    49. {
    50. #if defined(_WIN32)
    51. return _stricmp(strContent.c_str(), strContentCmp.c_str());
    52. #elif defined(__linux__)
    53. return strcasecmp(strContent.c_str(), strContentCmp.c_str());
    54. #endif
    55. }

    4.生成dll,编译报错;

     

     去掉预编译头

    成功

    5.说明:

            一般要将.h和.cpp分开,毕竟.h是对外调用的,要和管理;

    二、调用dll

            1.新建测试程序,这里新建一个控制台应用程序;

          2.调用:

    1. #include <iostream>
    2. #include "../../include/CStringUtils.h"
    3. int main()
    4. {
    5. std::string str = "I love China";
    6. std::cout << "Hello World!\n";
    7. std::cout << CStringUtils::ToUpper(str) << std::endl;
    8. std::cout << CStringUtils::ToLower(str) << std::endl;
    9. }

    在工程中设置调用库名和路径: 

     4.成功输出:

  • 相关阅读:
    2023年数维杯国际赛B题赛题解题思路+部分代码
    提到Canvas,必须好好唠唠它的图像操作能力
    使用Barrier共享鼠标键盘,通过macos控制ubuntu系统
    交叉熵损失函数
    Process in Semiconductor(半导体工艺)
    h5钉钉导航栏实时更新(跟踪标题)标题
    acwing 最大数
    Python从入门到入土-基本技能
    (附源码)springboot 病例管理系统 毕业设计 641645
    Android&Flutter混合开发
  • 原文地址:https://blog.csdn.net/qq_27344469/article/details/125530219