如何使用Protobuf元素创建库,而无需依赖关系

发布于 2025-02-13 22:46:46 字数 1835 浏览 0 评论 0原文

我必须为我的工作创建一个包含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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文