一、下載和編譯Poco庫
參考:www.jianshu.com/p/94c27101eee3
blog.csdn.net/walk_and_think/article/details/82432863
- 下載地址:https://pocoproject.org/releases/poco-1.9.0/
或者 https://github.com/pocoproject/poco/releases/tag/poco-1.9.0-release - 下載
poco-1.9.0-all.zip
,其中不帶all的是基礎版本,只包含基本的poco類庫,帶all的是全功能版本,包含sqlite,opensll等等。 - 解壓縮zip包后,得到文件。
編譯
---- Windows平台下
1、用記事本或UltraEdit打開components文件,添加或刪除指定的行即可配置編譯哪些模塊。不想要openssl和mysql, 刪掉openssl和data/mysql即可
例如:
CppUnit
CppUnit/WinTestRunner
Foundation
Encodings
XML
JSON
Util
Net
Zip
=> 只編譯基本模塊
2、根據Visual Studio版本,在文件夾中直接雙擊build_vsxxx.cmd文件即可自動編譯。
VS2008: build_vs90.cmd
VS2013: build_vs120.cmd
VS2015: build_vs140.cmd
VS2017: build_vs150.cmd
3、本人使用的VS2013因此,雙擊運行了文件夾中的build_vs120.cmd。之后會自動執行編譯腳本,並在當前目錄下生成兩個文件夾bin和lib。
lib:該文件夾中存放了.lib靜態連接庫等文件。編譯自己寫的源代碼時需要。
bin:該文件夾中存放了.dll動態鏈接庫等文件。運行編譯好的可執行文件時需要。
編譯:參考 https://www.cnblogs.com/fuland/p/3768705.html
下面簡單介紹一下方法,以 vs2010 為例。
第一種方法:
(1) Poco 根目錄下有build_vs100.cmd和buildwin.cmd這兩個批處理文件, 我們得修改一下它們。
把build_vs100.cmd 修改為以下內容:
@echo off
if defined VS100COMNTOOLS (
call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64\vsvars64.bat")
buildwin 100 build all both x64 samples紅色的部分是vsvars64.bat的路徑,具體要看本機的安裝位置。這里因為是要編譯X64,所以要修改環境。
藍色部分是執行編譯時候的參數(注意X64的X要小寫)。參數的具體說明為:
buildwin VS_VERSION [ACTION] [LINKMODE] [CONFIGURATION] [PLATFORM] [SAMPLES] [TESTS] [TOOL]
VS_VERSION: 71|80|90|100|110
ACTION: build|rebuild|clean
LINKMODE: static_mt|static_md|shared|all
CONFIGURATION: release|debug|both
PLATFORM: Win32|x64|WinCE
SAMPLES: samples|nosamples
TESTS: tests|notests
TOOL: devenv|vcexpress|msbuild具體的參數選擇根據需求。
如果不需要 NetSSL_OpenSSL 和 MySQL ,此時保存后就雙擊它就行了。 它會自動編譯好各種庫。
如果要NetSSL_OpenSSL 這個庫則打開buildwin.cmd文件:
設置 openssl 的路徑,如:set OPENSSL_DIR=c:\OpenSSL-Win64
set OPENSSL_INCLUDE=%OPENSSL_DIR%\include
set OPENSSL_LIB=%OPENSSL_DIR%\lib;%OPENSSL_DIR%\lib\VC
set INCLUDE=%INCLUDE%;%OPENSSL_INCLUDE%
set LIB=%LIB%;%OPENSSL_LIB%
注意,如果要 Poco 中的 MySQL 庫 還得安裝. Mysql
編輯好后,雙擊 build_vs100.cmd執行編譯。完成后會在Poco根目錄下的lib64中看到編譯好的庫。在bin64中有編譯好的dll。第二種方法(適合熟悉命令行的用戶):
按需求修改完buildwin.cmd文件后,使用vs2010的Tools中“Visual Studio x64 Win64 命令提示(2010)”命令,進入命令行狀態。轉到Poco所在的根目錄,直接輸入命令:
buildwin 100 build all both x64 samples
最后需要注意的地方:
上述方法完成編譯后,關於Data的MySQL、ODBC、SQLite三個庫沒有生成(原因不詳),需要單獨編譯,在此以MySQL為例記錄一下編譯過程。在\Data\MySQL文件夾下,找到MySQL_vs100.sln文件,打開。
打開的工程是win32的,需要改為X64。在C++包含目錄中添加本項目的include文件夾,例如:“C:\poco-1.5.1-all\Data\MySQL\include;”,還要添加安裝的MySQL的include文件夾,例如“C:\Program Files\MySQL\MySQL Server 5.6\include”,在庫目錄中添加所需庫的路徑,例如“C:\Program Files\MySQL\MySQL Server 5.6\lib”。
注意:編譯MySQL的Poco庫,必須先安裝MySQL。
設置好環境之后,就可以選擇相應的配置(debug or release)進行編譯。
編譯完成后,生成的庫在Poco根目錄下的lib文件夾,如果是編譯的動態庫,則動態庫在Poco根目錄下的bin文件夾。
VS2013下使用Poco庫
在Poco1.9.0目錄下新建文件夾 include ,把所有的工程下的include的內容合在一起。
設置vs2013的項目屬性》C/C++》附加包含目錄:D:\poco-1.9.0-release\include (你自己的Poco目錄\inlcude )
鏈接器》附加庫目錄》D:\poco-1.9.0-release\lib (你自己的Poco目錄\lib )
調試》環境:PATH=D:\poco-1.9.0-release\bin; (你自己的Poco目錄\bin; )
以下參考官網文檔和例子:https://pocoproject.org/slides/
#include <vector> #include "Poco/String.h" #include "Poco/Format.h" using Poco::cat; using Poco::format; int main(int argc, char** argv) { try{ std::vector<std::string> colors; colors.push_back("red"); colors.push_back("green"); colors.push_back("blue"); std::string str; str = cat(std::string(", "), colors.begin(), colors.end()); // "red, green, blue" int n = 42; std::string s; format(s, "The answer to life, the universe and everything is %d", n); s = format("%d + %d = %d", 2, 2, 4); // "2 + 2 = 4" s = format("%4d", 42); // " 42" s = format("%-4d", 42); // "42 " format(s, "%d", std::string("foo")); // "[ERRFMT]" } catch (Poco::Exception& e) { std::string s = e.what(); s += " " + e.message(); } return 0; }
Poco中提供了對 std::string 和 std::wstring 的操作函數。https://pocoproject.org/slides/040-StringsAndFormatting.pdf
例如: 刪除空白符(trimming ) 比較字符串 連接字符串 格式化字符串 字符串大小寫轉換 字符或字符串替換
std::[w]string trimLeft(const std::[w]string& str) // 刪除字符串開頭的空白符,返回修剪后的string,原字符串不變 std::[w]string& trimLeftInPlace(std::[w]string& str)// 刪除字符串開頭的空白符,直接修改原字符串 std::[w]string trimRight(const std::[w]string& str) //刪除結尾的空白符,返回修剪后的結果,原字符串不變 std::[w]string& trimRightInPlace(std::[w]string& str)//刪除結尾的空白符,直接在原字符串上 std::[w]string trim(const std::[w]string& str)//刪除開頭和結尾的空白符 std::[w]string& trimInPlace(std::[w]string& str) //直接在原字符串上刪除開頭和結尾的空白符 //帶 -InPlace 的都是在原字符串上直接修改。 //returns a copy of str with all characters converted to upper-/lowercase. std::[w]string toUpper(const std::[w]string& str) std::[w]string toLower(const std::[w]string& str) //直接在原字符串上轉換大小寫 std::[w]string& toUpperInPlace(std::[w]string& str) std::[w]string& toLowerInPlace(std::[w]string& str) Warning: These function do not work with UTF-8 strings. See Poco::UTF8 for an UTF-8 capable replacement. //Case-insensitive Comparison 不區分大小寫的比較 int icompare(const std::[w]string& str1, const std::[w]string& str2) //compares tr1 to str2, and returns 0 if str1 == str2 -1 if str1 < str2 +1 if str1 > str2 //returns a copy of str with all characters in from replaced with the corresponding (by position) characters in to.If there is no corresponding character in to, the character is removed. std::[w]string translate(const std::[w]string& str, const std::[w]string& from, const std::[w]string& to) std::[w]string& translateInPlace(std::[w]string& str, const std::[w]string& from, const std::[w]string& to) //from and to can also be old-style C strings. //returns a copy of str with all occurences of the substring given in from replaced with the string given in to. std::[w]string replace(const std::[w]string& str,const std::[w]string& from, const std::[w]string& to) std::[w]string& replaceInPlace(std::[w]string& str,const std::[w]string& from, const std::[w]string& to) //from and to can also be old-style C strings. //String Concatenation std::[w]string cat(const std::[w]string& s1, onst std::[w]string& s2 [, const std::[w]string& s3 [,...]]) //concatenates up to six strings and returns the result template <class S, class It>S cat(const S& delimiter, const It& begin, const It& end) //concatenates all string in the range [It, end), delimited by delimiter. cat() is more efficient than operator + of std::string //Typesafe Printf-style Formatting std::string format(const std::string& format, const Any& value1[, const Any& value2[, ...]]) void format(std::string& result, const std::string& format, const Any& value1[, const Any& value2[, ...]]) //Up to six parameters are supported.
例子:
#include <vector> #include <Poco/String.h> #include <Poco/format.h> //for Poco::format int main(int argc, char** argv) { std::string hello(" \n \t Hello, world! "); std::string s1(Poco::trimLeft(hello)); // "Hello, world! " Poco::trimRightInPlace(s1); // "Hello, world!" std::string s2 = Poco::trim(hello); // "Hello, world!" Poco::trimInPlace(hello); // "Hello, world!" s1 = Poco::toUpper(hello); // "HELLO, WORLD!" Poco::toLowerInPlace(s2); // "hello, world!" //不分大小寫的比較 int nRet = Poco::icompare(s1, s2);// 0 nRet = Poco::icompare(hello, "Hello, Universe!");// 1 nRet = strcmpi(s1.c_str(), s2.c_str());// 0 //分大小寫的比較 nRet = strcmp(s1.c_str(), s2.c_str());// -1 std::string s("Eiffel Tower"); //E->3 e->3 l->1 o->0 替換字符 Poco::translateInPlace(s, "Eelo", "3310"); // "3iff31 T0w3r" //替換字符串 s = "aabbcc"; std::string r(Poco::replace(s, "aab", "AA")); // "AAbcc" r = Poco::replace(s, "bb", "xxx"); // "aaxxxcc" r = Poco::replace(s, "bbcc", ""); // "aa" r = Poco::replace(s, "aaa", "b"); // "aabbcc" 沒有匹配到,不變 //字符串連接 std::vector<std::string> colors; colors.push_back("red"); colors.push_back("green"); colors.push_back("blue"); std::string st = Poco::cat(colors[0], std::string(" "), colors[1], std::string(" "), colors[2]);// "red green blue" st = Poco::cat(std::string(", "), colors.begin(), colors.end());// "red, green, blue" //字符串格式化 int n = 42; std::string str; Poco::format(str, "The answer to life, the universe and everything is %d", n); str = Poco::format("%d + %d = %d", 2, 2, 4); // "2 + 2 = 4" str = Poco::format("%4d", 42); // " 42" str = Poco::format("%-4d", 42); // "42 " Poco::format(str, "%d", std::string("foo")); // "[ERRFMT]" return 0; }