What is the normal, correct way to use Protobuf in a VC++ project on Windows?

317 Views Asked by At

I'm new to C++ and I'm writing a simple program that uses protobuf definitions (.proto) as part of its work. I come from C# and had a lot of trouble understanding how to use protobuf in my VC++ project. Please excuse the long and detailed post, I have spent several hours trying to make this work, to no avail.

Following an online tutorial I got VS to automatically parse and generate H/CPP files from PROTO files with nothing but protoc.exe. Syntax highlighting even works and gives me nice colors in the IDE. Hooray! enter image description here

"$(ProjectDir)../dependencies/protobuf/bin/protoc.exe" --proto_path=$(ProjectDir) --cpp_out=$(ProjectDir) %(FullPath)

However, the generated headerfiles have a lot of dependencies on other headers and inc files that I did not expect I would need (i thought I could just get around by using protoc.exe and that it would generate self-contained header files)

#include "google/protobuf/port_undef.inc"
#include "google/protobuf/io/coded_stream.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/arenastring.h"
#include "google/protobuf/generated_message_tctable_decl.h"
#include "google/protobuf/generated_message_util.h"
...

I added the whole Protobuf release (downloaded from their github page as their instructions specify) to a nice /dependencies folder and added it as part of my "additional include directories". Now my generated files can find their dependencies... but then I hit a wall again.

Protobuf source files includes a header that is, as far as i can tell, nowhere to be found in the protobuf release. I just don't have it!

#include "absl/strings/string_view.h" // < !!!

So I hack another thing ontop of this stuff by downloading google/absl and putting it in dependencies aswell, in additional includes; Now I can compile, but I can't link, because I'm missing protobuf.lib which I thought would be part of the release somewhere.... but it's not. I had no intention of compiling protobuf myself, I trust the developers and I just wanted to use a binary. Even if I did want to build it myself, the steps I followed feel extra convoluted and I'm not sure what I was supposed to do.

Before hacking yet another thing on top of all this to make it work I thought I'd better ask here to make sure I'm doing this the normal, intended way - Yes, I have read the protobuf README as well as their website which is not an install guide and further redirects to the github readme anyway. VCPKG did not work for me.

  • Was this missing dependency normal and was I supposed to patch it by hand by fetching it myself on the internet the way I did, or was it supposed to be included/found elsewhere/part of something else that I missed?

  • Is this the normal way to install and use Protobuf, or is there supposed to be an automated way that will mostly work for a Windows VC++ setup and I made my life more complicated than necessary?

Sorry in advance if those are naive questions, I'm trying to learn what is the proper way to install protobuf in a C++ project and I stumble upon a lot of conflicting or incomplete information.

0

There are 0 best solutions below