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.