CMake Build with Protobuf on Windows

Raymond Tang Raymond Tang 0 4210 2.60 index 1/18/2021

To build Visual C++ projects on Windows that has dependency on Google protobuf library, we can use CMake. The executable of protobuf is not sufficient to compile C++ projects.

Dependency on protobuf

To specify a dependency on protobuf, the following line can be added into CMakeLists.txt file:

find_package(Protobuf REQUIRED)

Install protobuf library on Windows

  1. Follow this article to install Microsoft vcpkg on Windows: Microsoft vcpkg C++ Library Manager

  2. Install protobuf library using the following command:

    vcpkg install protobuf:x64-windows
    
  3. Wait until the installation is completed: 20210118120839-image.png

Test the dependency

  1. Create a test CMake project with the following lists (CMakeLists.txt):

    cmake_minimum_required(VERSION 3.10)
    
    # set the project name
    project(Test)
    
    # Find package
    find_package(OpenSSL REQUIRED)
    find_package(Protobuf REQUIRED)
    
  2. Run the build command (under project folder path, for my environment, it locates in C:\hdp\cmaketest):

    cmake .
    
  3. The output looks like the following:

    C:\hdp\cmaketest>cmake .
    -- Building for: Visual Studio 14 2015
    -- Selecting Windows SDK version  to target Windows 10.0.18363.
    -- The C compiler identification is MSVC 19.0.24215.1
    -- The CXX compiler identification is MSVC 19.0.24215.1
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/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 (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe - skipped
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Found OpenSSL: C:/dev/vcpkg/installed/x64-windows/lib/libcrypto.lib (found version "1.1.1i")
    -- Found Protobuf: C:/dev/vcpkg/installed/x64-windows/lib/libprotobuf.lib (found version "3.14.0")
    -- Configuring done
    -- Generating done
    -- Build files have been written to: C:/hdp/cmaketest
    
c&cpp

Join the Discussion

View or add your thoughts below

Comments