libphonenumber--windows上编译libphonenumber.lib以及使用(C++、VS2015)
再介绍一个谷歌的,自带光环的库–libphonenumber.
虽然这个库是用c++写的,但是我们在网络上很少见到在C++开发中使用这个库,或是说在windows开发中使用这个库。也就是几乎很难找到如何编译libphonenumber.lib 和 libphonenumber.dll.
所以,今天就要与大家分享一下,如何编译libphonenumber.lib,以及简单的使用。
libphonenumber简介
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地址: https://github.com/googlei18n/libphonenumber
准备工作
我们先进入cpp: https://github.com/googlei18n/libphonenumber/tree/master/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):
http://cmake.org/cmake/resources/software.html
* Download and install the Win32 installer.
- Boost (tested with v1.44) from BoostPro:
http://www.boostpro.com/download/
* Select all the variants and Boost DateTime and Boost Thread during the
installation process.
See Linux instructions for information about thread-safety.
- GTest (tested with v1.6.0):
http://code.google.com/p/googletest/downloads/list
* Open msvc/gtest-md.sln with Visual Studio and build the whole solution.
- ICU (MSVC binaries, tested with v4.8.1):
http://site.icu-project.org/download/48#ICU4C-Download
* Simply extract the archive.
- Protocol Buffers:
http://code.google.com/p/protobuf/downloads/list
* Open vsprojects/protobuf.sln with Visual Studio and build the whole
solution.
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.lib
ICU_I18N_INCLUDE_DIR C:/icu/include
ICU_I18N_LIB C:/icu/lib/icuin.lib
ICU_UC_INCLUDE_DIR C:/icu/include
ICU_UC_LIB C:/icu/lib/icuuc.lib
PROTOBUF_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/Release/protoc.exe
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 a
CMake user interface (ccmake or cmake-gui).
USE_ALTERNATE_FORMATS = ON | OFF [ON] -- Use alternate formats for the phone
number matcher.
USE_BOOST = ON | OFF [ON] -- Use Boost. This is only needed in
multi-threaded environments that
are not Linux and Mac.
Libphonenumber relies on Boost for
non-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 that
doesn'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 https://cmake.org/
2 Boost 2.1下载 http://www.boost.org/
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需要V**访问外国网站,你懂的。 下载地址: http://download.icu-project.org/files/icu4c/4.8.1.1/
这里注意,尽管你的windows电脑是64位,但是你开发的往往是32位程序,所以需要下载win23:
解压到C盘即可。
5 Protocol Buffers 这个就不用再说了,上一篇博客就是关于《google/protobuf–VS2015编译、使用》
准备工作齐活了,开始干大事。
生成libphonenumber解决方案
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解决方案文件。
编译libphonenumber
打开解决方案,配置一些路径就可以了:
这里需要注意的是,可以由于各种原因,不能所有的build成功,主要确保phonenumber这个工程编译成功即可。
使用libphonenumber
终于搞定了,我们现在就使用一下这个牛逼的库吧。
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;
}
输入结果:
trueCN1347880831186
注意: 在生成库的时候,和我们编写自己的工程的时候,一定要选择一致的代码生成方式!!!!!!
结束: 大概就这样了,其实自己走的弯路、遇到的错误,比上面所写的多的多,但是很多都忘记了。 如果你遇到什么错误,可以给我留言,帮你解决,谢谢。
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法