How to use google protobuf in a project with precompiled headers

*爱你&永不变心* 提交于 2021-02-18 13:53:18

问题


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.


回答1:


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!




回答2:


You can disable the pre-compiled header option on a file-by-file basis.

Given that the pch option is intended to speed up compilation, you can turn it off for the whole project, and no further changes should be necessary.

The choice of name of the header file, and the pch file are also selectable per file in the project

Update

The idea behind Microsoft's Pre-compilation PCH system is to

  • Speed up compilation
  • Make it easy to use

The header file system in C/C++ is problematic, as it is really a textual replacement.

That means that

#include "localdefs.h"
#include <windows.h>
#include "project.h"
#include "support.h"

Is in no way similar to

#include <windows.h>
#include "project.h"
#include "support.h"

That is because localdefs.h can redefine the behavior of all of the other includes.

Further to this the costs of walking through the complexities of the windows.h header files, is time consuming.

The PCH system tries to solve this by the observation that most projects have a fixed set of include files which are included by most/all of the CPP files.

Defining this set in stdafx.h allows the textual result of that parsing to be pasted in the cpp file and save a lot of work.

If most of the includes in the project are different, then there is no need to use it.

So if you are including the same qt header files in lots of places - add them to a pre-compiled header file. The more of the common includes added to this file, the better the compile speed improvements will be.

Any non-standard cpp file can be excluded by being specifically disabled - examples are "generated files". Where the template generator does not understand the MSVC system.

If all the files are different, then only limited performance benefit will be gained - as each compile would probably also include a pch recompile.




回答3:


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

#include "pch.h"
#include "myproto.pb.cc"


来源:https://stackoverflow.com/questions/44307127/how-to-use-google-protobuf-in-a-project-with-precompiled-headers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!