CLion, a cross-platform C/C++ IDE. 本文主要介绍基于Clion做为IDE, MinGW做为编译器,CMake做为项目构建工具,开发基于Qt五、qwt的C++图形GUI项目的安装、配置、编译过程。php
KeyWords:Clion;Cmake;Qt5;Qwt;msys2;MinGW;Windowspython
"C:\Program Files\JetBrains\CLion 2019.1\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Release -DCMAKE_MAKE_PROGRAM=C:/msys64/mingw32/bin/mingw32-make.exe -DCMAKE_C_COMPILER=C:/msys64/mingw32/bin/clang.exe -DCMAKE_CXX_COMPILER=C:/msys64/mingw32/bin/clang++.exe -G "CodeBlocks - MinGW Makefiles" C:\CLionProjects\DynamicLayouts -- The C compiler identification is Clang 5.0.1 -- The CXX compiler identification is Clang 5.0.1 -- Check for working C compiler: C:/msys64/mingw32/bin/clang.exe -- Check for working C compiler: C:/msys64/mingw32/bin/clang.exe -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: C:/msys64/mingw32/bin/clang++.exe -- Check for working CXX compiler: C:/msys64/mingw32/bin/clang++.exe -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: C:/CLionProjects/DynamicLayouts/cmake-build-release-msys-clang [Finished]
然而,这种方法编译Qt项目,生成的项目执行文件比用QtTool的MinGW编译的大!c++
cmake_minimum_required(VERSION 3.7) project(DynamicLayouts) # 指定c++标准的版本 set(CMAKE_CXX_STANDARD 14) # 设置Qt5的cmake模块所在目录,若是不设置将使用系统提供的版本 # QT_DIR和QT_VERSION是指定了qt安装目录和版本的环境变量 # set(CMAKE_PREFIX_PATH $ENV{QT_DIR}/$ENV{QT_VERSION}/mingw53_32/lib/cmake) set(CMAKE_PREFIX_PATH "C:\\Qt\\Qt5.7.1\\5.7\\mingw53_32\\lib\\cmake") #设置工程包含当前目录,非必须 set(CMAKE_INCLUDE_CURRENT_DIR ON) #打开全局moc,设置自动生成moc文件,必定要设置 set(CMAKE_AUTOMOC ON) #打开全局uic,非必须 set(CMAKE_AUTOUIC ON) #打开全局rcc,非必须,如需打开,注意修改33行的qrc文件名 set(CMAKE_AUTORCC ON) # Add compiler flags for building executables (-fPIE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}") #设置运行时输出可执行文件目录(CMAKE源目录CMAKE_CURRENT_SOURCE_DIR,执行目录CMAKE_CURRENT_BINARY_DIR) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)
#设置运行时输出共享库文件目录
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
#查找须要的Qt库文件,最好每个库都要写,Qt也会根据依赖关系自动添加 set(Projects_QT5_COMPONENTS Core Gui Widgets ) set(Projects_QT5_Includes ${Qt5Core_INCLUDE} ${Qt5Gui_INCLUDE} ${Qt5Widgets_INCLUDE} ) set(Projects_QT5_Libraries Qt5::Core Qt5::Gui Qt5::Widgets ) find_package(Qt5 COMPONENTS ${Projects_QT5_COMPONENTS} REQUIRED) INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR} ${Projects_QT5_Includes} )#设置项目包含目录 #查找当前文件夹中的全部源代码文件,也能够经过Set命令将全部文件设置为一个变量 FILE(GLOB SRC_FILES "./*.cpp") #查找设置当前文件夹中全部的头文件 FILE(GLOB HEAD_FILES "./*.h") #查找设置当前文件夹中全部的ui文件 FILE(GLOB UI_FILES "./*.ui") #经过Ui文件生成对应的头文件,必定要添加 # qt5_wrap_ui(WRAP_FILES ${UI_FILES}) #添加资源文件,非必须,一旦采用,注意修改相应的qrc文件名 # set(RCC_FILES rcc.qrc) #将ui文件和生成文件整理在一个文件夹中,非必须 # source_group("Ui" FILES ${UI_FILES} ${WRAP_FILES} ) #建立工程文件 # add_executable(${PROJECT_NAME} ${SRC_FILES} ${HEAD_FILES} ${RCC_FILES} ${WRAP_FILES}) add_executable(${PROJECT_NAME} ${SRC_FILES} ${HEAD_FILES} ) #添加Qt5依赖项 target_link_libraries(${PROJECT_NAME} ${Projects_QT5_Libraries} )
cmake_minimum_required(VERSION 3.7)
project(QwtRadio)
# 指定c++标准的版本
set(CMAKE_CXX_STANDARD 14)
# 设置Qt5的cmake模块所在目录,若是不设置将使用系统提供的版本C:\Qt\Qt5.7.1\5.7\mingw53_32\lib\cmake
# QT_DIR和QT_VERSION是指定了qt安装目录和版本的环境变量C:\msys64\mingw32\lib\cmake
set(CMAKE_PREFIX_PATH "C:\\msys64\\mingw32\\lib\\cmake")
#设置工程包含当前目录,非必须
set(CMAKE_INCLUDE_CURRENT_DIR ON)
#打开全局moc,设置自动生成moc文件,必定要设置
set(CMAKE_AUTOMOC ON)
#打开全局uic,非必须
set(CMAKE_AUTOUIC ON)
#打开全局rcc,非必须,如需打开,注意修改33行的qrc文件名
set(CMAKE_AUTORCC ON)
# Add compiler flags for building executables (-fPIE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
#设置运行时输出可执行文件目录(CMAKE源目录CMAKE_CURRENT_SOURCE_DIR,执行目录CMAKE_CURRENT_BINARY_DIR)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)
#设置运行时输出共享库文件目录
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
#查找须要的Qt库文件,最好每个库都要写,Qt也会根据依赖关系自动添加
MESSAGE("======Searching for Qt5======")
set(Projects_QT5_COMPONENTS
Core
Gui
Widgets
)
set(Projects_QT5_Includes
${Qt5Core_INCLUDE_DIRS}
${Qt5Gui_INCLUDE_DIRS}
${Qt5Widgets_INCLUDE_DIRS}
)
set(Projects_QT5_Libraries
Qt5::Core
Qt5::Gui
Qt5::Widgets
)
find_package(Qt5 COMPONENTS ${Projects_QT5_COMPONENTS} REQUIRED)
MESSAGE(${Qt5Widgets_INCLUDE_DIRS})
MESSAGE(${Projects_QT5_Libraries})
MESSAGE("=======Searching for QWT======")
FIND_PATH(QWT_INCLUDE_DIR NAMES qwt.h PATHS
${QT_INCLUDE_DIR}
PATH_SUFFIXES qwt
)
MESSAGE(${QWT_INCLUDE_DIR})
FIND_LIBRARY(QWT_LIBRARY NAMES qwt PATHS
${QT_INCLUDE_DIR}/lib
)
MESSAGE(${QWT_LIBRARY})
#设置项目包含目录,包括将第三方库附加
INCLUDE_DIRECTORIES(
${CMAKE_CURRENT_SOURCE_DIR}
${Projects_QT5_Includes}
${QWT_INCLUDE_DIR}
)
# 将第三方库附加到变量Projects_Extra_Libraries中,便于管理
list(APPEND Projects_Extra_Libraries ${QWT_LIBRARY})
# 等价于VS下 属性-<配置属性-<C/C++-<预处理器,预处理器定义:QWT_DLL LOG4QT_DLL
ADD_DEFINITIONS(-DQWT_DLL)
#查找当前文件夹中的全部源代码文件,也能够经过Set命令将全部文件设置为一个变量
FILE(GLOB SRC_FILES "./*.cpp")
#查找设置当前文件夹中全部的头文件
FILE(GLOB HEAD_FILES "./*.h")
#查找设置当前文件夹中全部的ui文件
FILE(GLOB UI_FILES "./*.ui")
#经过Ui文件生成对应的头文件,必定要添加
# qt5_wrap_ui(WRAP_FILES ${UI_FILES})
#添加资源文件,非必须,一旦采用,注意修改相应的qrc文件名
# set(RCC_FILES rcc.qrc)
#将ui文件和生成文件整理在一个文件夹中,非必须
# source_group("Ui" FILES ${UI_FILES} ${WRAP_FILES} )
#建立工程文件
# add_executable(${PROJECT_NAME} ${SRC_FILES} ${HEAD_FILES} ${RCC_FILES} ${WRAP_FILES})
add_executable(${PROJECT_NAME}
${SRC_FILES}
${HEAD_FILES}
# ${RCC_FILES}
)
#添加Qt5依赖项,添加qwt等第三方依赖项
target_link_libraries(${PROJECT_NAME}
${Projects_QT5_Libraries}
${Projects_Extra_Libraries}
)
"C:\Program Files\JetBrains\CLion 2019.1\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Release -DCMAKE_MAKE_PROGRAM=C:/msys64/mingw32/bin/mingw32-make.exe -DCMAKE_C_COMPILER=C:/msys64/mingw32/bin/gcc.exe -DCMAKE_CXX_COMPILER=C:/msys64/mingw32/bin/g++.exe -G "CodeBlocks - MinGW Makefiles" C:\CLionProjects\QwtRadio -- The C compiler identification is GNU 7.3.0 -- The CXX compiler identification is GNU 7.3.0 -- Check for working C compiler: C:/msys64/mingw32/bin/gcc.exe -- Check for working C compiler: C:/msys64/mingw32/bin/gcc.exe -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: C:/msys64/mingw32/bin/g++.exe -- Check for working CXX compiler: C:/msys64/mingw32/bin/g++.exe -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done ======Searching for Qt5====== C:/msys64/mingw32/include/C:/msys64/mingw32/include/QtWidgetsC:/msys64/mingw32/include/QtGuiC:/msys64/mingw32/include/QtCoreC:/msys64/mingw32/share/qt5//mkspecs/win32-g++ Qt5::CoreQt5::GuiQt5::Widgets =======Searching for QWT====== C:/msys64/mingw32/include/qwt C:/msys64/mingw32/lib/libqwt.dll.a -- Configuring done -- Generating done -- Build files have been written to: C:/CLionProjects/QwtRadio/cmake-build-release-mingw32msys [Finished]
更新本地软件包: pacman -S --refresh 能够缩写为:pacman -Sy 升级软件包: pacman -S --refresh --sysupgrade 能够缩写为:pacman -Syu 列出全部已安装软件包: pacman -Q --explicit 或者 pacman -Q -e 能够列出全部的软件组: pacman -Q --groups 安装新的软件包: pacman -S <package_names|package_groups> 好比安装 Qt5: pacman -S mingw-w64-i686-qt5 注:能够不用明确软件包版本,系统会根据依赖项进行自动匹配下载相应版本。 搜索软件包: 若是不清楚软件的准确名称,能够经过核心软件名查询软件包的名称。 pacman -Ss <name_pattern> 好比搜索gcc相关的软件 pacman -Ss gcc 删除软件包: pacman -R <package_names|package_groups>
## mirrorlist.mingw32 ## 32-bit Mingw-w64 repository mirrorlist ## ## Primary ## msys2.org Server = http://mirrors.ustc.edu.cn/msys2/mingw/i686/ Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/i686/ Server = https://mirrors.huaweicloud.com/msys2/mingw/i686/ Server = http://repo.msys2.org/mingw/i686/ Server = https://sourceforge.net/projects/msys2/files/REPOS/MINGW/i686/ Server = http://www2.futureware.at/~nickoe/msys2-mirror/mingw/i686/ Server = https://mirror.yandex.ru/mirrors/msys2/mingw/i686/ -------------------------------------------------------------------------- ## mirrorlist.mingw64 ## 64-bit Mingw-w64 repository mirrorlist ## ## Primary ## msys2.org Server = http://mirrors.ustc.edu.cn/msys2/mingw/x86_64/ Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/x86_64/ Server = https://mirrors.huaweicloud.com/msys2/mingw/x86_64/ Server = http://repo.msys2.org/mingw/x86_64/ Server = https://sourceforge.net/projects/msys2/files/REPOS/MINGW/x86_64/ Server = http://www2.futureware.at/~nickoe/msys2-mirror/mingw/x86_64/ Server = https://mirror.yandex.ru/mirrors/msys2/mingw/x86_64/
开发环境安装配置示例:git
https://www.devdungeon.com/content/install-gcc-compiler-windows-msys2-ccsql
Download MSYS2 from http://www.msys2.org/. Download the .exe file and follow the installation instructions on the site. After installing, navigate to the directory where it was installed, and run msys2.exe. For this tutorial, we will assume the default location of C:\msys64. After opening it you should find yourself in a bash shell.shell
MSYS2 uses the pacman package manager that the Arch Linux distribution uses. After your initial install it is a good idea to update all the packages. Update everything using:json
pacman –Syu pacman –Su
In the MSYS2 bash shell, use pacman again to install the build toolchain and compilers. Run the command below to install the mingw-w64-x86_64-toolchain package group.vim
# Install make, autoconf, etc to C:\msys64\usr\bin pacman -S base-devel
# pacman -S base-devel 1) asciidoc 2) autoconf 3) autoconf2.13 4) autogen 5) automake-wrapper 6) automake1.10 7) automake1.11 8) automake1.12 9) automake1.13 10) automake1.14 11) automake1.15 12) automake1.16 13) automake1.6 14) automake1.7 15) automake1.8 16) automake1.9 17) bison 18) diffstat 19) diffutils 20) dos2unix 21) file 22) flex 23) gawk 24) gdb 25) gettext 26) gettext-devel 27) gperf 28) grep 29) groff 30) help2man 31) intltool 32) lemon 33) libtool 34) libunrar 35) libunrar-devel 36) m4 37) make 38) man-db 39) pacman 40) pactoys-git 41) patch 42) patchutils 43) perl 44) pkg-config 45) pkgfile 46) quilt 47) rcs 48) scons 49) sed 50) swig 51) texinfo 52) texinfo-tex 53) ttyrec
If you want to compile an SSL program that links to libssl and libcrypto with -lssl -lcrypto you will need to install openssl-devel as shown below. There are many other devel packages. For example, libbz2-devel, libelf-devel, libunrar-devel, and libyaml-devel. The environment is rather limited, but it can be useful for learning.windows
# Install all *-devel packages pacman -S development
Packages included in "development" group:数组
:: There are 78 members in group development: :: Repository msys 1) apr-devel 2) apr-util-devel 3) aspell-devel 4) bash-devel 5) cloog-devel 6) gamin-devel 7) gettext-devel 8) glib2-devel 9) gmp-devel 10) heimdal-devel 11) icu-devel 12) isl-devel 13) jansson-devel 14) jsoncpp-devel 15) libarchive-devel 16) libargp-devel 17) libassuan-devel 18) libatomic_ops-devel 19) libbobcat-devel 20) libbz2-devel 21) libcares-devel 22) libcrypt-devel 23) libcurl-devel 24) libdb-devel 25) libedit-devel 26) libelf-devel 27) libevent-devel 28) libexpat-devel 29) libffi-devel 30) libgc-devel 31) libgcrypt-devel 32) libgdbm-devel 33) libgnutls-devel 34) libgpg-error-devel 35) libgpgme-devel 36) libgpgme-python2 37) libgpgme-python3 38) libguile-devel 39) libiconv-devel 40) libidn-devel 41) libidn2-devel 42) libksba-devel 43) liblz4-devel 44) liblzma-devel 45) liblzo2-devel 46) libmetalink-devel 47) libneon-devel 48) libnettle-devel 49) libnghttp2-devel 50) libnpth-devel 51) libp11-kit-devel 52) libpipeline-devel 53) libpsl-devel 54) libreadline-devel 55) librhash-devel 56) libsasl-devel 57) libserf-devel 58) libsqlite-devel 59) libssh2-devel 60) libtasn1-devel 61) libtirpc-devel 62) libtre-devel-git 63) libunistring-devel 64) libuv-devel 65) libxml2-devel 66) libxslt-devel 67) libyaml-devel 68) mpc-devel 69) mpfr-devel 70) ncurses-devel 71) openssl-devel 72) pcre-devel 73) pcre2-devel 74) protobuf-devel 75) ucl-devel 76) util-macros 77) xproto 78) zlib-devel
If you want to access everything from your Windows Command Prompt, then add the bin directory to your Windows PATH environment variable. Keep in mind this adds a lot of executables to your path which might conflict with other applications. The usr\bin\ directory contains the whole slew of executables listed above. There is a lot of unnecessary stuff in that directory. mingw64\bin\ directory :
;C:\msys64;C:\msys64\mingw64\bin;C:\msys64\usr\bin
Libraries and include files can be found in two places.
# The dynamic lib runtime .dll files will be in bin dirs # Add the bin directory to PATH environment variable so it can find the .dll files C:\msys64\mingw64\bin C:\msys64\usr\bin # Static libraries C:\msys64\usr\lib C:\msys64\mingw64\lib # Header files C:\msys64\usr\include C:\msys64\mingw64\include
Some things are only available in the msys/ repo like vim and git, and will only be available in \usr\bin\. Some things like gcc are available in msys\, mingw32\, and mingw64\ repos and can potentially end up being installed in both \usr\bin\ and \mingw64\bin\. This is where you have to be careful about how you set up your PATH environment variable. If you add \usr\bin\ in order to make vim or git available, you will also add everything in that directory, which may conflict with something if you also add the \mingw64\bin\ directory to your path. If you only want the toolchain without as much extra stuff, use the mingw64 packages. Then you can add only the \mingw64\bin directory to your PATH if desired. The mingw64 repository generally has more libraries available for install that are unavailable in the general msys repo (e.g. SDL, exif, freeglut). The msys packages are intended to be used inside the msys shell, and the mingw packages are intended to be used outside of msys2.
# Install gcc in C:\msys64\mingw64\bin\ directory # To go with C:\msys64\mingw64\include and C:\msys64\mingw64\lib pacman -S mingw-w64-x86_64-toolchain Packages (17) mingw-w64-x86_64-binutils-2.30-5 mingw-w64-x86_64-crt-git-7.0.0.5245.edf66197-1 mingw-w64-x86_64-gcc-8.2.0-3 mingw-w64-x86_64-gcc-ada-8.2.0-3 mingw-w64-x86_64-gcc-fortran-8.2.0-3 mingw-w64-x86_64-gcc-libgfortran-8.2.0-3 mingw-w64-x86_64-gcc-libs-8.2.0-3 mingw-w64-x86_64-gcc-objc-8.2.0-3 mingw-w64-x86_64-gdb-8.2-1 mingw-w64-x86_64-headers-git-7.0.0.5245.edf66197-1 mingw-w64-x86_64-libmangle-git-7.0.0.5230.69c8fad6-1 mingw-w64-x86_64-libwinpthread-git-7.0.0.5231.7da6518b-1 mingw-w64-x86_64-make-4.2.1-2 mingw-w64-x86_64-pkg-config-0.29.2-1 mingw-w64-x86_64-tools-git-7.0.0.5242.1b29d1bc-1 mingw-w64-x86_64-winpthreads-git-7.0.0.5231.7da6518b-1 mingw-w64-x86_64-winstorecompat-git-7.0.0.5230.69c8fad6-1
Configuring IDE to work with this toolchain.