c++動態(tài)庫調用的實現(xiàn)
在平時的開發(fā)中某些情況,動態(tài)庫和靜態(tài)庫是程序開發(fā)的不二法門,例如封裝一個庫,供別人調用(日志庫、字符串處理庫、設備信息采集庫等),比如接入第三方系統(tǒng)或者平臺,等等是非常重要的,筆者最早接觸的MFC時就有dll(VC++深入詳解)及雞啄米的MFC環(huán)節(jié),后面隨著QT的盛行(國產(chǎn)化的推進),QT開始廣泛應用,里面也有動態(tài)庫,就筆者最近的項目為例,這里記錄下從庫的生成到最后的調用;
一、生成dll
1.安裝vs開發(fā)工具(2017);
2.新建c++ dll 工程;
3.實現(xiàn).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 // 版本說明:類初始版本實現(xiàn) //----------------------------------------------------------------------------- #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 // 版本說明:類初始版本實現(xiàn) //----------------------------------------------------------------------------- #include <string> #include <algorithm> #include "../../include/CStringUtils.h" using namespace std; //----------------------------------------------------------------------------- // 功能: 字符串轉大寫 // 參數(shù): // strContent: 待轉的字符串 // 返回值: 返回轉換后字符串的引用對象 // 創(chuàng)建者: Zhang Lei // 日期:2022.06.28 //----------------------------------------------------------------------------- string& CStringUtils::ToUpper(string& strContent) { transform(strContent.begin(), strContent.end(), strContent.begin(), ::toupper); return strContent; } //----------------------------------------------------------------------------- // 功能: 字符串轉小寫 // 參數(shù): // strContent: 待轉的字符串 // 返回值: 返回轉換后字符串的引用對象 // 創(chuàng)建者: Zhang Lei // 日期:2022.06.28 //----------------------------------------------------------------------------- string& CStringUtils::ToLower(string& strContent) { transform(strContent.begin(), strContent.end(), strContent.begin(), ::tolower); return strContent; } //----------------------------------------------------------------------------- // 功能: 字符串忽略大小寫比較 // 參數(shù): strContent 字符串 strContentCmp 比較的字符串 // 返回值: 比較結果 // 創(chuàng)建者: 2022.06.28 // 創(chuàng)建日期: 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.成功輸出:
到此這篇關于c++動態(tài)庫調用的實現(xiàn)的文章就介紹到這了,更多相關c++動態(tài)庫調用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!