前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >libphonenumber--windows上编译libphonenumber.lib以及使用(C++、VS2015)

libphonenumber--windows上编译libphonenumber.lib以及使用(C++、VS2015)

作者头像
程序员的酒和故事
发布2018-03-12 17:18:48
2.3K0
发布2018-03-12 17:18:48
举报

再介绍一个谷歌的,自带光环的库–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,往下翻,你会感到很兴奋:

代码语言:javascript
复制
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执行

代码语言:javascript
复制
// 如果要获取动态库:bjam install stage --toolset=msvc-14.0 --stagedir="C:\Boost\boost_vc_140" link=shared runtime-link=shared threading=multi debug release  // 如果是要获取静态库:bjam install stage --toolset=msvc-14.0 --stagedir="C:\Boost\boost_vc_140" link=static runtime-link=static threading=multi debug release 

3 GTest 下载gtest,然后进入文件夹: D:\gtest\googletest\googletest\msvc

打开解决方案,就可以编译了。

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 出现在这个,算是没毛病

代码语言:javascript
复制
Could NOT find PkgConfig (missing:  PKG_CONFIG_EXECUTABLE) 
Configuring done

4 点击 Generate 在build文件夹下就会看到产生的.sln解决方案文件。

编译libphonenumber

打开解决方案,配置一些路径就可以了:

这里需要注意的是,可以由于各种原因,不能所有的build成功,主要确保phonenumber这个工程编译成功即可。

使用libphonenumber

终于搞定了,我们现在就使用一下这个牛逼的库吧。

1 新建一个win32控制台应用程序

2 加入头文件,包含目录

3 添加引用库

4 编写代码

代码语言:javascript
复制
#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;
}

输入结果:

代码语言:javascript
复制
trueCN1347880831186

注意: 在生成库的时候,和我们编写自己的工程的时候,一定要选择一致的代码生成方式!!!!!!

结束: 大概就这样了,其实自己走的弯路、遇到的错误,比上面所写的多的多,但是很多都忘记了。 如果你遇到什么错误,可以给我留言,帮你解决,谢谢。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-01-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 人生有味是多巴胺 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • libphonenumber简介
  • 准备工作
  • 生成libphonenumber解决方案
  • 编译libphonenumber
  • 使用libphonenumber
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档