【不推荐】VS2010结合CLion如何配置Qt的开发环境?
缺点:这个教程存在问题——编译器、连接器好像用的是VS2017而不是2010(当时写教程的时候,不知道哪里的缓存没清理干净,导致 我以为用的是2010的编译器。实际上用的却是2017的编译器)
结论:我建议,直接下载qt-opensource-windows-x86-msvc2013_64_opengl-5.4.2.exe。这个版本的QT跟VS2010是完美兼容的。!!!不用考虑用VS2010编译Qt5.4的问题了。
【这个方案有问题,因为VCPKG支持VS2015以上的版本,不支持VS2010】
0、下载VisualStudioSetup.exe 安装全套的C++开发核心
1、下载Clion;
2、下载Vcpkg;
3、安装下载Qt5的相关依赖(下载、编译过程花费大概4~5个小时,此时编译启应该是2017或者更高的版本)
4、在Clion中新建一个Qt项目
5、Qt项目对应的Cmake文件:
cmake_minimum_required(VERSION 2.8)
project(untitled)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
find_package(Qt5 COMPONENTS
Core
Gui
Widgets
REQUIRED)
add_executable(untitled main.cpp)
target_link_libraries(untitled
Qt5::Core
Qt5::Gui
Qt5::Widgets
)
if (WIN32 AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
set(DEBUG_SUFFIX)
if (MSVC AND CMAKE_BUILD_TYPE MATCHES "Debug")
set(DEBUG_SUFFIX "d")
endif ()
set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")
if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
endif ()
endif ()
if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory
"$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
endif ()
foreach (QT_LIB Core Gui Widgets)
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${QT_INSTALL_PATH}/bin/Qt5${QT_LIB}${DEBUG_SUFFIX}.dll"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>")
endforeach (QT_LIB)
endif ()
6、Clion对应的Toolchain配置(与VS2010深度耦合)
7、Clion对应的Cmake启动参数
-DCMAKE_WARN_VS10=OFF
-DCMAKE_GENERATOR_PLATFORM=x64
-DCMAKE_TOOLCHAIN_FILE=C:\Users\XXX\.vcpkg-clion\vcpkg\scripts\buildsystems\vcpkg.cmake
8、main函数的代码:
#include <QApplication>
#include <QPushButton>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
QPushButton button("Hello world!", nullptr);
button.resize(200, 100);
button.show();
return QApplication::exec();
}
==上述代码,会出现命令行。 下述代码不会出现命令行:==
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
#include <QApplication>
#include <QPushButton>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QPushButton button("Hello world!", nullptr);
button.resize(200, 100);
button.show();
return QApplication::exec();
}