本篇内容介绍了“Linux中安装google的libphonenumber c++库方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
进入cpp中可以看到三大linux, mac, win系统的安装说明README, 这里只是记录在Centos6中安装的过程, 如果你选择的是google推荐的ubuntu系统, 照着做就好. 我使用的是Centos6x, 由于当前版本的libphonenumber库已经迁移到c++11了, 而Centos6默认安装的编译器GCC4.4.7并不支持全部的c++11特性, 事实上它只支持C++0x, 这就需要安装scl (一个在centos/redhat系列安装最新版本开发环境的工具, 这里不做介绍), 我使用的是Scl安装的GCC8编译, 以下列出我使用的依赖库及版本:
1. Cmake: 使用系统源安装 2.8
*yum install cmake -y
2. Google test: 1.8.1 (需要C++11)
*wget https://github.com/google/googletest/archive/release-1.8.1.tar.gz
*tar xf release-1.8.1.tar.gz
*cd googletest-release-1.8.1
*mkdir build
*cd build
*cmake -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX= ..
*make && make install
3. RE2: 使用系统源安装
*yum install re2-devel -y
4. protobuffer: 2.6.1
*wget https://github.com/protocolbuffers/protobuf/releases/download/v2.6.1/protobuf-2.6.1.tar.bz2
*tar xf protobuf-2.6.1.tar.bz2
*cd protobuf-2.6.1
*./configure && make && make install
5. ICU: 我选择的是当前最新版本 64.2
*wget https://github.com/unicode-org/icu/releases/download/release-64-2/icu4c-64_2-src.tgz
*tar xf icu4c-64_2-src.tgz
*cd icu/source
*./configure && make && make install
6. Boost: 这个也是源安装
*yum install -y boost-devel boost-system boost-thread
7. 注: 由于make install需要安装到系统, 所以需要你提供管理员权限, 貌似操作yum也需要, 如果使用的普通用户要注意这一点.
完成以上步骤以后, libphonenumber的依赖库都安装完毕, 接下来让我们编译主角吧
git clone https://github.com/google/libphonenumber.git
cd libphonenumber/cpp
mkdir build
cd build
cmake ..
make
./libphonenumber_test
test都成功了那就是ok了, 接下来就是如何使用了
#include <iostream>
#include <string>
using namespace std;
#include "phonenumbers/phonenumber.h"
using i18n::phonenumbers::PhoneNumber;
#include "phonenumbers/phonenumberutil.h"
using i18n::phonenumbers::PhoneNumberUtil;
// phone格式类似于: +86:18612345678
void test_phone_number(const string& phone) {
string region;
string phone_num;
auto idx = phone.find(":");
if (idx != string::npos) {
region = phone.substr(0, idx);
phone_num = phone.substr(idx + 1);
} else {
region = "+86";
phone_num = phone;
}
PhoneNumber pn;
pn.set_country_code(std::stoul(region));
pn.set_national_number(std::stoull(phone_num));
PhoneNumberUtil *util = PhoneNumberUtil::GetInstance();
// region code == ISO Id
std::string reg_code;
util->GetRegionCodeForNumber(pn, ®_code);
std::cout << "region code: " << reg_code << std::endl;
// country code
cout<< "country code: " << util->GetCountryCodeForRegion(reg_code) << endl;
// phone number
std::string name;
util->GetNationalSignificantNumber(pn, &name);
std::cout << "national number: " << name << std::endl;
// validation
std::cout<<"validation: " << std::boolalpha << util->IsValidNumber(pn) << std::endl;
std::cout<<"possibale validation: " << std::boolalpha << util->IsPossibleNumber(pn) << std::endl;
std::cout<<"possibale reason: " << util->IsPossibleNumberWithReason(pn) << std::endl;
std::cout<<"validation for region: " << std::boolalpha << util->IsValidNumberForRegion(pn, reg_code) << std::endl;
}
int main(int argc, char** argv)
{
if (argc != 2) {
cout << "error argc: " << argc << endl;
display_help(argv[0]);
return -1;
}
string phone = argv[1];
test_phone_number(phone);
return 0;
}
// 编译时 指定 libphonenumber.so 所在目录. -L lib -lphonenumber
// 执行前可以指定动态库路径
[hello@world test-phonenumber]$ LD_LIBRARY_PATH=./lib ./test-phonenumber +86:18612345678
执行结果如下:
test stoul: 86
region code: CN
country code: 86
national number: 18612345678
validation: true
possibale validation: true
possibale reason: 0
validation for region: true
注: 如果有问题可以联系我, 因为是截取的代码, 可能有疏漏.
[hello@world test-phonenumber]$ ldd test-phonenumber
linux-vdso.so.1 => (0x00007fff3ad5f000)
libphonenumber.so.8 => not found
libdl.so.2 => /lib64/libdl.so.2 (0x00007fc1acad1000)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007fc1ac7cb000)
libm.so.6 => /lib64/libm.so.6 (0x00007fc1ac547000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fc1ac330000)
libc.so.6 => /lib64/libc.so.6 (0x00007fc1abf9c000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fc1abd7f000)
/lib64/ld-linux-x86-64.so.2 (0x00007fc1acce2000)
[hello@world test-phonenumber]$ ldd ./lib/libphonenumber.so.8.10
linux-vdso.so.1 => (0x00007ffedf7fb000)
libprotobuf.so.9 => /usr/local/lib/libprotobuf.so.9 (0x00007fb7e8d8a000)
libboost_date_time-mt.so.5 => /usr/lib64/libboost_date_time-mt.so.5 (0x00007fb7e8b6c000)
libboost_system-mt.so.5 => /usr/lib64/libboost_system-mt.so.5 (0x00007fb7e8969000)
libboost_thread-mt.so.5 => /usr/lib64/libboost_thread-mt.so.5 (0x00007fb7e8754000)
libicuuc.so.64 => /usr/local/lib/libicuuc.so.64 (0x00007fb7e8384000)
libicui18n.so.64 => /usr/local/lib/libicui18n.so.64 (0x00007fb7e7e9e000)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007fb7e7b98000)
libm.so.6 => /lib64/libm.so.6 (0x00007fb7e7913000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fb7e76fd000)
libc.so.6 => /lib64/libc.so.6 (0x00007fb7e7369000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fb7e714b000)
libz.so.1 => /lib64/libz.so.1 (0x00007fb7e6f35000)
librt.so.1 => /lib64/librt.so.1 (0x00007fb7e6d2d000)
libicudata.so.64 => /usr/local/lib/libicudata.so.64 (0x00007fb7e50ea000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007fb7e4ee6000)
/lib64/ld-linux-x86-64.so.2 (0x00007fb7e93d0000)
使用ldd命令查看依赖库及位置, 依赖的库名: libicuuc.so.64 => 库在系统中的路径 /usr/local/lib/libicuuc.so.64 (0x00007fb7e8384000).
部署的时候只要把所有依赖的so都拷贝下来一起部署, 这样就不用在目标系统安装编译安装一堆库了, 我把libphonenumber依赖的so都列出来, 请按照实际情况 (我都copy到当前目录的lib目录下):
[hello@world test-phonenumber]$ ls -1 lib
libboost_date_time-mt.so
libboost_date_time-mt.so.5
libboost_system-mt.so
libboost_system-mt.so.5
libboost_thread-mt.so
libboost_thread-mt.so.5
libicudata.so.64
libicudata.so.64.2
libicui18n.so.64
libicui18n.so.64.2
libicuuc.so.64
libicuuc.so.64.2
libphonenumber.so
libphonenumber.so.8
libphonenumber.so.8.10
libprotobuf.so
libprotobuf.so.9
libprotobuf.so.9.0.1
注: .so 和 .so.9 这种都是对应的.so.9.0.1的符号链接, 在使用之前指定动态库搜索路径 LD_LIBRARY_PATH=./lib.
“Linux中安装google的libphonenumber c++库方法是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/nemesis/blog/3073902