5

I have cmake 3.10.x and downloaded current protobuf sources 3.6.1. Using cmake I created bin directory "{PROTOBUF_SOURCE_DIR}/bin" where this library is successfully built. As the next step I would like to use this custom tree in my cmake based project. I have

set ( Protobuf_USE_STATIC_LIBS ON )

find_package( Protobuf REQUIRED )
if ( Protobuf_FOUND )
    message( STATUS "Protobuf version : ${Protobuf_VERSION}" )
    message( STATUS "Protobuf include path : ${Protobuf_INCLUDE_DIRS}" )
    message( STATUS "Protobuf libraries : ${Protobuf_LIBRARIES}" )
else()
    message( WARNING "Protobuf package not found -> specify search path via PROTOBUF_ROOT variable")
endif()

But how to specify my custom directory tree for cmake to find the necessary stuff.

If I use find_package( Protobuf REQUIRED PATHS ${PROTOBUF_ROOT}/bin/lib/cmake/protobuf ) then I see the following output from cmake :

Protobuf version : 3.6.1
Protobuf include path : 
Protobuf libraries :

How can I make cmake to find include paths, libraries and protoc compiler?

Dmitry
  • 1,912
  • 2
  • 18
  • 29
  • For hint CMake about path where protobuf is installed, you may use [CMAKE_PREFIX_PATH](https://stackoverflow.com/questions/34795816/hinting-findname-cmake-files-with-a-custom-directory/34797156#34797156) variable, like described in that my answer: https://stackoverflow.com/questions/34795816/hinting-findname-cmake-files-with-a-custom-directory/34797156#34797156. Option `PATH` is not the proper way for such hints, and it changes mode of `find_package()` (from MODULE to CONFIG), so common [documentation](https://cmake.org/cmake/help/v3.9/module/FindProtobuf.html) is not applicable. – Tsyvarev Dec 06 '18 at 12:57
  • This does not work : I added list( APPEND CMAKE_PREFIX_PATH ${PROTOBUF_ROOT}/bin ) but still see the same output – Dmitry Dec 06 '18 at 13:08
  • In any case, remove `PATH` option from the call `find_package(Protobuf)`. If this doesn't work, then you probably hint with wrong installation prefix. Normally, installation prefix has `lib/` and `include/` subdirectories, – Tsyvarev Dec 06 '18 at 13:19

2 Answers2

9

Finally I've got a solution - maybe it would save a lot of time for someone else

set ( Protobuf_USE_STATIC_LIBS ON )

include(${PROTOBUF_ROOT}/bin/lib/cmake/protobuf/protobuf-config.cmake)
include(${PROTOBUF_ROOT}/bin/lib/cmake/protobuf/protobuf-module.cmake)
include(${PROTOBUF_ROOT}/bin/lib/cmake/protobuf/protobuf-options.cmake)
include(${PROTOBUF_ROOT}/bin/lib/cmake/protobuf/protobuf-targets.cmake)

find_package( Protobuf REQUIRED HINTS ${PROTOBUF_ROOT}/bin/lib/cmake/protobuf )
if ( Protobuf_FOUND )
    message( STATUS "Protobuf version : ${Protobuf_VERSION}" )
    message( STATUS "Protobuf include path : ${Protobuf_INCLUDE_DIRS}" )
    message( STATUS "Protobuf libraries : ${Protobuf_LIBRARIES}" )
    message( STATUS "Protobuf compiler libraries : ${Protobuf_PROTOC_LIBRARIES}")
    message( STATUS "Protobuf lite libraries : ${Protobuf_LITE_LIBRARIES}")
else()
    message( WARNING "Protobuf package not found -> specify search path via PROTOBUF_ROOT variable")
endif()
Dmitry
  • 1,912
  • 2
  • 18
  • 29
1

This worked for me well pointing to my custom Windows static build:

set(Protobuf_USE_STATIC_LIBS ON)
# add parent directory containing bin/protoc.exe, cmake/protobuf-config.cmake, lib/libprotobufd.lib (or .a), etc.
list(APPEND CMAKE_PREFIX_PATH "/dir/where/protobuf/was/installed")
find_package(Protobuf REQUIRED)

Setting Protobuf_DIR or Protobuf_ROOT did NOT work for some reason.

Darien Pardinas
  • 5,910
  • 1
  • 41
  • 48