如何用 gtk 2.x 而不是 gtk3.x 编译 opencv

How to compile opencv with gtk 2.x rather than gtk3.x

我在 ubuntu 16.04 中使用 tensorflow 1.4 和 qt。问题是当我同时包含 tensorflow(版本:1.4)和 opencv(版本:3.2.0)时,protobuf 发生冲突。报错信息如下:

[libprotobuf FATAL external/protobuf_archive/src/google/protobuf/stubs/common.cc:79] This program was compiled against version 2.6.1 of the Protocol 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 Protocol Buffers as your link-time library. (Version verification failed in "/build/mir-O8_xaj/mir-0.26.3+16.04.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 Protocol 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 Protocol Buffers as your link-time library. (Version verification failed in "/build/mir-O8_xaj/mir-0.26.3+16.04.20170605/obj-x86_64-linux-gnu/src/protobuf/mir_protobuf.pb.cc".)

我从某处了解到opencv with gtk-3.x会使用protobuf 2.6.1导致以上问题,安装opencv with gtk-2.x不会依赖protobuf。部分opencv cmake msg如下,cmake use gtk 3.x automatically.

GUI: -- QT: NO -- GTK+ 3.x: YES (ver 3.18.9) -- GThread : YES (ver 2.48.2) -- GtkGlExt: NO -- OpenGL support: NO -- VTK support: NO

而系统gtk信息如下:

$ pkg-config --modversion gtk+-2.0 $ 2.24.30

这是 problem.How 我可以用 gtk 2.x 使用 cmake?

安装 opencv

对我来说,你只需要安装所需的依赖项,即protobuf兼容2.6.1的版本。

packages.ubuntu.com 的快速搜索显示包 libprotobuf9v5 提供 protocol buffers C++ library 并且版本为 2.6.1。只需尝试安装它。

顺便说一句,回到过去使用像 GTK+2 这样的过时技术是一个糟糕的举动。如果您真的想更改您的工具包,只需在 cmake 调用中定义正确的选项即可选择它。在顶层 CMakeLists.txt 中,可以轻松找到 WITH_GTKWITH_GTK2WITH_QT 选项。在调用 cmake 或使用 cmake-gui 的命令行上启用它们 如果您自己已经在使用 Qt,只需使用 Qt 后端而不是旧的 GTK 后端进行重建。