Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use google protobuf in a project with precompiled headers

I have a solution which contains several projects. My projects (but not all of them) use precompiled headers. I decided to use protobuf and I've met a problem. After generetaing *.pb.h from *.proto by protoc.exe I'm trying to include the header and get the error - precompiled header wasn't included into *.pb.h.

How I can solve this problem? I have an idea (but I don't like it at all) - after protoc generates *.pb.h I can run some script, which'll include my precompiled header into the *.pb.h. But I don't like it because some projects may not use PCH, and PCH file name can be different.

I understand that I can just remove PCH from my projects, but I don't like that idea too.

like image 751
Arman Oganesyan Avatar asked Feb 04 '23 08:02

Arman Oganesyan


2 Answers

Dont add the generated myproto.pb.cc to your project. Instead, create a myproto.cpp with

#include "pch.h"
#include "myproto.pb.cc"
like image 59
palfi Avatar answered Feb 12 '23 07:02

palfi


I resolved my problem by creating a static library called proto-objects (without PCH) and including all my *pb.h(cpp) files there. After it I link that library to every project where I need my protobuf objects. Profit!

enter image description here

like image 28
Arman Oganesyan Avatar answered Feb 12 '23 09:02

Arman Oganesyan