c++动态库调用

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

一、生成dll

        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是对外调用的,要和管理;

二、调用dll

        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.成功输出:

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值