Installing protobuf for Windows 11, Visual Studio 2022, C++, cmake

These are my notes on using protobuf as part of a recent experiment. For these notes, I an using Microsoft Windows 11, Visual Studio 2017, cmake 3.22.1, and protobuf 3.20.1.

Acquire source code

Download the version-specific, language-specific distribution file: https://github.com/protocolbuffers/protobuf/releases/download/v3.20.1/protobuf-cpp-3.20.1.zip (this can be found from the distribution page as newer releases are made available (https://github.com/protocolbuffers/protobuf/releases). Unzip the file. For me, I unzipped in my WIndows home directory resulting in a folder located at C:\Users\cj\protobuf-3.20.1.

Use cmake to prepare build files

I used a git bash terminal window with cmake version 3.22.3. The output looked like this:

$ cd ~/protobuf-3.20.1/cmake
$ mkdir build
$ cd build
$ cmake ..
-- Building for: Visual Studio 17 2022
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.22000.
-- The C compiler identification is MSVC 19.31.31107.0
-- The CXX compiler identification is MSVC 19.31.31107.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.31.31103/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.31.31103/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
--
-- 3.20.1.0
-- Performing Test protobuf_HAVE_LD_VERSION_SCRIPT
-- Performing Test protobuf_HAVE_LD_VERSION_SCRIPT - Failed
-- Looking for pthread.h
-- Looking for pthread.h - not found
-- Found Threads: TRUE
-- Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR)
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/cj/protobuf-3.20.1/cmake/build

From the output, you can see that there may be a couple potential issues, but I believe these are things I can move forward without for now (for my limited experimental purposes).

Build in Visual Studio

After running cmake, you should have a VS solution file. You can open the solution file as a normal user and build the projects it contains, but you may not be able to successfully run the “INSTALL” project without running VS as an administrator (in order to install files into system directories). For this reason, I first opened VS as an admin, then “built” the INSTALL project. Here is the VS output:

Build started...
1>------ Build started: Project: INSTALL, Configuration: Release x64 ------
1>-- Install configuration: "Release"
1>-- Installing: C:/Program Files (x86)/protobuf/lib/libprotobuf-lite.lib
1>-- Installing: C:/Program Files (x86)/protobuf/lib/libprotobuf.lib
1>-- Installing: C:/Program Files (x86)/protobuf/lib/libprotoc.lib
1>-- Installing: C:/Program Files (x86)/protobuf/bin/protoc.exe
1>-- Installing: C:/Program Files (x86)/protobuf/lib/pkgconfig/protobuf.pc
1>-- Installing: C:/Program Files (x86)/protobuf/lib/pkgconfig/protobuf-lite.pc
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/any.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/any.pb.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/api.pb.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/arena.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/arena_impl.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/arenastring.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/arenaz_sampler.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/code_generator.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/command_line_interface.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/cpp/cpp_file.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/cpp/cpp_generator.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/cpp/cpp_helpers.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/cpp/cpp_names.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/csharp/csharp_doc_comment.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/csharp/csharp_generator.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/csharp/csharp_names.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/csharp/csharp_options.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/importer.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/java/java_generator.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/java/java_kotlin_generator.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/java/java_names.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/js/js_generator.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/objectivec/objectivec_generator.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/objectivec/objectivec_helpers.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/parser.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/php/php_generator.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/plugin.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/plugin.pb.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/python/python_generator.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/python/python_pyi_generator.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/python/python_helpers.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/ruby/ruby_generator.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/descriptor.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/descriptor.pb.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/descriptor_database.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/duration.pb.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/dynamic_message.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/empty.pb.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/explicitly_constructed.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/extension_set.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/extension_set_inl.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/field_access_listener.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/field_mask.pb.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/generated_enum_reflection.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/generated_enum_util.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/generated_message_bases.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/generated_message_reflection.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/generated_message_tctable_decl.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/generated_message_tctable_impl.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/generated_message_util.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/has_bits.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/implicit_weak_message.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/inlined_string_field.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/io/coded_stream.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/io/gzip_stream.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/io/io_win32.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/io/printer.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/io/strtod.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/io/tokenizer.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/io/zero_copy_stream.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/io/zero_copy_stream_impl.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/io/zero_copy_stream_impl_lite.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/map.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/map_entry.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/map_entry_lite.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/map_field.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/map_field_inl.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/map_field_lite.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/map_type_handler.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/message.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/message_lite.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/metadata.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/metadata_lite.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/parse_context.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/port.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/port_def.inc
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/port_undef.inc
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/reflection.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/reflection_ops.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/repeated_field.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/repeated_ptr_field.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/service.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/source_context.pb.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/struct.pb.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/stubs/bytestream.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/stubs/callback.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/stubs/casts.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/stubs/common.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/stubs/hash.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/stubs/logging.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/stubs/macros.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/stubs/map_util.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/stubs/mutex.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/stubs/once.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/stubs/platform_macros.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/stubs/port.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/stubs/status.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/stubs/stl_util.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/stubs/stringpiece.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/stubs/strutil.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/stubs/template_util.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/text_format.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/timestamp.pb.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/type.pb.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/unknown_field_set.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/util/delimited_message_util.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/util/field_comparator.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/util/field_mask_util.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/util/json_util.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/util/message_differencer.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/util/time_util.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/util/type_resolver.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/util/type_resolver_util.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/wire_format.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/wire_format_lite.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/wrappers.pb.h
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/any.proto
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/api.proto
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/plugin.proto
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/descriptor.proto
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/duration.proto
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/empty.proto
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/field_mask.proto
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/source_context.proto
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/struct.proto
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/timestamp.proto
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/type.proto
1>-- Installing: C:/Program Files (x86)/protobuf/include/google/protobuf/wrappers.proto
1>-- Up-to-date: C:/Program Files (x86)/protobuf/include/google/protobuf/any.proto
1>-- Up-to-date: C:/Program Files (x86)/protobuf/include/google/protobuf/api.proto
1>-- Up-to-date: C:/Program Files (x86)/protobuf/include/google/protobuf/compiler/plugin.proto
1>-- Up-to-date: C:/Program Files (x86)/protobuf/include/google/protobuf/descriptor.proto
1>-- Up-to-date: C:/Program Files (x86)/protobuf/include/google/protobuf/duration.proto
1>-- Up-to-date: C:/Program Files (x86)/protobuf/include/google/protobuf/empty.proto
1>-- Up-to-date: C:/Program Files (x86)/protobuf/include/google/protobuf/field_mask.proto
1>-- Up-to-date: C:/Program Files (x86)/protobuf/include/google/protobuf/source_context.proto
1>-- Up-to-date: C:/Program Files (x86)/protobuf/include/google/protobuf/struct.proto
1>-- Up-to-date: C:/Program Files (x86)/protobuf/include/google/protobuf/timestamp.proto
1>-- Up-to-date: C:/Program Files (x86)/protobuf/include/google/protobuf/type.proto
1>-- Up-to-date: C:/Program Files (x86)/protobuf/include/google/protobuf/wrappers.proto
1>-- Installing: C:/Program Files (x86)/protobuf/cmake/protobuf-targets.cmake
1>-- Installing: C:/Program Files (x86)/protobuf/cmake/protobuf-targets-release.cmake
1>-- Up-to-date: C:/Program Files (x86)/protobuf/cmake
1>-- Installing: C:/Program Files (x86)/protobuf/cmake/protobuf-config-version.cmake
1>-- Installing: C:/Program Files (x86)/protobuf/cmake/protobuf-config.cmake
1>-- Installing: C:/Program Files (x86)/protobuf/cmake/protobuf-module.cmake
1>-- Installing: C:/Program Files (x86)/protobuf/cmake/protobuf-options.cmake
========== Build: 1 succeeded, 0 failed, 12 up-to-date, 0 skipped ========== 

From here I can see where the C++ header files and library files are installed.

My initial attempt at adding protobuf to my cmake-based project resulted in several linking errors related to the Windows runtime library settings. An answer to a similar question on stack exchange (https://stackoverflow.com/a/56490614/6311677) led me to a solution that worked in my project. Essentially, I added this after my cmake_minimum_required line:

cmake_policy(SET CMP0091 NEW)

And I added this after my add_executable line:

set_property(TARGET circucast PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>")

The name “circucast” is the name of the project I am experimenting integrating protobuf into. Here are the other relevant changes I needed to make to my CMakeLists.txt file:

I created a circucastnode.proto file and included it in my “add_executable” cmake declaration:

add_executable(circucast WIN32
  src/circucast.cpp
  ...
  src/circucastnode.proto
) 

I added the find_package declaration after setting the C++ standard (e.g. “set(CMAKE_CXX_STANDARD 17)”). I think this just need to be near the top before other protobuf functions are used.

find_package(protobuf CONFIG REQUIRED)

Finally, in my target_link_libraries declaration, I added “protobuf::libprotobuf” and the “protobuf_generate” declaration:

target_link_libraries(circucast ${COMMON_LIBS} ... protobuf::libprotobuf)
protobuf_generate(TARGET ${PROJECT_NAME})

Now that I could run cmake and generate a project that would link against, protobuf, I could verify that libraries could be included in the source code. I added th following include statements and built my project for verification:

#include <google/protobuf/util/time_util.h>
#include "src/circucastnode.pb.h"

Side note: after adding the include statements above, I actually had a bunch of compiler errors that turned out to be related to teh way I named one of my variables. I had a variable named “interface” – which is evidently used form something in one of the new include files I added. Once I renamed the variable to something different/non-conflicting, everything built OK. I still had not integrated protobuf in any useful way at this point, but it was a start to get all the plumbing in place. I figured these notes will be helpful for the next time I attempt to get this setup in another project.

Leave a Comment