Qt-Creator编译开源项目

使用Qt开发CMake开源项目设置教程

Posted by Jannon on March 13, 2020

下载源码和环境安装

一些开源项目使用的CMake来配置和make来编译安装的,我们需要查看或者修改和调试代码需要在IDE里面进行会方便很多,接下来会说明如何使用QTCreator来开发CMake开源项目KiCad。
首先需要到KiCad官网下载好源码,根据开源代码的说明配置好开发环境,如开源项目使用说明window平台编译需要用到msys2来安装依赖环境,首先下载安装好msys2之后,运行对应命令环境,注意需要编译多少位的选择对应的位数命令行程序,如果运行的是msys2.exe命令行是没有环境的,使用该命令行会发现安装的命令都找不到,如:
camke命令未找到
应该运行对应位数的命令行程序:
MSYS2-MinGw程序
该快捷方式对应的实际程序:
MSYS2-MinGW快捷方式
打开命令行之后安装项目依赖库环境命令如下:

pacman -S base-devel \
          git \
          mingw-w64-x86_64-cmake \
          mingw-w64-x86_64-doxygen \
          mingw-w64-x86_64-gcc \
          mingw-w64-x86_64-python2 \
          mingw-w64-x86_64-pkg-config \
          mingw-w64-x86_64-swig \
          mingw-w64-x86_64-boost \
          mingw-w64-x86_64-cairo \
          mingw-w64-x86_64-glew \
          mingw-w64-x86_64-curl \
          mingw-w64-x86_64-wxPython \
          mingw-w64-x86_64-wxWidgets \
          mingw-w64-x86_64-toolchain \
          mingw-w64-x86_64-glm \
          mingw-w64-x86_64-oce \
          mingw-w64-x86_64-ngspice
cd project-source
mkdir -p build/release
mkdir build/debug               # Optional for debug build.
cd build/release
cmake -DCMAKE_BUILD_TYPE=Release \
      -G "MSYS Makefiles" \
      -DCMAKE_PREFIX_PATH=/mingw64 \
      -DCMAKE_INSTALL_PREFIX=C:/projectName/ \
      -DDEFAULT_INSTALL_PATH=C:/projectName/ \
      ../../
make install

/**
* list all installed packages:
* pacman -Q --explicit
* or pacman -Q -e

* update all local packages:
* pacman -Syu
* (S for sync ,y for refresh,u for sysupgrade)
*/

完成以上步骤就可以完成整个开源项目的编译运行环境安装和编译完成整个可运行项目。以上是通过MSYS2命令行方式配置和编译项目,如果需要更好的查看项目代码和调试修改代码,我们需要将项目导入到开发工具IDE当中去。下面就是说明如何使用Qt-Creator来编译项目。

Qt-Creator配置CMake项目

打开qt-creator,打开项目,选择项目CMakeLists.txt文件,选择构建套件,等待qt生成配置选项,如果报错如下:
compiler-failed

Starting to parse CMake project, using: "-DCMAKE_BUILD_TYPE:STRING=Debug", "-DCMAKE_CXX_COMPILER:STRING=C:/Qt/Qt5.13.1/Tools/mingw730_64/bin/g++.exe", "-DCMAKE_C_COMPILER:STRING=C:/Qt/Qt5.13.1/Tools/mingw730_64/bin/gcc.exe", "-DCMAKE_PREFIX_PATH:STRING=D:/msys64/mingw64", "-DQT_QMAKE_EXECUTABLE:STRING=C:/Qt/Qt5.13.1/5.13.1/mingw73_64/bin/qmake.exe".
The C compiler identification is GNU 7.3.0
The CXX compiler identification is GNU 7.3.0
Check for working C compiler: C:/Qt/Qt5.13.1/Tools/mingw730_64/bin/gcc.exe
Check for working C compiler: C:/Qt/Qt5.13.1/Tools/mingw730_64/bin/gcc.exe -- broken
CMake Error at C:/Program Files (x86)/CMake/share/cmake-3.14/Modules/CMakeTestCCompiler.cmake:60 (message):
  The C compiler

    "C:/Qt/Qt5.13.1/Tools/mingw730_64/bin/gcc.exe"

  is not able to compile a simple test program.

  It fails with the following output:

qt的编译套件设置的编译器报错,改为之前MSYS2安装的mingw编译器,如下图:
compiler错误
如果还是报错如下图所示:
compiler-error

Starting to parse CMake project, using: "-DCMAKE_BUILD_TYPE:STRING=Debug", "-DCMAKE_CXX_COMPILER:STRING=D:\msys64\mingw64\bin\g++.exe", "-DCMAKE_C_COMPILER:STRING=D:\msys64\mingw64\bin\gcc.exe", "-DCMAKE_PREFIX_PATH:STRING=D:/msys64/mingw64", "-DQT_QMAKE_EXECUTABLE:STRING=C:/Qt/Qt5.13.1/5.13.1/mingw73_64/bin/qmake.exe".
The C compiler identification is unknown
CMake Error at C:/Users/Top/AppData/Local/Temp/QtCreator-TkpEOe/qtc-cmake-FcjGRuRJ/CMakeFiles/3.14.0-rc1/CMakeCCompiler.cmake:1 (set):
  Syntax error in cmake code at

    C:/Users/Top/AppData/Local/Temp/QtCreator-TkpEOe/qtc-cmake-FcjGRuRJ/CMakeFiles/3.14.0-rc1/CMakeCCompiler.cmake:1

  when parsing string

    D:\msys64\mingw64\bin\gcc.exe

  Invalid escape sequence \m
Call Stack (most recent call first):
  CMakeLists.txt:33 (project)


Configuring incomplete, errors occurred!

或者报错如下:

CMake Error at C:/msys64/mingw64/share/cmake-3.12/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
  Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the
  system variable OPENSSL_ROOT_DIR (missing: OPENSSL_LIBRARIES) (found
  version "1.0.2p")
Call Stack (most recent call first):
  C:/msys64/mingw64/share/cmake-3.12/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
  CMakeModules/FindOpenSSL.cmake:326 (find_package_handle_standard_args)
  common/CMakeLists.txt:28 (find_package)

qt项目设置当中的CMake配置需要设置好DCMAKE_INSTALL_PREFIX字段,告诉CMake依赖库和编译器搜索目录,正确配置修改为:
qt-cmake-config
如果以上都设置好还是遇到报错:

Check for working C compiler: D:/msys64/mingw64/bin/gcc.exe -- broken
CMake Error at D:/msys64/mingw64/share/cmake-3.11/Modules/CMakeTestCCompiler.cmake:52 (message):
  The C compiler

    "D:/msys64/mingw64/bin/gcc.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: D:/QTProject/gerberview/gerberviews/build-gerberviews-Desktop_Qt_5_13_1_MinGW_64_bit-Debug/CMakeFiles/CMakeTmp

    Run Build Command:"C:/usr/local/bin/make.exe" "cmTC_9fd2f/fast"
    D:\QTProject\gerberview\gerberviews\build-gerberviews-Desktop_Qt_5_13_1_MinGW_64_bit-Debug\CMakeFiles\CMakeTmp/C:/usr/local/bin/make.exe  -f CMakeFiles/cmTC_9fd2f.dir/build.make CMakeFiles/cmTC_9fd2f.dir/build
    make.exe: *** [cmTC_9fd2f/fast] Signal 127

看报错信息提示报错是执行make命令出错Run Build Command:"C:/usr/local/bin/make.exe" 修改CMake配置:
qt-cmake-config
编译过程如果出现以下错误

CMake Error at C:/msys64/mingw64/share/cmake-3.12/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
Could NOT find wxWidgets (missing: wxWidgets_LIBRARIES
wxWidgets_INCLUDE_DIRS) (Required is at least version "3.0.0")

说明CMake配置设置还是不对,需要修改构建套件当中CMake generator为“MSYS Makefiles”,具体设置如下:
CMakeSetting
如果仍然报错,说明环境变量设置还是不正确,CMake找不到wxWidgets库,需要在系统环境变量添加C:\msys64\usr\bin到Path变量,即项目编译必须确保添加了C:\msys64\mingw64;C:\msys64\mingw64\bin;C:\msys64\usr\bin;三个变量到系统环境变量,设置完成以上步骤,如果没有报错就能在Qt-Creator当中开发和调试KiCad开源项目了。