CMake 编译 Qt 安装第三方 libqgit2 扩展

虽然非常不愿意踏上编译这条不归路, 谁叫我爱折腾跨平台呢.
之前一直在搞 Cordova, 不过现在彻底对 H5 跨平台绝望了, 这里不做过多说明了, 可能以后会单独拿出来说道说道.
该扯得蛋也扯完了, ok, 进入正题, 编译 Qt 扩展, 坑坑坑…

这里我以编译libqgit2为例说明

下载安装相关依赖

download.qt.io 下载 qt-opensource-mac-x64-clang-5.7.0.dmg 安装包
anselmolsm/libqgit2 下载libqgit2源码
libgit2/libgit2/releases 下载 libgit2 v0.23.0源码

安装 Qt

傻瓜式安装 Pass…

Ps: 不要忘记了安装目录, 要不就要悲剧了, 不过你不可能这么蠢的对不…

编译准备

先列出几个遇到的错误, 编译最坑的莫过于各种依赖了, 而且安装方式还不一样

Building with Qt4 support

1
2
3
4
5
6
7
8
9
-- Building with Qt4 support
CMake Error at /usr/local/Cellar/cmake/3.6.3/share/cmake/Modules/FindQt4.cmake:1328 (message):
Found unsuitable Qt version "" from NOTFOUND, this code requires Qt 4.x
Call Stack (most recent call first):
CMakeLists.txt:57 (find_package)


-- Configuring incomplete, errors occurred!
See also "/Users/MakeHui/Downloads/libqgit2-master/build/CMakeFiles/CMakeOutput.log".

错误原因: 是找不到 Qt 库
解决方法: 需要指定 Qt 库的路径

No libgit2 found

1
2
3
FATAL_ERROR "No libgit2 found!
Install the libgit2 development files to your system.
Alternatively define LIBGIT2_SRC_DIR to point to a libgit2 source directory.

错误原因: 找不到 libgit2 库
解决方法: 有两种解决方法

  1. 安装libgit2库, 但是千万别brew install libgit2这样安装(这个非常坑爹, 差点搞的但都碎了), 原因是这样安装的一定是最新版, 但是libqgit2这个库不支持最新版本的libgit2, 够囧吧(主要还是没什么人维护了, 库原地址: https://github.com/KDE/libqgit2 有能力的可以去贡献代码)
  2. 指定 LIBGIT2_SRC_DIR 参数, 也就是指定libgit2源代码的位置, 直接一并编译安装, 或者你先去手动编译安装一个低版本的libgit2库同样没问题, 这里我推荐直接包含在libqgit2中编译

Could NOT find Doxygen

1
Could NOT find Doxygen (missing:  DOXYGEN_EXECUTABLE)

这个错误不是很懂什么意思, 不过直接brew install doxygen完事
http://stackoverflow.com/questions/29846214/could-not-find-doxygen-missing-doxygen-executable-in-mac-os-x

修改 CMakeLists.txt 文件

添加下面两行

1
2
set (CMAKE_PREFIX_PATH "/Applications/Qt/5.7/clang_64")         # Qt 库的路径
set (LIBGIT2_SRC_DIR "/Users/MakeHui/Downloads/libgit2-0.23.0") # libgit2 源码的路径

编译

上面的准备工作完成后执行下面的命令开始编译
Ps: 先切换到libqgit2源文件根目录

1
2
3
4
5
6
7
8
# Building (out of source build):
$ > mkdir build && cd build
$ > cmake ..
$ > cmake --build .
# Testing: 可不检查
$ > ctest -V
# Install:
$ > cmake --build . --target install

默认安装路径: /usr/local

使用

  1. 创建项目Libqgit2Test
  2. 打开Libqgit2Test.pro
  3. 右键 -> Add Library… -> External library -> continue -> 选择库安装路径 -> continue
  4. 生成代码如下:

    1
    2
    3
    4
    5
    6
    win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../../usr/local/lib/release/ -lqgit2
    else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../../usr/local/lib/debug/ -lqgit2
    else:unix: LIBS += -L$$PWD/../../../../../usr/local/lib/ -lqgit2

    INCLUDEPATH += $$PWD/../../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../../usr/local/include
  5. clone远程仓库

    1
    2
    3
    4
    5
    6
    7
    auto repository = new LibQGit2::Repository();
    try {
    repository->clone("http://git.famishare.cn/MakeHui/test2.git", "/Users/MakeHui/Desktop/test3");
    } catch (const LibQGit2::Exception& e) {
    qDebug() << e.message();
    }
    qDebug() << repository->listReferences();

参考资料

http://doc.qt.io/qt-5/cmake-manual.html
http://blog.claves.me/2015/04/29/cmake/
http://www.cnblogs.com/listensong/p/4316944.html
https://svn.osgeo.org/ossim/trunk/ossim_package_support/cmake/CMakeModules/FindQt5.cmake
http://stackoverflow.com/questions/15639781/how-to-find-qt5-cmake-module-on-windows
http://www.cppblog.com/Error/archive/2013/04/13/199407.aspx
http://blog.csdn.net/u010598445/article/details/46729705
http://stackoverflow.com/questions/29846214/could-not-find-doxygen-missing-doxygen-executable-in-mac-os-x
http://stackoverflow.com/questions/33728905/qt-creator-project-error-xcode-not-set-up-properly-you-may-need-to-confirm-t