Shawn
Shawn

Reputation: 21

CMAKE cannot find the protobuf package

I want to use the protobuf package in my C++ project, and I have downloaded the binary executable of protoc from github, on the WIndows platform. I according to the contents of the https://github.com/protocolbuffers/protobuf/blob/main/src/README.md, The protobuf package is installed by running vcpkg install protobuf:x64-windows. But I used the CMake find_package directive in vs2022 to look up the protobuf package and couldn't find it.

I tried to re-download protobuf with vcpkg install protobuf:x64-windows and it said it was installed.

download protobuf

I added the configuration for CMAKE_TOOLCHAIN_FILE to CMakePresets.json. CMakePresets.json settings

I tried to clear the cache and reconfigure, but CMAKE still couldn't find protobuf packages.

[CMake] -- The C compiler identification is MSVC 19.40.33811.0
1> [CMake] -- The CXX compiler identification is MSVC 19.40.33811.0
1> [CMake] -- Detecting C compiler ABI info
1> [CMake] -- Detecting C compiler ABI info - done
1> [CMake] -- Check for working C compiler: D:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.40.33807/bin/Hostx64/x64/cl.exe - skipped
1> [CMake] -- Detecting C compile features
1> [CMake] -- Detecting C compile features - done
1> [CMake] -- Detecting CXX compiler ABI info
1> [CMake] -- Detecting CXX compiler ABI info - done
1> [CMake] -- Check for working CXX compiler: D:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.40.33807/bin/Hostx64/x64/cl.exe - skipped
1> [CMake] -- Detecting CXX compile features
1> [CMake] -- Detecting CXX compile features - done
1> [CMake] CMake Error at D:/Program Files/Microsoft Visual Studio/2022/Community/VC/vcpkg/scripts/buildsystems/vcpkg.cmake:859 (_find_package):
1> [CMake]   Could not find a package configuration file provided by "protobuf" with any
1> [CMake]   of the following names:
1> [CMake] 
1> [CMake]     protobufConfig.cmake
1> [CMake]     protobuf-config.cmake
1> [CMake] 
1> [CMake]   Add the installation prefix of "protobuf" to CMAKE_PREFIX_PATH or set
1> [CMake]   "protobuf_DIR" to a directory containing one of the above files.  If
1> [CMake]   "protobuf" provides a separate development package or SDK, be sure it has
1> [CMake]   been installed.
1> [CMake] Call Stack (most recent call first):
1> [CMake]   CMakeLists.txt:14 (find_package)
1> [CMake] -- Configuring incomplete, errors occurred!

Here is the contents of my cmakelists.txt file:

cmake_minimum_required (VERSION 3.8)

if (POLICY CMP0141)
  cmake_policy(SET CMP0141 NEW)
  set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()

project ("testProtobuf")

find_package(protobuf CONFIG REQUIRED)


add_executable (testProtobuf "testProtobuf.cpp" "testProtobuf.h")

target_link_libraries(testProtobuf PRIVATE protobuf::libprotoc protobuf::libprotobuf protobuf::libprotobuf-lite)

if (CMAKE_VERSION VERSION_GREATER 3.12)
  set_property(TARGET testProtobuf PROPERTY CXX_STANDARD 20)
endif()

Upvotes: 2

Views: 189

Answers (1)

Constantinos Glynos
Constantinos Glynos

Reputation: 3186

I cannot remember how I install google's protocol buffers, but I think I also did it via vcpkg. My CMakeLists.txt looks like this:

find_package( Protobuf REQUIRED )

include( CMakePrintHelpers )
include_directories( ${PROTOBUF_INCLUDE_DIRS} )

file( TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/include/proto/ PROTO_INPUT_PATH )
file( TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/include/proto/ PROTO_OUTPUT_PATH )
file( GLOB PROTO_FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/proto/*.proto" )
foreach( proto ${PROTO_FILES})
   file( TO_NATIVE_PATH ${proto} proto_native )
   execute_process( COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --proto_path=${PROTO_INPUT_PATH} --cpp_out=${PROTO_OUTPUT_PATH} ${proto_native} )
endforeach()

cmake_print_variables(PROTO_INPUT_PATH)
cmake_print_variables(PROTO_OUTPUT_PATH)
cmake_print_variables(PROTOBUF_PROTOC_EXECUTABLE)
cmake_print_variables(PROTOBUF_LIBRARIES)

set( PROTO_LIBRARY_NAME ${PROJECT_NAME}_proto )

file( GLOB DEFS "${CMAKE_CURRENT_SOURCE_DIR}/include/proto/*.proto" )
file( GLOB HDRS "${CMAKE_CURRENT_SOURCE_DIR}/include/proto/*.pb.h" )
file( GLOB SRCS "${CMAKE_CURRENT_SOURCE_DIR}/include/proto/*.pb.cc" )
add_library( ${PROTO_LIBRARY_NAME} STATIC ${DEFS} ${HDRS} ${SRCS} )
target_link_libraries( ${PROTO_LIBRARY_NAME} PRIVATE ${PROTOBUF_LIBRARIES} )

...

target_include_directories( ${MY_LIBRARY_NAME} PUBLIC ${PROTOBUF_INCLUDE_DIRS} )

target_link_libraries( ${MY_LIBRARY_NAME} PRIVATE ${PROTO_LIBRARY_NAME} )

The include/proto/ is the directory that includes the .proto file, which is used to generate the .pb.h and .pb.cc.

Upvotes: 0

Related Questions