arm64交叉编译protobuf 3.12.0

目录

   

1.源码下载

2.编译步骤

3.验证 & 测试


   

1.源码下载

1.源码下载

github下载源码 GitHub - protocolbuffers/protobuf: Protocol Buffers - Google's data interchange format

2.编译步骤

 ./autogen.sh
如果出错的话 apt-get install这些:
autoconf
automake
ibtool
make
g++
unzip

 交叉编译的话

     PATH 是你工具链所在的路径
$ ./configure --host=arm-linux CC=/PATH/aarch64-linux-gnu-gcc CXX=/PATH/aarch64-linux-gnu-g++ --prefix=/home/protobuf/install
$ make -j;make install

3.验证 & 测试

在/home/protobuf/install   下有 bin  include  lib  三个文件夹  分别表示 可执行文件、头文件、生成的库

cd home/protobuf/install 
cd bin
protoc --version   //查看 protoc是不是arm64 版本  如果是交叉编译成功  但是得测试一下

测试用例

helloworld.proto

syntax = "proto3";
 
package lm; 
message helloworld 
{ 
    int32     id = 1;  // ID 
    string    str = 2;  // str 
}

person.proto

syntax="proto3";
package tutorial;
 
message Person
{
	string name = 1;
	int32 id = 2;
	string email = 3;
 
	enum PhoneType
	{
		MOBILE = 0;
		HOME = 1;
		WORK = 2;
	}
 
	message PhoneNumber
	{
		string number = 1;
		PhoneType type = 2; 
	}
 
	repeated PhoneNumber phone = 4;
}
 
message AddressBook
{
	repeated Person person =1;
}

protoc -I=./ --cpp_out=./ helloworld.proto
protoc -I=./ --cpp_out=./ person.proto

会自动生成文件:

helloworld.pb.h

helloworld.pb.cc

person.pb.h

person.pb.cc

 

CMakeLists.txt        (参考的 暂不明确此处用处)

cmake_minimum_required(VERSION 2.8)
 
project(pbtest)
 
#set(SRC .)
#查找当前目录下的所有源文件,并将名称保存到DIR_SRCS变量
aux_source_directory(. DIR_SRCS)
 
add_executable(${PROJECT_NAME} ${DIR_SRCS})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} protobuf)


 

main.cpp

#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string>
#include <pthread.h>
#include <fstream>
#include "helloworld.pb.h"
 
using namespace std;
#define BUFFSIZE 1024
 
int main()
{
    GOOGLE_PROTOBUF_VERIFY_VERSION;
 
    //eg1, write file
    lm::helloworld msg1;
    msg1.set_id(1001);
    msg1.set_str("hello world");
 
    fstream output("./log", ios::out | ios::trunc | ios::binary);
    if (!msg1.SerializeToOstream(&output)) {
        cerr << "Failed to write msg." << endl;
        return -1;
    }
 
    output.close();
    cout << msg1.id() << endl;
    cout << msg1.str() << endl;
 
    //eg2, read file
    lm::helloworld msg2;
    fstream input("./log", ios::in | ios::binary);
    if (!input)
    {
        cerr << "open file failed!\n";
        return -1;
    }
 
    if (!msg2.ParseFromIstream(&input)) {
        cerr << "Parse file failed!" << endl;
        return -1;
    }
 
    input.close();
    cout << msg2.id() << endl;
    cout << msg2.str() << endl;
 
    //eg3, write buf, protobuf序列化
    lm::helloworld msg3;
    msg3.set_id(1002);
    msg3.set_str("good idea");
    char buf[BUFFSIZE];
    memset(buf, 0, BUFFSIZE);
    msg3.SerializeToArray(buf, BUFFSIZE);
 
    //eg4, read buf, protobuf反序列化
    lm::helloworld msg4;
    msg4.ParseFromArray(buf, BUFFSIZE);
    cout << msg4.id() << endl;
    cout << msg4.str() << endl;
 
    // Optional:  Delete all global objects allocated by libprotobuf.
    google::protobuf::ShutdownProtobufLibrary();
 
    return 0;
}

测试用例参考:我个人的protobuf-3.5.2实践:安装与测试_libaineu2004的博客-CSDN博客

​
生成测试的可执行文件:g++ -g -Wall -std=c++11 main.cpp helloworld.pb.cc person.pb.cc -L/home/protobuf/install/lib  -I/home/protobuf/install/include  -lprotobuf -lpthread


注意: 这是是交叉编译的 arm64  的可执行文件  在x86平台不可运行

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要进行交叉编译protobuf,您需要以下步骤: 1. 确定目标平台的架构和操作系统,例如 ARM 架构的 Linux 系统。 2. 安装交叉编译工具链,该工具链包括交叉编译器和库文件。这些工具可用于在开发机上编译目标平台的代码。具体的安装方法因不同的平台而异。 3. 下载protobuf源代码,您可以从GitHub上下载最新版本的protobuf源代码。解压后进入protobuf源代码目录。 4. 配置protobuf源代码的编译选项。运行如下命令: ``` ./configure --host=target --with-sysroot=path/to/sysroot ``` 其中,`--host` 参数指定目标平台的架构和操作系统,`--with-sysroot` 参数指定交叉编译器的 sysroot 路径。 5. 运行 make 命令来编译protobuf。如果一切正常,make 命令将生成一个可执行文件,名为 `protoc`,它是 protobuf 的编译器。 ``` make ``` 6. 运行 make install 命令来安装protobuf。 ``` make install ``` 完成上述步骤后,您就可以在目标平台上使用 protobuf 了。 ### 回答2: 交叉编译是指在一台机器上编译适用于另一种体系结构的软件。若要进行protobuf交叉编译,可以按照以下步骤进行操作: 1. 准备交叉编译工具链:首先需要安装适用于目标体系结构的交叉编译工具链,可以通过搜索引擎获取相关的工具链安装方式。 2. 下载与安装protobuf源码:从protobuf的官方GitHub仓库下载最新的源码,并解压缩到本地文件夹。 3. 配置交叉编译环境变量:进入protobuf的源码文件夹,在命令行中设置交叉编译的环境变量,以便正确地使用交叉编译工具链。 4. 配置编译参数:使用文本编辑器打开源码文件夹中的根目录下的`configure`文件,并修改其中的编译参数。根据目标体系结构的不同,可以根据需要进行添加、修改或删除一些参数。 5. 运行配置脚本:在命令行中运行`./configure`命令,开始配置protobuf的编译环境。 6. 编译protobuf:运行`make`命令开始编译protobuf。编译过程可能需要一些时间,请耐心等待完成。 7. 交叉编译结果获取:编译完成后,在源码文件夹中可以找到编译好的protobuf二进制文件。将这些文件拷贝到目标体系结构所在的机器中。 以上是一个简单的protobuf交叉编译的过程。具体的操作步骤可能因不同的体系结构而有所差异,可以根据实际需要进行相应的修改和调整。 ### 回答3: 交叉编译protobuf可以按照以下步骤进行: 1. 首先,确保在本地机器上安装了protoc编译器和protobuf库。如果没有,可以从官方网站下载并按照官方文档进行安装。 2. 创建一个交叉编译工具链。根据目标平台的不同,选择合适的交叉编译工具链来编译protobuf。这通常包括交叉编译器、库文件和头文件,可以从目标平台的官方网站或其他来源获取。 3. 下载protobuf源码,并解压到本地。 4. 进入protobuf源码目录,并使用交叉编译工具链的前缀指定编译器和链接器,执行以下命令进行配置: ``` $ ./configure --host=目标平台-triplet --with-protoc=protoc路径 ``` 注意将目标平台-triplet替换为交叉编译工具链的前缀,将protoc路径替换为本地机器上protoc的路径。 5. 执行make命令进行编译。这将使用交叉编译工具链来编译protobuf,并生成目标平台的可执行文件和库文件。 6. 编译完成后,可以将生成的可执行文件和库文件拷贝到目标平台上进行部署和使用。 总结:交叉编译protobuf需要创建交叉编译工具链,并使用该工具链对protobuf源码进行配置和编译。通过这种方式,可以从一台机器上编译生成在另一台机器上运行的可执行文件和库文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值