Caffe: Could not find PROTOBUF Compiler(Profobuf 3.0 above)

82 篇文章 18 订阅
26 篇文章 1 订阅

在用cmake生成Caffe工程文件的时候,如果你使用Protobuf 3.0以上的版本,cmake可能会产生如下的报错:

CMake Error at cmake/ProtoBuf.cmake:18 (message):
Could not find PROTOBUF Compiler
Call Stack (most recent call first):
cmake/Dependencies.cmake:48 (include)
CMakeLists.txt:81 (include)

#解决办法

通过设置protobuf_MODULE_COMPATIBLE=on指定打开Protobuf的兼容模式。
注意区分大小写

设置protobuf_MODULE_COMPATIBLE=on也有两种办法:
##命令行添加
cmake生成Makefile时命令行添加-Dprotobuf_MODULE_COMPATIBLE=on,指定打开Protobuf兼容模式,类似下面的命令行:

d:\caffe> cmake -G "Visual Studio 12 2013 Win64"  . -Dprotobuf_MODULE_COMPATIBLE=on 

这个办法的好处是不用修改caffe的代码

修改CMakeLists.txt

也可以在直接修改Caffe根目录下的CMakeLists.txt
打开CMakeLists.txt,查找是否有类型下面这行代码

caffe_option(protobuf_MODULE_COMPATIBLE "Make the protobuf-config.cmake compatible with the module mode" ON IF MSVC)

如果有,直接把后面的 IF MSVC删除,强制将protobuf_MODULE_COMPATIBLE 置为ON。

如果没有,就在# ---[ Options后面添加一行,下面三种写法任选一行就可以

# 写法1
caffe_option(protobuf_MODULE_COMPATIBLE "Make the protobuf-config.cmake compatible with the module mode" ON)
# 写法2
set (protobuf_MODULE_COMPATIBLE on CACHE BOOL "CMake build-in FindProtobuf.cmake module compatible" )
# 写法3
option(protobuf_MODULE_COMPATIBLE "CMake build-in FindProtobuf.cmake module compatible" ON)

""包含的字符串只是描述信息,内容是什么不重要

问题溯源

产生这个问题的根本原因是Protobuf3.0以后的版本的cmake脚本默认不向下兼容。需要设置protobuf_MODULE_COMPATIBLE=on开启兼容模式。
进一步我们分析caffe_folder/cmake/ProtuBuf.cmake分析,如下代码,就能看到"Could not find PROTOBUF Compiler"这个错误信息的输出位置:

find_package( Protobuf REQUIRED NO_MODULE)
set(PROTOBUF_INCLUDE_DIR ${PROTOBUF_INCLUDE_DIRS})
list(APPEND Caffe_INCLUDE_DIRS PUBLIC ${PROTOBUF_INCLUDE_DIR})
list(APPEND Caffe_LINKER_LIBS PUBLIC ${PROTOBUF_LIBRARIES})


# As of Ubuntu 14.04 protoc is no longer a part of libprotobuf-dev package
# and should be installed separately as in: sudo apt-get install protobuf-compiler
if(EXISTS ${PROTOBUF_PROTOC_EXECUTABLE})
  message(STATUS "Found PROTOBUF Compiler: ${PROTOBUF_PROTOC_EXECUTABLE}")
else()
  # 没有找到 protoc(.exe)可执行文件,就报错
  message(FATAL_ERROR "Could not find PROTOBUF Compiler")
endif()

为什么PROTOBUF_PROTOC_EXECUTABLE会没有定义呢?
进一步我们追踪到protobuf_installation/cmake/protobuf-config.cmake,就能找到protobuf_MODULE_COMPATIBLE这个变量真正起作用的位置(protobuf_installation是你的ProtuBuf安装的位置):

# User options
include("${CMAKE_CURRENT_LIST_DIR}/protobuf-options.cmake")

# Depend packages


# Imported targets
include("${CMAKE_CURRENT_LIST_DIR}/protobuf-targets.cmake")

# CMake FindProtobuf module compatible file
if(protobuf_MODULE_COMPATIBLE)
  # 加载 兼容旧版本的脚本,生成MODULE模式下的变量PROTOBUF_INCLUDE_DIRS PROTOBUF_LIBRARIES...等等
  # 参见 https://cmake.org/cmake/help/v3.8/module/FindProtobuf.html
  # 也就是protobuf-module.cmake 这个脚本才会定义 Protobuf_PROTOC_EXECUTABLE 这个变量
  include("${CMAKE_CURRENT_LIST_DIR}/protobuf-module.cmake")
endif()

延伸阅读 : Imported Target

那么你也许会问,如果不设置protobuf_MODULE_COMPATIBLE=on要如何使用ProtoBuf 3.0的库呢?
Protobuf 3.0以后的版本采用imported target这种新的形式来为应用软件提供library和include dir.
以libprotobuf-lite库为例说明,
打开protobuf_installation/cmake/protobuf-targets.cmake,你会发现文件中有如下的代码

add_library(protobuf::libprotobuf-lite STATIC IMPORTED)
set_target_properties(protobuf::libprotobuf-lite PROPERTIES
  # 指定 include 文件夹
  INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
)

打开protobuf_installation/cmake/protobuf-targets-release.cmake,进一步还能找到如下的代码

# Import target "protobuf::libprotobuf-lite" for configuration "release"
set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(protobuf::libprotobuf-lite PROPERTIES
  IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX"
  # 指定 library 的位置
  IMPORTED_LOCATION_RELEASE "${_IMPORT_PREFIX}/lib/libprotobuf-lite.lib"
  )

这两段代码定义了一个IMPORTED 类型的target,名字叫 protobuf::libprotobuf-lite,target的属性中定义了include文件夹的位置以及库的位置。

那么应用程序要使用libprotobuf-lite库,只需像以前一样调用 target_link_libraries将protobuf::libprotobuf-lite添加 到依赖库列表中就可以了。

target_link_libraries( your_target,protobuf::libprotobuf-lite)

也许你会问那还需要调用include_directories添加protobuf的include_dir么?
不需要了,your_target指定连接一个imported target时,会自动把imported target的INTERFACE_INCLUDE_DIRECTORIES 属性指定的include文件夹加到your_target的include文件夹列表中的。

关于imported-target的详细说明,参见 [imported-targets][1]

参考资料

《Using find_package(Protobuf) with 3.0.0》
[《imported-targets》][1]
[1]:https://cmake.org/cmake/help/v3.8/manual/cmake-buildsystem.7.html#imported-targets

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值