如何将编译的Protobuf导出到静态库C++
我想制作一个包含.cc和.h文件的静态库,这些库是从Protoc编译器生成的,以与其他项目链接,我正在使用C ++
I want to make a static library containing .cc and .h files that are generated from protoc compiler to link with it from other project, I'm using C++
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是手动使用ProtoC,您可以让CMAKE编译原始文件。为此,您将在与原始文件的同一文件夹中使用cmakelists.txt(我在此处使用一个称为proto的单独文件夹)。而且此cmakelists.txt看起来像:
因此,您需要看到需要指定要编译的proto文件(第一个
set
)。然后,您需要指定生成的CC和H文件的名称(第二和第三set
)。然后,我们使用自定义命令自动生成您使用ProtoC手动做的事情。最后,我们使用
protos_lib.a创建一个
,然后将ProtoBuf依赖关系与libprotos.a
(静态库)文件,用add_library
targe> target_link_libraries
链接。希望有帮助,让我知道您是否需要更多细节。
Instead of using protoc manually you can let CMake compile the proto files. To do so you will have CMakeLists.txt in the same folder as you proto files (I use a separate folder called proto here). and this CMakeLists.txt will look like:
So as you can see you need to specify the proto files that you want to compile (first
set
). Then you need the specify the name of the generated cc and h file (second and thirdset
). And then we use the custom command to generate automatically what you have been doing manually with protoc.Finally, we create a
libprotos.a
(static library) file called protos_lib.a with theadd_library
and link the protobuf dependency withtarget_link_libraries
.Hope that helps, let me know if you need more details.