1

My VS project uses protobuf, which is accessed using the system-wide integration feature of vcpkg. When I link my project I get the following

1>...: error LNK2001: unresolved external symbol "public: virtual char const * __cdecl google::protobuf::FatalException::what(void)const " (?what@FatalException@protobuf@google@@UEBAPEBDXZ)
1>...: error LNK2001: unresolved external symbol "public: virtual __cdecl google::protobuf::FatalException::~FatalException(void)" (??1FatalException@protobuf@google@@UEAA@XZ)

The problem seems to be that PROTOBUF_USE_EXCEPTIONS is used to conditionally declare and define these functions in protobuf library common.h and common.cc, respectively. PROTOBUF_USE_EXCEPTIONS is defined in my project (seems to be the default for my project), so the header declares the functions. But, it is presumably not defined when vcpkg builds protobuf and so the functions are not defined.

How can I define PROTOBUF_USE_EXCEPTIONS when vcpkg builds protobuf?

I'm using VS2022 preview 4.1, protobuf 3.18.0, and vcpkg (2021-09-10-2059ef11aa6067e6f59b0d939c5d17e3c5c47d3e)

1 Answers1

0

This isn't an answer on getting vcpkg to build protobuf with PROTOBUF_USE_EXCEPTIONS, but I very recently encountered this issue with VS, protobuf, and vcpkg, and it ate quite a bit of my time. My resolution was different to building protobuf with PROTOBUF_USE_EXCEPTIONS, but hopefully this information can still help someone.

In my case, I encountered this issue when I was migrating an existing C++ project over to use C++20 modules and module header units. When including the protobuf headers normally in source files, no issues would occur, but when the protobuf headers were put into a module header unit, this issue would inexplicably manifest for whatever reason.

I tried several things, including undefining PROTOBUF_USE_EXCEPTIONS as well as manually defining it as 0 in the header units and source files. The only thing that worked in the end was setting PROTOBUF_USE_EXCEPTIONS=0 under Project Properties > Configuration Properties > C/C++ > Preprocessor > Preprocessor Definitions on the module header unit project in Visual Studio.

This finally made the issue go away.