首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法在cmakelists.txt中使用protobuf

无法在cmakelists.txt中使用protobuf
EN

Stack Overflow用户
提问于 2017-01-11 00:33:49
回答 3查看 13.8K关注 0票数 11

我正在尝试运行c++版本的protobuf repo here中给出的示例。我已经成功地安装库,并且能够运行Makefile。但是在运行CMakeLists.txt时,我得到了这个错误:

代码语言:javascript
运行
复制
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".

我已经更新了我的LD_LIBRARY_PATH,但是这个错误仍然存在。如何删除此错误?

编辑:

CMakeLists.txt:

代码语言:javascript
运行
复制
# 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()

编辑2:

尝试了2个小时后,我无法修复google examples提供的CMakeLists.txt。我写了这个基本的代码,它对我很有效:

代码语言:javascript
运行
复制
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})
EN

回答 3

Stack Overflow用户

发布于 2017-11-17 05:00:26

你的问题在这里:

代码语言:javascript
运行
复制
find_package(protobuf CONFIG REQUIRED)

名称应以大写字母开头:Protobuf。这就是你的版本能够工作的原因;因为在那里,你使用了正确的大小写(最后的代码片段第6行):

代码语言:javascript
运行
复制
find_package(Protobuf REQUIRED)

Here cmake documentation for find_package

对于指定的每个名称,该命令都会搜索名为<name>Config.cmake<lower-case-name>-config.cmake的文件。

票数 9
EN

Stack Overflow用户

发布于 2021-09-06 07:33:17

this线程中,弗雷泽解决了这个问题,但如果你需要根据protobuf、CMake配置和CMake中的find_package命令进行开发,以找到protobuf库。您的protobuf库必须使用CMake编译,并且不要使用配置例程。使用CMake编译protobuf后,将在前缀/lib/CMake/protobuf目录中生成一个名为protobuf-config.cmake的配置文件。

票数 0
EN

Stack Overflow用户

发布于 2021-11-17 07:29:00

OP提供的CmakeList.txt可以在Linux上运行,但不能在Windows上运行。

有一种方法可以让实际的CMakeList.txt在不做任何更改的情况下工作。问题是它需要CONFIG参数,并且没有任何地方记录该部分。在生成项目时,我们需要使用-Dprotobuf_DIR参数提供该配置的路径。

在Windows上,无论你在哪里安装protobuf,它都会有bincmakeincludelib文件夹。我们需要提供此cmake文件夹的路径作为参数,如下所示:

代码语言:javascript
运行
复制
cmake -G "Visual Studio 16 2019" -A x64 -B _build2 -Dprotobuf_DIR=C:/protobuf/install/cmake

这将在当前目录中构建一个解决方案文件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41573702

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档