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

来源:互联网 发布:写歌的软件 编辑:程序博客网 时间:2024/06/06 04:22

在用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后面添加一行,下面三种写法任选一行就可以

# 写法1caffe_option(protobuf_MODULE_COMPATIBLE "Make the protobuf-config.cmake compatible with the module mode" ON)# 写法2set (protobuf_MODULE_COMPATIBLE on CACHE BOOL "CMake build-in FindProtobuf.cmake module compatible" )# 写法3option(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-compilerif(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 optionsinclude("${CMAKE_CURRENT_LIST_DIR}/protobuf-options.cmake")# Depend packages# Imported targetsinclude("${CMAKE_CURRENT_LIST_DIR}/protobuf-targets.cmake")# CMake FindProtobuf module compatible fileif(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

参考资料

《Using find_package(Protobuf) with 3.0.0》
《imported-targets》

阅读全文
0 0