Can't use protobuf in cmakelists.txt

14,983

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.

Share:
14,983

Related videos on Youtube

Kapil Gupta
Author by

Kapil Gupta

Updated on September 16, 2022

Comments

  • Kapil Gupta
    Kapil Gupta over 1 year

    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})
    
  • Jake Sylvestre
    Jake Sylvestre over 4 years
    Why is the config not required?
  • ifyalciner
    ifyalciner over 4 years
    I have just copied the line from where it is working in the question.(last code section 6th line). It is his decision not to use.
  • Jake Sylvestre
    Jake Sylvestre over 4 years
    Got it. Yeah, it's interesting all the example documentation has Config Required, but no actual config files