这个博主的操作对我有效,这个写的好
apt-get install autoconf automake libtool curl make g++ unzip(成功)
git clone https://github.com/protocolbuffers/protobuf.git(成功)
cd protobuf (成功)
git submodule update --init --recursive(成功)
make (success)
sudo make install (success)
sudo ldconfig (success)
直接输入下面的命令,直接安装成功了,暂时先用这个libprotoc 3.0.0吧
apt install protobuf-compiler
protoc --version
Failed to connect to github.com port 443: 拒绝连接
在hosts文件(window端的路径在:C:\Windows\System32\drivers\etc\hosts)的末尾添加:
199.232.69.194 gitub.global.ssl.fastly.net
140.82.114.3 github.com
185.199.108.153 assets-cdn.github.com
185.199.109.153 assets-cdn.github.com
185.199.110.153 assets-cdn.github.com
185.199.111.153 assets-cdn.github.com
root@wxncom-virtual-machine:/home/wxncom/shared_bike/demo/protobuf/shared_bike_demo# g++ -std=c++11 example.cc bike.pb.cc -lprotobuf
In file included from example.cc:1:0:
bike.pb.h:9:10: fatal error: google/protobuf/stubs/common.h: 没有那个文件或目录
#include
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
In file included from bike.pb.cc:5:0:
bike.pb.h:9:10: fatal error: google/protobuf/stubs/common.h: 没有那个文件或目录
#include
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
root@wxncom-virtual-machine:/home/wxncom/shared_bike/demo/protobuf/shared_bike_demo# ls
bike.pb.cc bike.pb.h bike.proto example.cc
root@wxncom-virtual-machine:/home/wxncom/shared_bike/demo/protobuf/shared_bike_demo# sudo apt-get install libprotobuf-dev protobuf-compiler
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
protobuf-compiler 已经是最新版 (3.0.0-9.1ubuntu1.1)。
下列【新】软件包将被安装:
libprotobuf-dev libprotobuf-lite10
升级了 0 个软件包,新安装了 2 个软件包,要卸载 0 个软件包,有 46 个软件包未被升级。
需要下载 1,059 kB 的归档。
解压缩后会消耗 8,508 kB 的额外空间。
获取:1 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libprotobuf-lite10 amd64 3.0.0-9.1ubuntu1.1 [98.0 kB]
获取:2 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libprotobuf-dev amd64 3.0.0-9.1ubuntu1.1 [961 kB]
已下载 1,059 kB,耗时 3秒 (367 kB/s)
正在选中未选择的软件包 libprotobuf-lite10:amd64。
(正在读取数据库 ... 系统当前共安装有 181625 个文件和目录。)
正准备解包 .../libprotobuf-lite10_3.0.0-9.1ubuntu1.1_amd64.deb ...
正在解包 libprotobuf-lite10:amd64 (3.0.0-9.1ubuntu1.1) ...
正在选中未选择的软件包 libprotobuf-dev:amd64。
正准备解包 .../libprotobuf-dev_3.0.0-9.1ubuntu1.1_amd64.deb ...
正在解包 libprotobuf-dev:amd64 (3.0.0-9.1ubuntu1.1) ...
正在设置 libprotobuf-lite10:amd64 (3.0.0-9.1ubuntu1.1) ...
正在设置 libprotobuf-dev:amd64 (3.0.0-9.1ubuntu1.1) ...
正在处理用于 libc-bin (2.27-3ubuntu1.5) 的触发器 ...
root@wxncom-virtual-machine:/home/wxncom/shared_bike/demo/protobuf/shared_bike_demo# g++ -std=c++11 example.cc bike.pb.cc -lprotobuf
感谢这位老哥的博客,成功解决问题!
sudo apt-get install libprotobuf-dev protobuf-compiler
第一步:
sudo apt-get install autoconf automake libtool curl make g++ unzip
第二步:(第二步不可能不成功,如果你第二步执行出错,说明你的网络有问题,搞一个好点的热点就可以了)
sudo git clone https://github.com/google/protobuf.git
cd protobuf
sudo apt install libtool-bin
sudo git submodule update --init --recursive
第三步:
先执行 1),如果提示:sudo: ./autogen.sh:找不到命令,则执行2)的5条语句
#1) sudo ./autogen.sh
#2)5条语句
sudo ./update_subtrees.sh
sudo ./regenerate_stale_files.sh
sudo ./google3_export_generated_files.sh
sudo ./fix_permissions.sh
sudo ./generate_descriptor_proto.sh
sudo ./configure # (如果执行失败,先不要管)
sudo make#(如果提示"make: *** 没有指明目标并且找不到 makefile。 停止。" , 则先执行 cmake . 再执行make)
不要怕,兵来将挡水来土掩
locate libabsl.so
确实系统没有这个库
那就下载这个库:
sudo apt-get install apt-file
apt-file update
apt-file search libabsl.so # labsl
此时,你再去查
locate libabsl.so
哦!Mygod!还是没有!
再次尝试!这次我问了gpt3.5!
sudo apt-get install build-essential cmake git
git clone https://github.com/abseil/abseil-cpp.git
遇到问题:
fatal: unable to access 'https://github.com/abseil/abseil-cpp.git/': Failed to connect to github.com port 443: 拒绝连接
git clone https://github.com/abseil/abseil-cpp.git(多尝试了几次,不知道怎么就成功了)
cd abseil-cpp
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=/usr/local ..
make
sudo make install
cd ..
cd ..
我执行了一下make,还是报错
然后我又执行cmake .发现有几个错误:
CMake Error: install(EXPORT "abslTargets" ...) includes target "hash_testing" which requires target "gmock" that is not in the export set.
CMake Error: install(EXPORT "abslTargets" ...) includes target "scoped_mock_log" which requires target "gmock" that is not in the export set.
CMake Error: install(EXPORT "abslTargets" ...) includes target "scoped_mock_log" which requires target "gmock" that is not in the export set.
CMake Error: install(EXPORT "abslTargets" ...) includes target "random_mocking_bit_gen" which requires target "gmock" that is not in the export set.
CMake Error: install(EXPORT "abslTargets" ...) includes target "random_mocking_bit_gen" which requires target "gmock" that is not in the export set.
我又重新cmake 了几次,当我再次make的时候,居然成功了!
如果你们还没有成功,可以去搜一下Abseil的安装(Abseil C++库通常被简称为“absl”。)
第四步:
sudo make check(执行报错,不管他)
sudo sudo make install
sudo ldconfig
第五步:
protoc --version
git clone https://github.com/abseil/abseil-cpp
cd abseil-cpp
mkdir bulid #用于构建
mkdir install #用于安装
cd bulid/
cmake .. -DCMAKE_INSTALL_PREFIX=/opt/abseil-cpp/install/
make && make install
cp -R /opt/abseil-cpp/install/include/absl/ /usr/include/
find ./ -name "*.o" | xargs ar cr libabsl.a
cp libabsl.a /usr/lib
又遇到了问题,说我至少要用c++14的版本,这个ok,简单处理.
但是他又说我的protobuf的3.0.0版本太高了
使用这个博客里面的版本
wget https://github.com/google/protobuf/archive/v2.6.1.zip
sudo apt-get install fastjar
jar xvf ./v2.6.1.zip
unzip protobuf-2.6.1.zip #这里我下载得到的文件是v2.6.1,所以改为unzip v2.6.1
cd protobuf-2.6.1
chmod 777 ./autogen.sh
./autogen.sh
- bike.proto
syntax = "proto2"; //协议版本
package tutorial;//包,我理解为命名空间
message mobile_request//短信请求
{
required string mobile = 1;//一定要写一个编号,有多个就1,2,3
}
message mobile_response//短信回应
{
required int32 code = 1; //响应代号
required int32 icode = 2; //验证码
optional string data = 3; //失败原因
}
message login_request
{
required string mobile = 1; // 手机号码
required int32 icode = 2; // 验证码
}
message login_response
{
required int32 code = 1; // 响应代号
optional string desc = 2; // 验证码
}
message recharge_request
{
required string mobile = 1; // 手机号码
required int32 amount = 2; // 充值金额
}
message recharge_response
{
required int32 code = 1; // 响应代号
optional string desc = 2; // 验证码
required int32 balance = 3; // 最新的余额
}
message account_balance_request //balance:余额
{
required string mobile = 1;//回应一个手机号
}
message account_balance_response
{
required int32 code = 1; // 响应代号
optional string desc = 2; // 验证码
required int32 balance = 3;
}
message list_account_records_request
{
required string mobile = 1;
}
message list_account_records_response
{
required int32 code = 1; // 响应代号
optional string desc = 2; // 验证码
message account_record
{
required int32 type = 1; // 0 : 骑行消费, 1 : 充值, 2 : 退款
required int32 limit = 2; // 消费或者充值金额
required uint64 timestamp = 3; // 记录发生时的时间戳
}
repeated account_record records = 3;
}
message list_travel_records_request
{
required string mobile = 1;
}
message list_travel_records_response
{
required int32 code = 1; // 响应代号
optional string desc = 2; // 验证码
message travel_record
{
required uint64 stm = 1; // start timestamp
required uint32 duration = 2; // 骑行时长
required uint32 amount = 3; // 所耗金额
}
required double mileage = 3; // 里程
required double discharge = 4; // 排放
required double calorie = 5; // 卡路里
repeated travel_record records = 6;
}
protoc --cpp_out=. bike.proto//它会在当前目录下会生成两个C++文件
- 设置环境变量
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
- example1.cc
#include "bike.pb.h"
#include
#include
using namespace std;
using namespace tutorial;
int main(void)
{
std::string data; // 存储序列化的信息
// 客户端发出请求
{
mobile_request mr;
mr.set_mobile("12345678910");
mr.SerializeToString(&data); // 打包变成2进制数据
cout << "序列化之后的数据 :[" << data.size() << "]" << endl;
cout << data << endl;
// 反序列化
cout << (int)*((char *)data.c_str()) << endl;
cout << (int)*((char *)(data.c_str() + 1)) << endl;
// 客户端发送data send(sockfd,data.c_str(),data.length());
}
// 服务器接收请求
{
// receive(sockfd,data,...);
mobile_request mr;
mr.ParseFromString(data);
cout << "客户端手机号码 : " << mr.mobile() << endl;
}
return 0;
}
// g++ -std=c++11 example.cc bike.pb.cc -lprotobuf -pthread
g++ -std=c++11 example.cc bike.pb.cc -lprotobuf -pthread
#include "bike.pb.h"
#include
#include
using namespace std;
using namespace tutorial;
int main(void)
{
std::string data; // 存储序列化的信息
// 客户端发出请求
{
list_account_records_response larr;
larr.set_code(200);
larr.set_desc("ok");
// add five records
for (int i = 0; i < 5; ++i)
{
list_account_records_response_account_record *ar = larr.add_records();
ar->set_type(0);
ar->set_limit(i * 100);
ar->set_timestamp(time(NULL));
}
printf("records size: %d \n", larr.records_size());
larr.SerializeToString(&data); // 打包变成2进制数据
// 客户端发送data send(sockfd,data.c_str(),data.length());
}
// 服务器接收请求
{
// receive(sockfd,data,...);
list_account_records_response larr;
larr.ParseFromString(data);
printf("records size :%d \n", larr.records_size());
printf("code :%d \n", larr.code());
for (int i = 0; i < larr.records_size(); ++i)
{
const list_account_records_response_account_record &ar = larr.records(i);
printf("limit : %d\n", ar.limit());
}
}
return 0;
}
// g++ -std=c++11 example1.cc bike.pb.cc -lprotobuf -pthread
g++ -std=c++11 example1.cc bike.pb.cc -lprotobuf -pthread