再介绍一个谷歌的,自带光环的库–libphonenumber.
虽然这个库是用c++写的,但是我们在网络上很少见到在C++开发中使用这个库,或是说在windows开发中使用这个库。也就是几乎很难找到如何编译libphonenumber.lib 和 libphonenumber.dll.
所以,今天就要与大家分享一下,如何编译libphonenumber.lib,以及简单的使用。
phonenumber是用于解析、格式化、存储和校验电话号码的Java、C++或 JavaScript类库。
Google’s common Java, C++ and JavaScript library for parsing, formatting, and validating international phone numbers. The Java version is optimized for running on smartphones, and is used by the Android framework since 4.0 (Ice Cream Sandwich).
github地址:
我们先进入cpp:
看看这里的README,往下翻,你会感到很兴奋:
Building the library on Windows (Visual Studio)
-----------------------------------------------
The library was tested with Visual Studio 2010.You will need to manually fetch and install the following dependencies:- CMake (tested with v2.8.6):.html* Download and install the Win32 installer.- Boost (tested with v1.44) from BoostPro:/* Select all the variants and Boost DateTime and Boost Thread during theinstallation process.See Linux instructions for information about thread-safety.- GTest (tested with v1.6.0):* Open msvc/gtest-md.sln with Visual Studio and build the whole solution.- ICU (MSVC binaries, tested with v4.8.1):* Simply extract the archive.- Protocol Buffers:* Open vsprojects/protobuf.sln with Visual Studio and build the wholesolution.Then run cmake-gui and specify the path to the libphonenumber's cpp directory
and its build directory which must be created (e.g. cpp/build).When clicking on "Configure", specify the appropriate Visual Studio version
(tested with 2010).Then CMake will need your help to locate the dependencies. You will have to set
the following variables (this example assumes that you extracted the
dependencies to C:/).GTEST_INCLUDE_DIR C:/gtest-1.6.0/include
GTEST_LIB C:/gtest-1.6.0/msvc/gtest-md/Release/gtest.libICU_I18N_INCLUDE_DIR C:/icu/include
ICU_I18N_LIB C:/icu/lib/icuin.libICU_UC_INCLUDE_DIR C:/icu/include
ICU_UC_LIB C:/icu/lib/icuuc.libPROTOBUF_INCLUDE_DIR C:/protobuf-2.4.1/src
PROTOBUF_LIB C:/protobuf-2.4.1/vsprojects/Release/libprotobuf.lib
PROTOC_BIN C:/protobuf-2.4.1/vsprojects/Then you can click on "Configure" again. CMake should have located all the
dependencies.
Then click on "Generate" to generate the appropriate Visual Studio project.
Then open cpp/build/libphonenumber.sln with Visual Studio and build the INSTALL
target.As a result the library's headers and binaries should have been installed to
C:/Program Files/libphonenumber/.
Note that this path can be set by overriding the CMAKE_INSTALL_PREFIX variable
with cmake-gui.Supported build parameters
--------------------------Build parameters can be specified invoking CMake with '-DKEY=VALUE' or using aCMake user interface (ccmake or cmake-gui).USE_ALTERNATE_FORMATS = ON | OFF [ON] -- Use alternate formats for the phonenumber matcher.USE_BOOST = ON | OFF [ON] -- Use Boost. This is only needed inmulti-threaded environments thatare not Linux and Mac.Libphonenumber relies on Boost fornon-POSIX (e.g. Windows)multi-threading.USE_ICU_REGEXP = ON | OFF [ON] -- Use ICU regexp engine.USE_LITE_METADATA = ON | OFF [OFF] -- Generates smaller metadata thatdoesn't include example numbers.USE_RE2 = ON | OFF [OFF] -- Use RE2.USE_STD_MAP = ON | OFF [OFF] -- Force the use of std::map.
1 cmake
对于cmake不太了解的同志,就跟我一样好了,下载一个cmake-gui
/
2 Boost
2.1下载
/
2.2解压
解压到C盘boost_1_62_0
2.3执行
执行bootstrap.bat
2.4执行
// 如果要获取动态库:
bjam install stage --toolset=msvc-14.0 --stagedir="C:Boostboost_vc_140" link=shared runtime-link=shared threading=multi debug release
// 如果是要获取静态库:
bjam install stage --toolset=msvc-14.0 --stagedir="C:Boostboost_vc_140" link=static runtime-link=static threading=multi debug release
3 GTest
下载gtest,然后进入文件夹:
D:gtestgoogletestgoogletestmsvc
打开解决方案,就可以编译了。
4 ICU
这里的icu不是重症监护室,但是下载icu需要vpn翻墙,你懂的。
下载地址:
.8.1.1/
这里注意,尽管你的windows电脑是64位,但是你开发的往往是32位程序,所以需要下载win23:
解压到C盘即可。
5 Protocol Buffers
这个就不用再说了,上一篇博客就是关于《google/protobuf–VS2015编译、使用》
准备工作齐活了,开始干大事。
1 在cpp文件夹在新建一个文件夹,叫build。
2 打开cmake-gui,选择cpp文件夹作为source code, 选择build文件夹作为目标文件夹,然后添加一些变量,指向自己的路径即可:
这里需要注意的是,如果boost的错误太多,可以暂时把USE_BOOST去掉。
3 Configure
出现在这个,算是没毛病
Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE)
Configuring done
4 点击 Generate
在build文件夹下就会看到产生的.sln解决方案文件。
打开解决方案,配置一些路径就可以了:
这里需要注意的是,可以由于各种原因,不能所有的build成功,主要确保phonenumber这个工程编译成功即可。
终于搞定了,我们现在就使用一下这个牛逼的库吧。
1 新建一个win32控制台应用程序
2 加入头文件,包含目录
3 添加引用库
4 编写代码
#include<iostream>
#include<string>
#include "phonenumber.h"
#include "phonenumberutil.h"using i18n::phonenumbers::PhoneNumberUtil;
using i18n::phonenumbers::PhoneNumber;int main()
{PhoneNumberUtil *phoneUtil = PhoneNumberUtil::GetInstance();//初始化一个电话号码PhoneNumber *pn = new PhoneNumber();pn->set_country_code(86);pn->set_national_number(13478808311);//判断电话号码是否有效std::cout << std::boolalpha << phoneUtil->IsValidNumber(*pn) << std::endl;//判断电话号码所在的地区std::string *region = new std::string();phoneUtil->GetRegionCodeForNumber(*pn, region);std::cout << *region << std::endl;std::string* name = new std::string();phoneUtil->GetNationalSignificantNumber(*pn, name);std::cout << *name << std::endl;//获取某个国家的国字区号PhoneNumber *example = new PhoneNumber();phoneUtil->GetInvalidExampleNumber("CN", example);std::cout << example->country_code() << std::endl;//释放内存delete pn;pn = nullptr;delete region;region = nullptr;delete name;name = nullptr;delete example;example = nullptr;system("pause");return 0;
}
输入结果:
true
CN
13478808311
86
注意:
在生成库的时候,和我们编写自己的工程的时候,一定要选择一致的代码生成方式!!!!!!
结束:
大概就这样了,其实自己走的弯路、遇到的错误,比上面所写的多的多,但是很多都忘记了。
如果你遇到什么错误,可以给我留言,帮你解决,谢谢。
本文发布于:2024-01-28 17:54:49,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17064356949206.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |