'Finding Protobuf package with CMake

I cannot seem to get CMake to consistently find my protobuf package. I would like to include the protobuf source and build it as part of my project. I'm using protobuf 3.15.8 and protobuf-c 1.3.3 (useful as a control example).

This is the directory setup for the example:

- CMakeLists.txt
- deps/
       - CMakeLists.txt
       - protobuf/            # The protobuf 3.15.8 repository
       - protobuf-c/          # The protobuf-c 1.3.3 repository
- target/
         - CMakeLists.txt

Root CMakeLists.txt:

cmake_minimum_required(VERSION 3.15)
project(PROTOBUF_IMPORT_FAIL)
add_subdirectory(deps)
add_subdirectory(target)

deps/CMakeLists.txt:

add_subdirectory(protobuf/cmake)
set(Protobuf_LIBRARIES "${CMAKE_CURRENT_BINARY_DIR}/protobuf/cmake/lib")
set(Protobuf_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/protobuf/src")
set(Protobuf_PROTOC_LIBRARY "${CMAKE_CURRENT_BINARY_DIR}/protobuf/cmake/libprotoc.a")
add_subdirectory(protobuf-c/build-cmake)

target/CMakeLists.txt:

set(Protobuf_LIBRARIES "${CMAKE_CURRENT_BINARY_DIR}/../deps/protobuf/cmake/lib")
set(Protobuf_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}../deps/protobuf/src")
message("This is the problem!")
FIND_PACKAGE(Protobuf REQUIRED)

Once everything is set up I can then try to run cmake:

mkdir build
cd build
cmake ..

Unfortunately, that eventually gives me the following error:

CMake Error at /usr/local/Cellar/cmake/3.23.0/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find Protobuf (missing: Protobuf_INCLUDE_DIR)
Call Stack (most recent call first):
  /usr/local/Cellar/cmake/3.23.0/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
  /usr/local/Cellar/cmake/3.23.0/share/cmake/Modules/FindProtobuf.cmake:650 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
  target/CMakeLists.txt:7 (FIND_PACKAGE)

In case it's helpful, here's some of the environment information:

$ uname -a
Darwin coop-mac 21.4.0 Darwin Kernel Version 21.4.0: Fri Mar 18 00:45:05 PDT 2022; root:xnu-8020.101.4~15/RELEASE_X86_64 x86_64
$ cmake --version
cmake version 3.23.0

protobuf-c also tries to find the protobuf package, but it's actually successful. Why does searching for it from the target not find it and how do I fix it?



Solution 1:[1]

find_package is used to find installed libraries. An installed library is normally found by a script shipped with cmake (so called find-modules) or because the library provides a config script (like protobuf-config.cmake or profobuf-targets.cmake). The latter is generated once the library is build and installed. This is possibly why your second configure step is successfull.

You don't want to link against the installed library, but build the library by yourself. So all you need to do is add the subfolders using add_subdirectory (don't add the cmake subfolder, it's deprecated according to protobuf). After that you can link protobuf against your target using target_link_libraries without using find_package.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1