Linus Griebsch
Linus Griebsch

Reputation: 31

MSVC Visual Studio 22 Protobuf integration errors

I am trying to get protobuf to work for Visual C++ Visual Studio 2022 on windows 10, but the generated pb.h and pb.cc files are throwing tons of errors like: PROTOBUF_NAMESPACE_OPEN this declaration has no storage class or type specifier Since I just switched from go to c++ I am wondering if I am missing any steps that might be obvious to c++ users.

Reproduction Steps: I followed the instructions found here: https://medium.com/@dev.ashurai/protoc-protobuf-installation-on-windows-linux-mac-d70d5380489d and here under the c++ windows section: https://github.com/protocolbuffers/protobuf/blob/main/src/README.md

Basically using vcpkg to install protobuff. I then build the pb.c and pb.cc from the proto file detailed below using the protoc.exe inside the vcpkg packages folder. I create an empty C++ project and add the protobuff files to it.


option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";

package helloworld;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

Upvotes: 2

Views: 3331

Answers (1)

Setsu
Setsu

Reputation: 1218

I just ran into the same problem after having everything installed and started integrating my generated .pb.h/.pb.cpp files into my project. It seems to me to be a Visual Studio bug.

First, make sure your Protobuf is properly installed:

  1. install vcpkg if you haven't already
  2. install protobuf using vcpkg
  3. use the command vcpkg integrate install to have it automatically configure Visual Studio to use installed packages

You can verify the install by opening your project in visual studio and checking the project properties: VC++ Directories \ External Include Directories. The evaluated list should include you vcpkg directory.

Once done, take a look at your .pb.h header file and note where the compile errors are. If you get errors in the #include <google/protobuf/...> lines then there's something wrong with your protobuf installation. If you get errors on lines with macro expansions, such as PROTOBUF_NAMESPACE_OPEN then do the following:

  1. Mouse over the error and let intellisense show you what the expanded value should be, and replace the macro with the defined value. For example, PROTOBUF_NAMESPACE_OPEN expands to namespace google { namespace protobuf {
  2. Do this for every instance of the macro that shows errors.

Hopefully, after you have fixed all of the problematic macros you should be able to compile.

The reason I think this is a Visual Studio bug is because intellisense is able to correctly expand the macro but the compiler can't. Also, not all instances of the same macro will give compile errors, which is really weird.

Upvotes: 1

Related Questions