qt配置tensorflow + opencv 提示protoc版本错误
使用qt配置自己编译的tensorflow,在.pro
文件下添加如下配置:
INCLUDEPATH += /usr/local/opencv3/include \
/usr/local/include \
/home/zy/.cache/bazel/_bazel_zy/69673882bdf22f44b89ee446a9525e4a/external/eigen_archive/Eigen \ #Eigen library;
/home/zy/.cache/bazel/_bazel_zy/69673882bdf22f44b89ee446a9525e4a/external/eigen_archive \
/home/zy/.cache/bazel/_bazel_zy/69673882bdf22f44b89ee446a9525e4a/external/nsync/public \
/home/zy/Downloads/tensorflow-1.4.0/bazel-tensorflow-1.4.0 \
/home/zy/Downloads/tensorflow-1.4.0/bazel-genfiles
LIBS += -L/home/zy/Downloads/tensorflow-1.4.0/bazel-bin/tensorflow \
-L/usr/local/opencv3/lib/ \
-L/usr/local/lib \
/usr/local/lib/libprotobuf.a \
/home/zy/Downloads/tensorflow-1.4.0/bazel-bin/tensorflow/libtensorflow_cc.so \
/home/zy/Downloads/tensorflow-1.4.0/bazel-bin/tensorflow/libtensorflow_framework.so \
/usr/local/opencv3/lib/libopencv_core.so \
/usr/local/opencv3/lib/libopencv_highgui.so \
/usr/local/opencv3/lib/libopencv_imgproc.so \
/usr/local/opencv3/lib/libopencv_imgcodecs.so
运行包含tensorflow头文件的程序,出现如下错误:
[libprotobuf FATAL external/protobuf_archive/src/google/protobuf/stubs/common.cc:79] This program was compiled against version 2.6.1 of the Protocal Buffer runtime library, which is not compatible with the installed version(3.4.0). Contact the program author for an update. If you compiled the program yourself, make sure that your headers are from the same version of Protocal Buffers as your link-time library. (Version verification failed in "/build/mir-08_xaj/mir-0.26.3+16.044.20170605/obj-x86_64-linux-gnu/src/protobuf/mir_protobuf.pb.cc".)terminate called after throwing an instance of 'google::protobuf::FatalException' what(): This program was compiled against version 2.6.1 of the Protocal Buffer runtime library, which is not compatible with the installed version (3.4.0). Contact the program author for an update. If you compiled the program yourself, make sure that your headers are from the same version of the Protocal Buffers as your link-time library. (Version verification failed in "/build/mir-08_xaj/mir-0.26.3+16.04.20170605/obj-x86_64-linux-gun/src/protobuf/mir_protobuf.pb.cc".)
问题分析
系统的protoc位置在/usr/local/bin
, 使用protoc --version
输出的版本号为:libprotoc 3.4.0
.
另有一个protoc在/usr/bin/
,使用protoc --version
输出的版本号为:libprotoc 2.6.1
.
在/usr/local/bin位置将protoc重命名未protoc.3.4.0,然后建立指向/usr/bin的protoc的软连接(保证系统中protoc都使用2.6.1的版本),重新运行程序,还是报以上错误。分析估计是:tensorflow编译动态链接库的过程中使用了3.4.0版本的protoc,但实际上tensorflow要求使用2.6.1版本,所以不能用。
解决
重新编译tensorflow动态链接库,使用命令:
$./configure
$bazel build -c opt --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-mfpmath=both --copt=-msse4.2 --config=cuda tensorflow:libtensorflow_cc.so
推测:bazel编译tensorflow过程中使用了protoc,上面的步骤保证了使用到的protoc是和tensorflow工程要求的版本一致(一杯茶,慢慢等待结果……)
测试结果
在tensorflow的源码目录下,获得bazel-bin/tensorflow/libtensorflow_cc.so文件,
遇到错误: error: unsupported/Eigen/CXX11/Tensor: No such file or directory
;安装eigen3,并将安装目录放置到工程链接库文件INCLUDEPATH += /usr/include/eigen3 \
;
error: nsync_cv.h: No such file or directory
添加~/.cache
目录下的动态链接库。
还是不行,郁闷,报上面那个错误。。。
重来
发现问题:删除工程中对opencv的引入,则可以正常运行。推测是opencv对protoc 2.6.1有依赖,看https://stackoverflow.com/questions/41964212/conflict-protobuf-version-when-using-opencv-and-tensorflow-c 中也说,因为安装opencv时使用GTK3,而GTK3依赖于protobuf 2.6.1,所以,在同一个cpp里面同时使用opencv和tensorflow会报protoc版本错误的问题(可是我已经把系统的protoc装成2.6.1了啊???难道是tensorflow自己去下载使用了protoc 3.4.0???而且,其实我没有找到GTK3会依赖protoc2.6.1的证据。,我已经非常混乱了。。。)
重装opencv
打开QT选项,希望它不会和GTK 3、protoc2.6.1扯上关系。。。
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local/opencv3 \
-D INSTALL_C_EXAMPLES=OFF \
-D WITH_TBB=ON \
-D WITH_V4L=ON \
-D WITH_QT=ON \
-D WITH_GTK=ON \
-D WITH_OPENGL=ON \
-D BUILD_EXAMPLES=OFF ..
编译过程非常漫长,特别是在最后97%的时候,'Built target opencv_perf_stitching'大约会停止15分钟,头一次我编译的时候以为中间出错了,一看cpu占用99%,于是就ctrl+c了,提示:一定要耐心,BE PATIENT!
重新安装opencv后,在工程中同时引入opencv和tensorflow成功,证明了我之前的推断是正确的,给自己点个赞!