15

I am trying to run the example given in protobuf repo here, the c++ version. I have successfully installed the library and am able to run the Makefile. But on running the CMakeLists.txt, I get this error:

CMake Error at CMakeLists.txt:9 (find_package):
  Could not find a package configuration file provided by "protobuf" with any
  of the following names:

    protobufConfig.cmake
    protobuf-config.cmake

  Add the installation prefix of "protobuf" to CMAKE_PREFIX_PATH or set
  "protobuf_DIR" to a directory containing one of the above files.  If
  "protobuf" provides a separate development package or SDK, be sure it has
  been installed.


-- Configuring incomplete, errors occurred!
See also "/home/cortana/Projects/CppProjects/proto/build/CMakeFiles/CMakeOutput.log".
See also "/home/cortana/Projects/CppProjects/proto/build/CMakeFiles/CMakeError.log".

I have updated my LD_LIBRARY_PATH but this error is still there. How do I remove this error?

EDIT:
CMakeLists.txt:

# Minimum CMake required
cmake_minimum_required(VERSION 2.8.12)

# Project
project(protobuf-examples)

include(FindProtobuf)
# Find required protobuf package
find_package(protobuf CONFIG REQUIRED)

if(protobuf_VERBOSE)
  message(STATUS "Using Protocol Buffers ${Protobuf_VERSION}")
endif()

set(CMAKE_INCLUDE_CURRENT_DIR TRUE)

set(CMAKE_PREFIX_PATH
    ${CMAKE_PREFIX_PATH}
    ${THIRDPARTY_DIR}/protobuf-3.1.0
)

include_directories(${ProtobufIncludePath})

# http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
if(MSVC AND protobuf_MSVC_STATIC_RUNTIME)
  foreach(flag_var
      CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
      CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
    if(${flag_var} MATCHES "/MD")
      string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
    endif(${flag_var} MATCHES "/MD")
  endforeach()
endif()

foreach(example add_person list_people)
  set(${example}_SRCS ${example}.cc)
  set(${example}_PROTOS addressbook.proto)

  #Code Generation
  if(protobuf_MODULE_COMPATIBLE) #Legacy Support
    protobuf_generate_cpp(${example}_PROTO_SRCS ${example}_PROTO_HDRS ${${example}_PROTOS})
    list(APPEND ${example}_SRCS ${${example}_PROTO_SRCS} ${${example}_PROTO_HDRS})
  else()

    foreach(proto_file ${${example}_PROTOS})
      get_filename_component(proto_file_abs ${proto_file} ABSOLUTE)
      get_filename_component(basename ${proto_file} NAME_WE)
      set(generated_files ${basename}.pb.cc ${basename}.pb.h)
      list(APPEND ${example}_SRCS ${generated_files})

      add_custom_command(
        OUTPUT ${generated_files}
        COMMAND protobuf::protoc
        ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR} -I ${CMAKE_CURRENT_SOURCE_DIR} ${proto_file_abs}
        COMMENT "Generating ${generated_files} from ${proto_file}"
        VERBATIM
      )
    endforeach()
  endif()

  #Executable setup
  set(executable_name ${example}_cpp)
  add_executable(${executable_name} ${${example}_SRCS} ${${example}_PROTOS})
  if(protobuf_MODULE_COMPATIBLE) #Legacy mode
    target_include_directories(${executable_name} PUBLIC ${PROTOBUF_INCLUDE_DIRS})
    target_link_libraries(${executable_name} ${PROTOBUF_LIBRARIES})
  else()
    target_link_libraries(${executable_name} protobuf::libprotobuf)
  endif()

endforeach()

EDIT 2:
After trying for 2 hours, I couldn't fix the CMakeLists.txt provided by google examples. I wrote this basic one and it works for me:

PROJECT(protopuff)
CMAKE_MINIMUM_REQUIRED (VERSION 3.5)
SET(CMAKE_CXX_FLAGS "-g -Wall -Werror -std=c++11")

INCLUDE(FindProtobuf)
FIND_PACKAGE(Protobuf REQUIRED)
INCLUDE_DIRECTORIES(${PROTOBUF_INCLUDE_DIR})
PROTOBUF_GENERATE_CPP(PROTO_SRC PROTO_HEADER addressbook.proto)
ADD_LIBRARY(proto ${PROTO_HEADER} ${PROTO_SRC})


INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
ADD_EXECUTABLE(${CMAKE_PROJECT_NAME}_add add_person.cc)
ADD_EXECUTABLE(${CMAKE_PROJECT_NAME}_list list_people.cc)
TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME}_add proto ${PROTOBUF_LIBRARY})
TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME}_list proto ${PROTOBUF_LIBRARY})
ifyalciner
  • 1,190
  • 1
  • 10
  • 22
Kapil Gupta
  • 7,091
  • 5
  • 16
  • 25
  • What version of `CMake` are you using ? – pSoLT Jan 10 '17 at 16:37
  • `cmake_minimum_required(VERSION 2.8.12)`. I have `cmake 3.5.1` installed. – Kapil Gupta Jan 10 '17 at 16:38
  • @JohnM. do you have multiple CMake versions installed (usually discouraged)? Make sure you're picking the right one – Marco A. Jan 10 '17 at 16:39
  • @MarcoA. No, I have just the one. – Kapil Gupta Jan 10 '17 at 16:40
  • Have you tried what the error message says. I mean setting `protobuf_DIR` to the location you installed protobuf in ccmake or cmake-gui or via the command line. – drescherjm Jan 10 '17 at 16:40
  • Yes, I tried adding CMAKE_PREFIX_PATH but that didn't work. – Kapil Gupta Jan 10 '17 at 16:42
  • Can you show us your `CMakeLists.txt`? – pSoLT Jan 10 '17 at 16:42
  • @drescherjm I pointed my `LD_LIBRARY_PATH` to that location. Shouldn't that help? Sorry, but I dont know a lot about cmake. – Kapil Gupta Jan 10 '17 at 16:45
  • Where are you setting `THIRDPARTY_DIR`? Also your setting of `CMAKE_PREFIX_PATH` is too late. You must do that before the `find_package` – drescherjm Jan 10 '17 at 16:50
  • I haven't setup `THIRDPARTY_DIR`. What should it be pointing to? I have moved the CMAKE_PREFIX_PATH code to below `project(protobuf-examples)` line – Kapil Gupta Jan 10 '17 at 17:03
  • ***What should it be pointing to?*** The folder containing `protobuf-3.1.0` – drescherjm Jan 10 '17 at 17:20
  • Is there a solution where I don't have to do that considering I am not distributing protobuf with my project and the folder containing protobuf might not be same for everyone? Also I did this : `set(${THIRDPARTY_DIR} /home/cortana/Libraries)` but am getting same error. `protobuf-3.1.0` is in `/home/cortana/Libraries/` – Kapil Gupta Jan 10 '17 at 17:23
  • ***Is there a solution where I don't have to do that considering I am not distributing protobuf with my project and the folder containing protobuf might not be same for everyone?*** That is what find_package is supposed to do. The user is supposed to set `protobuf_DIR` in cmake-gui or ccmake when configuring with cmake to tell CMake where protobuf is installed when it is not in a standard location. – drescherjm Jan 10 '17 at 17:47
  • I didn't change the standard location though. Installation is done in `/usr/local/lib` – Kapil Gupta Jan 10 '17 at 17:48
  • I would search your system for the `protobufConfig.cmake` or `protobuf-config.cmake` files. Are they in `/home/cortana/Libraries/protobuf-3.1.0` ? – drescherjm Jan 10 '17 at 17:49
  • There is a file `protobuf-config.cmake.in` in `/home/cortana/Libraries/protobuf-3.1.0/cmake/` but not the 2 files mentioned in error – Kapil Gupta Jan 10 '17 at 17:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/132836/discussion-between-john-m-and-drescherjm). – Kapil Gupta Jan 10 '17 at 17:52
  • GREAT! INCLUDE(FindProtobuf) just works – YNX Nov 26 '19 at 15:10
  • Is this problem resolved? I am observing the same error using ndk toolchain for my compilation. I do not find ProtobufConfig.cmake or protobuf-config.cmake files either, but i have pkgconfig/protobuf.pc and pkgconfig/protobuf-lite.pc files – Ashkanxy Nov 28 '22 at 23:28

3 Answers3

12

Your problem is here:

find_package(protobuf CONFIG REQUIRED)

The name should start with uppercase: Protobuf. And that is the reason why your version is working; because in there, you have used correct case (last code snippet line 6):

find_package(Protobuf REQUIRED)

Here cmake documentation for find_package

The command searches for a file called <name>Config.cmake or <lower-case-name>-config.cmake for each name specified.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
ifyalciner
  • 1,190
  • 1
  • 10
  • 22
0

in this thread fraser solved the problem but if you need to develop according to protobuf CMake config and find_package command in CMake for finding protobuf libraries. your protobuf library must be compiled with CMake and do not use configure routine . after compile protobuf with CMake , a config file named protobuf-config.cmake will be generated into the prefix/lib/CMake/protobuf directory.

SajadBlog
  • 514
  • 2
  • 12
0

The CmakeList.txt that is provided by the OP works on Linux but it does NOT work on Windows.

There is a way to make the actual CMakeList.txt work without any changes. The problem is that it requires the CONFIG parameter and that part is not documented anywhere. We need to provide the path to that config using -Dprotobuf_DIR parameter while generating the project.

On Windows, wherever you have installed protobuf, it will have bin, cmake, include, lib folders. We need to give the path of this cmake folder as an argument, like following:

cmake -G "Visual Studio 16 2019" -A x64 -B _build2 -Dprotobuf_DIR=C:/protobuf/install/cmake

This will build a solution file in the current directory.

mradul dubey
  • 65
  • 1
  • 11