目录
  • 一、生成dll
  • 二、调用dll

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

一、生成dll

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

2.新建c++ dll 工程;

c++动态库调用的实现

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,编译报错;

c++动态库调用的实现

 去掉预编译头

c++动态库调用的实现

成功

c++动态库调用的实现

5.说明:

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

二、调用dll

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

c++动态库调用的实现

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;
}

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

c++动态库调用的实现

c++动态库调用的实现

 4.成功输出:

c++动态库调用的实现

 到此这篇关于c++动态库调用的实现的文章就介绍到这了,更多相关c++动态库调用内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。