C++ Phonebook example doesn't compile on Windows with protobuf from vcpkg and latest LLVM/MSVC toolchain

207 views
Skip to first unread message

Evan Wegley

unread,
Apr 10, 2023, 11:58:40 AM4/10/23
to Protocol Buffers
The compiler error I get seems to relate to protoc-generated C++ code using offsetof in a constexpr statement, but the MSVC-provided offsetof macro uses reinterpret_cast.

In file included from .\writer.cpp:4:
In file included from ./addressbook.pb.h:26:
In file included from C:\vcpkg\packages\protobuf_x64-windows\include\google/protobuf/generated_message_util.h:54:
In file included from C:\vcpkg\packages\protobuf_x64-windows\include\google/protobuf/implicit_weak_message.h:39:
In file included from C:\vcpkg\packages\protobuf_x64-windows\include\google/protobuf/repeated_field.h:60:
C:\vcpkg\packages\protobuf_x64-windows\include\google/protobuf/repeated_ptr_field.h(645,27): error: constexpr variable
      'kRepHeaderSize' must be initialized by a constant expression
  static constexpr size_t kRepHeaderSize = offsetof(Rep, elements);
                          ^                ~~~~~~~~~~~~~~~~~~~~~~~
C:\vcpkg\packages\protobuf_x64-windows\include\google/protobuf/repeated_ptr_field.h(645,44): note: cast that performs
      the conversions of a reinterpret_cast is not allowed in a constant expression
  static constexpr size_t kRepHeaderSize = offsetof(Rep, elements);
                                           ^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt\stddef.h(47,32): note: expanded from macro 'offsetof'
        #define offsetof(s,m) ((::size_t)&reinterpret_cast<char const volatile&>((((s*)0)->m)))


My environment:
  • Windows 10, 64-bit
  • Installed protobuf from vcpkg as per these instructions (vcpkg install protobuf protobuf:x64-windows) (protoc.exe --version reports 3.21.12)
  • Installed latest LLVM toolchain from their releases (LLVM-16.0.1-win64.exe)
  • Installed Visual Studio 2022 (cl.exe version is 19.35.32215)
To reproduce:
  1. Save this proto structure as addressbook.proto
  2. Run C:\vcpkg\packages\protobuf_x64-windows\tools\protobuf\protoc.exe --cpp_out=. addressbook.proto
  3. Save this code as writer.cpp
  4. Run C:\Program Files\LLVM\bin\clang-cl.exe /std:c++20 '/IC:\vcpkg\packages\protobuf_x64-windows\include' -EHsc .\writer.cpp /MD /link '/LIBPATH:C:\vcpkg\packages\protobuf_x64-windows\lib'

Is there a way to make the protoc.exe pre-compiled and distributed by vcpkg to generate valid C++ output for this toolchain, or do I have to compile protobuf myself to get this working? Not sure if this is a bug or an issue with my use case.
Reply all
Reply to author
Forward
0 new messages