如何使用Protobuf元素创建库,而无需依赖关系
我必须为我的工作创建一个包含Protobuf元素的库。它应该在其他PC上运行,而不安装Protobuf。但是我不能在同一文件夹中使用Protobuf文件的情况下运行库(所以我将此文件编译到,并且用户必须安装ProtoBuf),
因此我为此创建了一个测试程序来证明我的问题:
- > lias 。
syntax = "proto3";
package aliasPackage;
message NumberSave{
int32 number = 1;
}
protoc -I. --cpp_out=. alias.proto
#include "alias.pb.h"
class Backbone{
public:
Backbone();
~Backbone();
void save_Number(int value);
int get_Number();
private:
aliasPackage::NumberSave number;
};
#include "../header/Backbone.h"
#include <google/protobuf/stubs/common.h>
Backbone::Backbone()
{
number.set_number(0);
}
Backbone::~Backbone()
{
google::protobuf::ShutdownProtobufLibrary();
}
void Backbone::save_Number(int value)
{
number.set_number(value);
}
int Backbone::get_Number()
{
return number.number();
}
class Backbone{
public:
Backbone();
~Backbone();
void save_Number(int value);
int get_Number();
};
命令(我考虑了一个静态的命令,将所有独立性汇总到文件中,但似乎行不通)
g++ -c --std=c++20 -O3 -DBUILD_SHARED_LIBS=ON -I/usr/local/include *.c* -lpthread -L/usr/local/lib -lprotobuf && ar rcs Backbone.sll *.o
- &gt; main.cpp
#include "../libs/lib/Backbone.h"
#include <iostream>
int main(){
auto backbone = new Backbone();
backbone->save_Number(5);
std::cout<<backbone->get_Number()<<std::endl;
delete backbone;
return 0;
}
compile命令
g++ -Wall -Wextra -Wpedantic --std=c++20 -O3 main.cpp -L. -l:../libs/lib/Backbone.sll
我在做什么错?创建LIB的正确命令是什么,以便用户可以在不安装Protobuf的情况下使用它?
I have to create a Library for my work that contains Protobuf elements. It should run on every other PC without installing Protobuf. But I can not make the library run without having also the Protobuf files in the same folder (so I compile this files to and the user have to install Protobuf)
So I created for this a test program to demonstrate my problem:
->alias.proto
syntax = "proto3";
package aliasPackage;
message NumberSave{
int32 number = 1;
}
I created the .h and .cc with
protoc -I. --cpp_out=. alias.proto
->Backbone.h
#include "alias.pb.h"
class Backbone{
public:
Backbone();
~Backbone();
void save_Number(int value);
int get_Number();
private:
aliasPackage::NumberSave number;
};
->Backbone.cpp
#include "../header/Backbone.h"
#include <google/protobuf/stubs/common.h>
Backbone::Backbone()
{
number.set_number(0);
}
Backbone::~Backbone()
{
google::protobuf::ShutdownProtobufLibrary();
}
void Backbone::save_Number(int value)
{
number.set_number(value);
}
int Backbone::get_Number()
{
return number.number();
}
->Backbone.h for the lib
class Backbone{
public:
Backbone();
~Backbone();
void save_Number(int value);
int get_Number();
};
Creating the lib out of the files with this command (I thought about a static one, compiling the all independences into the file, but it seems not to work)
g++ -c --std=c++20 -O3 -DBUILD_SHARED_LIBS=ON -I/usr/local/include *.c* -lpthread -L/usr/local/lib -lprotobuf && ar rcs Backbone.sll *.o
->main.cpp
#include "../libs/lib/Backbone.h"
#include <iostream>
int main(){
auto backbone = new Backbone();
backbone->save_Number(5);
std::cout<<backbone->get_Number()<<std::endl;
delete backbone;
return 0;
}
Compile command
g++ -Wall -Wextra -Wpedantic --std=c++20 -O3 main.cpp -L. -l:../libs/lib/Backbone.sll
What am I doing wrong? What's the right command to create a Lib so that users can use it without installing Protobuf?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论