clion 设置
设置,外观与行为,外观,主菜单:与主工具栏合并
Settings → Build, Execution, Deployment → Toolchains,默认
在CLion中生成单一独立可执行文件(不依赖外部动态库)需要结合编译器/链接器配置和打包工具,cmake配置文件通用版本:
# 静态链接配置
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
中文乱码:源码使用 GB18030
快捷键:
- 运行:shift + F10
- 新增、删除断点:ctrl + F8
- 进入调试:选中c++文件,shift + F9
- 逐过(Step Over):按 F8,执行当前行代码并跳到下一行,即使当前行包含函数调用,函数内部不会进入
安装qt6
安装qt6: https://mirrors.tuna.tsinghua.edu.cn/qt/official_releases/online_installers/
查看当前最长支持周期版本(6.8):https://doc.qt.io/qt-6/qt-releases.html
使用国内镜像安装:
.\qt-online-installer-windows-x64-online.exe --mirror https://mirrors.ustc.edu.cn/qtproject
自定义安装,如果需要添加或移除组件,可通过以下方式操作: 打开 Qt Maintenance Tool(维护工具)。登录后选择“添加或移除组件”,根据需求调整:
- Qt Creator:不选插件开发
- Qt Installer Framework
- Extensions:选择MinGW 和源码
- Qt for Development:选择MinGW 和源码
- Qt Design Studio: 选择最新版本
记录 Qt 的安装路径(如: C:\Qt\6.11.1\mingw_64\bin),将 C:\Qt\6.11.1\mingw_64\bin ,C:\Qt\Tools\CMake_64\bin\,C:\Qt\Tools\QtCreator\bin 加入系统path
创建环境变量 CMAKE_PREFIX_PATH, 值为 C:\Qt\6.11.1\mingw_64\lib\cmake
clion新建项目:
新建Qt 微件可执行项目,c++标准 23,Qt CMake前缀:C:/Qt/6.11.1/mingw_64/bin, 对应cmake文件钟的下面语句
set(CMAKE_PREFIX_PATH "C:/Qt/6.11.1/mingw_64/bin")
4、外部工具:
设置,工具,外部工具
- 名称:UI_Designer
- 程序
C:\Qt\6.11.1\mingw_64\bin\designer.exe - 实参:
$FileName$ - 工作目录:
$FileDir$
项目,新建,Qt UI 类,如 MainWindow,就可以在项目中的ui上右键,ExternalTool,UI_Designer来打开图形界面。
设置,工具,外部工具
- 名称:UIC,用户界面编译器
- 程序:C:\Qt\6.11.1\mingw_64\bin\uic.exe
- 实参:
$FileName$ -o ui_$FileNameWithoutExtension$.h - 工作目录:
$FileDir$
执行 uic生成 ui_mainwindow.h
需要修改 main.cpp 使用自己创建的Widget组件
main.cpp 原始代码:
#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();
}
修改后代码:
#include "mainwindow.h"
#include "ui_MainWindow.h"
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
MainWindow m;
m.show();
return QApplication::exec();
}
打包qt6程序:
- 新建目录,如
打包, 将生成的exe拷贝到该目录下 - 在该目录下运行
windeployqt xxx.exe --force
--force, 通过添加强制覆盖参数–force强制覆盖已存在的文件
打包时去掉命令行黑框:使用 CMake(Qt6 默认推荐)在你的 CMakeLists.txt 文件中,找到 add_executable 这一行,在目标名称后面加上 WIN32 关键字:
add_executable(你的项目名称 WIN32 main.cpp mainwindow.cpp)
警告提示: Warning: Cannot find any version of the dxcompiler.dll and dxil.dll., 处理方案:
- 方案1:增加参数
--no-system-dxc-compiler - 方案2:将包含这两个dll 的
C:\JetBrains\CLion\jbr\bin加入系统path, 不过这样打包多两个dll 会多出近30 MB左右
windeployqt xxx.exe --force --no-system-dxc-compiler
删除不必要的库,打包对应大小 26M+
vcpkg
安装git: https://git-scm.com/install/windows
安装到: C:\Git
创建目录 c:\vcpkg
目录下运行:
git clone https://github.com/Microsoft/vcpkg.git
进入vcpkg,执行:
bootstrap-vcpkg.bat -disableMetrics
clion,视图,工具窗口,vcpkg,目录选择: C:\vcpkg\vcpkg
vcpkg 需要 visual studio: https://visualstudio.microsoft.com/zh-hans/vs/
默认安装桌面c++
clion 导航到 Build, Execution, Deployment -> CMake。在 CMake options 中填入以下参数,将 [你的vcpkg路径] 替换为实际路径:
-DCMAKE_TOOLCHAIN_FILE=[你的vcpkg路径]/scripts/buildsystems/vcpkg.cmake
c++23中的定宽浮点数支持
通过vcpkg 安装fmt库,替代c++23默认的print库
C:\vcpkg\vcpkg\vcpkg.exe install fmt
Successfully downloaded msys2-msys2-runtime-3.6.5-1-x86_64.pkg.tar.zst
-- Using msys root at C:/vcpkg/vcpkg/downloads/tools/msys2/3e71d1f8e22ab23f
-- Fixing pkgconfig file: C:/vcpkg/vcpkg/packages/fmt_x64-windows/debug/lib/pkgconfig/fmt.pc
-- Installing: C:/vcpkg/vcpkg/packages/fmt_x64-windows/share/fmt/usage
-- Installing: C:/vcpkg/vcpkg/packages/fmt_x64-windows/share/fmt/copyright
-- Performing post-build validation
Starting submission of fmt:x64-windows@12.1.0 to 1 binary cache(s) in the background
Elapsed time to handle fmt:x64-windows: 20 s
Total install time: 20 s
Installed contents are licensed to you by owners. Microsoft is not responsible for, nor does it grant any licenses to, third-party packages.
Packages installed in this vcpkg installation declare the following licenses:
MIT
The package fmt provides CMake targets:
find_package(fmt CONFIG REQUIRED)
target_link_libraries(main PRIVATE fmt::fmt)
# Or use the header-only version
find_package(fmt CONFIG REQUIRED)
target_link_libraries(main PRIVATE fmt::fmt-header-only)