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

3.实现.h和.cpp,将新建默认的.h和.cpp移除;
OSCommonDefine.h
- #ifndef __BASE_OSCOMMONDEFINE_H__
- #define __BASE_OSCOMMONDEFINE_H__
-
- #define AT_DLLEXPORT __declspec(dllexport)
-
- #endif // __BASE_OSCOMMONDEFINE_H__
CStringUtils.h
- //-----------------------------------------------------------------------------
- // Copyright (c) 2022, xxx
- // All rights reserved.
- //
- // 摘要: CStringUtils.h 字符串工具类声明
- // 当前版本: 1.0
- // 作者: Zhang Lei
- // 日期:2022.06.28
- // 版本说明:类初始版本实现
- //-----------------------------------------------------------------------------
-
- #ifndef __BASE_CSTRINGUTILS_H__
- #define __BASE_CSTRINGUTILS_H__
-
- #include "OSCommonDefine.h"
- #include <string>
- #include <vector>
- using namespace std;
-
- // CStringUtils类定义
- class AT_DLLEXPORT CStringUtils
- {
- public:
- // 字符串转大写
- static string& ToUpper(string& strContent);
- // 字符串转小写
- static string& ToLower(string& strContent);
- // 字符串忽略大小写比较
- static int CompareNoCase(const string& strContent, const string& strContentCmp);
- };
-
- #endif // __BASE_CSTRINGUTILS_H__
CStringUtils.cpp
- //-----------------------------------------------------------------------------
- // Copyright (c) 2022, xxx
- // All rights reserved.
- //
- // 摘要: CStringUtils.h 字符串工具类声明
- // 当前版本: 1.0
- // 作者: Zhang Lei
- // 日期:2022.06.28
- // 版本说明:类初始版本实现
- //-----------------------------------------------------------------------------
-
- #include <string>
- #include <algorithm>
- #include "../../include/CStringUtils.h"
- using namespace std;
-
- //-----------------------------------------------------------------------------
- // 功能: 字符串转大写
- // 参数:
- // strContent: 待转的字符串
- // 返回值: 返回转换后字符串的引用对象
- // 创建者: Zhang Lei
- // 日期:2022.06.28
- //-----------------------------------------------------------------------------
- string& CStringUtils::ToUpper(string& strContent)
- {
- transform(strContent.begin(), strContent.end(), strContent.begin(), ::toupper);
- return strContent;
- }
-
- //-----------------------------------------------------------------------------
- // 功能: 字符串转小写
- // 参数:
- // strContent: 待转的字符串
- // 返回值: 返回转换后字符串的引用对象
- // 创建者: Zhang Lei
- // 日期:2022.06.28
- //-----------------------------------------------------------------------------
- string& CStringUtils::ToLower(string& strContent)
- {
- transform(strContent.begin(), strContent.end(), strContent.begin(), ::tolower);
- return strContent;
- }
-
- //-----------------------------------------------------------------------------
- // 功能: 字符串忽略大小写比较
- // 参数: strContent 字符串 strContentCmp 比较的字符串
- // 返回值: 比较结果
- // 创建者: 2022.06.28
- // 创建日期: 2022.06.28
- //-----------------------------------------------------------------------------
- int CStringUtils::CompareNoCase(const string& strContent, const string& strContentCmp)
- {
- #if defined(_WIN32)
- return _stricmp(strContent.c_str(), strContentCmp.c_str());
- #elif defined(__linux__)
- return strcasecmp(strContent.c_str(), strContentCmp.c_str());
- #endif
- }
4.生成dll,编译报错;

去掉预编译头

成功

5.说明:
一般要将.h和.cpp分开,毕竟.h是对外调用的,要和管理;
1.新建测试程序,这里新建一个控制台应用程序;

2.调用:
- #include <iostream>
- #include "../../include/CStringUtils.h"
-
- int main()
- {
- std::string str = "I love China";
-
- std::cout << "Hello World!\n";
- std::cout << CStringUtils::ToUpper(str) << std::endl;
- std::cout << CStringUtils::ToLower(str) << std::endl;
- }
在工程中设置调用库名和路径: 

4.成功输出:
