Linux protobuf错误处理

In file included from /usr/include/c++/4.8.2/mutex:35:0,
                 from /usr/local/include/google/protobuf/stubs/mutex.h:33,
                 from /usr/local/include/google/protobuf/stubs/common.h:52,
                 from student.pb.h:9,
                 from test.cpp:5:
/usr/include/c++/4.8.2/bits/c++0x_warning.h:32:2: 错误:#error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support for the \
  ^
In file included from /usr/local/include/google/protobuf/stubs/common.h:52:0,
                 from student.pb.h:9,
                 from test.cpp:5:
/usr/local/include/google/protobuf/stubs/mutex.h:58:3: 错误:‘mutex’不是命名空间‘std’中的一个类型名
   std::mutex mu_;
   ^
/usr/local/include/google/protobuf/stubs/mutex.h: 在成员函数‘void google::protobuf::internal::WrappedMutex::Lock()’中:
/usr/local/include/google/protobuf/stubs/mutex.h:51:17: 错误:‘mu_’在此作用域中尚未声明
   void Lock() { mu_.lock(); }
                 ^
/usr/local/include/google/protobuf/stubs/mutex.h: 在成员函数‘void google::protobuf::internal::WrappedMutex::Unlock()’中:
/usr/local/include/google/protobuf/stubs/mutex.h:52:19: 错误:‘mu_’在此作用域中尚未声明
   void Unlock() { mu_.unlock(); }
                   ^
/usr/local/include/google/protobuf/stubs/mutex.h: 在全局域:
/usr/local/include/google/protobuf/stubs/mutex.h:61:7: 错误:expected nested-name-specifier before ‘Mutex’
 using Mutex = WrappedMutex;
       ^
/usr/local/include/google/protobuf/stubs/mutex.h:61:7: 错误:‘Mutex’未声明
/usr/local/include/google/protobuf/stubs/mutex.h:61:13: 错误:expected ‘;’ before ‘=’ token
 using Mutex = WrappedMutex;
             ^
/usr/local/include/google/protobuf/stubs/mutex.h:61:13: 错误:expected unqualified-id before ‘=’ token
/usr/local/include/google/protobuf/stubs/mutex.h:66:28: 错误:expected ‘)’ before ‘*’ token
   explicit MutexLock(Mutex *mu) : mu_(mu) { this->mu_->Lock(); }
                            ^
/usr/local/include/google/protobuf/stubs/mutex.h:69:3: 错误:‘Mutex’不是一个类型名
   Mutex *const mu_;
   ^
/usr/local/include/google/protobuf/stubs/mutex.h: 在析构函数‘google::protobuf::internal::MutexLock::~MutexLock()’中:
/usr/local/include/google/protobuf/stubs/mutex.h:67:24: 错误:‘class google::protobuf::internal::MutexLock’没有名为‘mu_’的成员
   ~MutexLock() { this->mu_->Unlock(); }
                        ^
/usr/local/include/google/protobuf/stubs/mutex.h: 在全局域:
/usr/local/include/google/protobuf/stubs/mutex.h:80:33: 错误:expected ‘)’ before ‘*’ token
   explicit MutexLockMaybe(Mutex *mu) :
                                 ^
In file included from /usr/local/include/google/protobuf/arena.h:48:0,
                 from student.pb.h:23,
                 from test.cpp:5:
/usr/include/c++/4.8.2/typeinfo:39:37: 错误:expected ‘}’ before end of line
 #pragma GCC visibility push(default)
                                     ^
/usr/include/c++/4.8.2/typeinfo:39:37: 错误:expected unqualified-id before end of line
/usr/include/c++/4.8.2/typeinfo:39:37: 错误:expected ‘}’ before end of line
/usr/include/c++/4.8.2/typeinfo:39:37: 错误:expected ‘}’ before end of line
/usr/include/c++/4.8.2/typeinfo:39:37: 错误:expected ‘}’ before end of line
/usr/include/c++/4.8.2/typeinfo:39:37: 错误:expected declaration before end of line
In file included from /usr/include/c++/4.8.2/mutex:35:0,
                 from /usr/local/include/google/protobuf/stubs/mutex.h:33,
                 from /usr/local/include/google/protobuf/stubs/common.h:52,
                 from student.pb.h:9,
                 from student.pb.cc:4:
/usr/include/c++/4.8.2/bits/c++0x_warning.h:32:2: 错误:#error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support for the \
  ^
In file included from /usr/local/include/google/protobuf/stubs/common.h:52:0,
                 from student.pb.h:9,
                 from student.pb.cc:4:
/usr/local/include/google/protobuf/stubs/mutex.h:58:3: 错误:‘mutex’不是命名空间‘std’中的一个类型名
   std::mutex mu_;
   ^
/usr/local/include/google/protobuf/stubs/mutex.h: 在成员函数‘void google::protobuf::internal::WrappedMutex::Lock()’中:
/usr/local/include/google/protobuf/stubs/mutex.h:51:17: 错误:‘mu_’在此作用域中尚未声明
   void Lock() { mu_.lock(); }
                 ^
/usr/local/include/google/protobuf/stubs/mutex.h: 在成员函数‘void google::protobuf::internal::WrappedMutex::Unlock()’中:
/usr/local/include/google/protobuf/stubs/mutex.h:52:19: 错误:‘mu_’在此作用域中尚未声明
   void Unlock() { mu_.unlock(); }
                   ^
/usr/local/include/google/protobuf/stubs/mutex.h: 在全局域:
/usr/local/include/google/protobuf/stubs/mutex.h:61:7: 错误:expected nested-name-specifier before ‘Mutex’
 using Mutex = WrappedMutex;
       ^
/usr/local/include/google/protobuf/stubs/mutex.h:61:7: 错误:‘Mutex’未声明
/usr/local/include/google/protobuf/stubs/mutex.h:61:13: 错误:expected ‘;’ before ‘=’ token
 using Mutex = WrappedMutex;
             ^
/usr/local/include/google/protobuf/stubs/mutex.h:61:13: 错误:expected unqualified-id before ‘=’ token
/usr/local/include/google/protobuf/stubs/mutex.h:66:28: 错误:expected ‘)’ before ‘*’ token
   explicit MutexLock(Mutex *mu) : mu_(mu) { this->mu_->Lock(); }
                            ^
/usr/local/include/google/protobuf/stubs/mutex.h:69:3: 错误:‘Mutex’不是一个类型名
   Mutex *const mu_;
   ^
/usr/local/include/google/protobuf/stubs/mutex.h: 在析构函数‘google::protobuf::internal::MutexLock::~MutexLock()’中:
/usr/local/include/google/protobuf/stubs/mutex.h:67:24: 错误:‘class google::protobuf::internal::MutexLock’没有名为‘mu_’的成员
   ~MutexLock() { this->mu_->Unlock(); }
                        ^
/usr/local/include/google/protobuf/stubs/mutex.h: 在全局域:
/usr/local/include/google/protobuf/stubs/mutex.h:80:33: 错误:expected ‘)’ before ‘*’ token
   explicit MutexLockMaybe(Mutex *mu) :
                                 ^
In file included from /usr/local/include/google/protobuf/arena.h:48:0,
                 from student.pb.h:23,
                 from student.pb.cc:4:
/usr/include/c++/4.8.2/typeinfo:39:37: 错误:expected ‘}’ before end of line
 #pragma GCC visibility push(default)
                                     ^
/usr/include/c++/4.8.2/typeinfo:39:37: 错误:expected unqualified-id before end of line
/usr/include/c++/4.8.2/typeinfo:39:37: 错误:expected ‘}’ before end of line
/usr/include/c++/4.8.2/typeinfo:39:37: 错误:expected ‘}’ before end of line
/usr/include/c++/4.8.2/typeinfo:39:37: 错误:expected ‘}’ before end of line
/usr/include/c++/4.8.2/typeinfo:39:37: 错误:expected declaration before end of line

 

解决办法:

g++ -std=c++11  -o protobufTest.out -lprotobuf -lpthread  test.cpp student.pb.cc

 

以下是一个简单的示例代码,演示了如何在Linux C++中接收protobuf消息并进行解码: ```cpp #include <iostream> #include <string> #include <netinet/in.h> #include <sys/socket.h> #include <arpa/inet.h> #include "my_proto.pb.h" using namespace std; int main() { int sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { cerr << "socket error!" << endl; exit(-1); } struct sockaddr_in servaddr; bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(8080); servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); if (connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) { cerr << "connect error!" << endl; exit(-1); } char buffer[1024]; int n = read(sockfd, buffer, sizeof(buffer)); if (n < 0) { cerr << "read error!" << endl; exit(-1); } MyProto my_proto; if (!my_proto.ParseFromArray(buffer, n)) { cerr << "protobuf parse error!" << endl; exit(-1); } cout << "message: " << my_proto.message() << endl; close(sockfd); return 0; } ``` 这里假设我们已经定义了为`MyProto`的protobuf消息类型,并将其编码后发送给了本机的8080端口。在客户端中,我们首先创建一个Socket套接字,并将其连接到服务器。然后,我们从Socket中读取数据,并将其存储在为`buffer`的字符数组中。接下来,我们使用`ParseFromArray`函数将`buffer`中的数据解析为protobuf消息。最后,我们打印出消息中的内容,并关闭Socket连接。 请注意,上述代码仅为演示目的,并未进行错误处理或异常情况处理。在实际应用中,你需要根据具体情况进行适当的错误处理
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值