C++编程环境:win64,visual studio,msvc

安装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:选择msvc 和源码
  • Qt for Development:选择 6.8 ,msvc 和源码
  • Qt Design Studio: 选择最新版本

记录 Qt 的安装路径(如: C:\Qt\6.8.3\msvc2022_64),将 C:\Qt\6.8.3\msvc2022_64\bin ,C:\Qt\Tools\CMake_64\bin\,C:\Qt\Tools\QtCreator\bin 加入系统path

创建环境变量 CMAKE_PREFIX_PATH, 值为 C:\Qt\6.8.3\msvc2022_64\lib\cmake

安装visual studio

安装visual studio 2026社区版:https://visualstudio.microsoft.com/zh-hans/

选择桌面c++开发,修改产品安装位置安装:C:\VisualStudio\18\Community

vs 安装插件: Qt VS Tools for Visual Studio 2022 and 2026。(根据提示关闭vs自动安装插件)

vs 配置qt版本:扩展,qt vs tools,qt versions,autodect

**创建 qt quick项目,不要勾选 Enable PCH support, 可能报错 C1014 包含文件太多: 深度 = 1024,Build System 选cmake project for qt,设置,选项,cmake,常规,cmake配置文件:始终使用cmake预设

可以使用c++2023标准编译:

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

打包:

C:\Qt\6.8.3\msvc2022_64\bin\windeployqt.exe  xxx.exe --qmldir xxx\QtQuickProject

cmake项目(默认C++标准为 2020)

cmake项目默认源代码:

CMakeLists.txt

# CMakeList.txt: CMakeProject1 的 CMake 项目,在此处包括源代码并定义
# 项目特定的逻辑。
#
cmake_minimum_required (VERSION 3.10)

# 如果支持,请为 MSVC 编译器启用热重载。
if (POLICY CMP0141)
  cmake_policy(SET CMP0141 NEW)
  set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()

project ("CMakeProject1")

# 将源代码添加到此项目的可执行文件。
add_executable (CMakeProject1 "CMakeProject1.cpp" "CMakeProject1.h")

if (CMAKE_VERSION VERSION_GREATER 3.12)
  set_property(TARGET CMakeProject1 PROPERTY CXX_STANDARD 20)
endif()

# TODO: 如有需要,请添加测试并安装目标。

CMakePresets.json

{
    "version": 3,
    "configurePresets": [
        {
            "name": "windows-base",
            "hidden": true,
            "generator": "Ninja",
            "binaryDir": "${sourceDir}/out/build/${presetName}",
            "installDir": "${sourceDir}/out/install/${presetName}",
            "cacheVariables": {
                "CMAKE_C_COMPILER": "cl.exe",
                "CMAKE_CXX_COMPILER": "cl.exe"
            },
            "condition": {
                "type": "equals",
                "lhs": "${hostSystemName}",
                "rhs": "Windows"
            }
        },
        {
            "name": "x64-debug",
            "displayName": "x64 Debug",
            "inherits": "windows-base",
            "architecture": {
                "value": "x64",
                "strategy": "external"
            },
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug",
                "CMAKE_PROJECT_TOP_LEVEL_INCLUDES": "$env{VSINSTALLDIR}Common7/IDE/CommonExtensions/Microsoft/CMake/cmake/Microsoft/SegmentHeap.cmake"
            }
        },
        {
            "name": "x64-release",
            "displayName": "x64 Release",
            "inherits": "x64-debug",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Release",
                "CMAKE_PROJECT_TOP_LEVEL_INCLUDES": "$env{VSINSTALLDIR}Common7/IDE/CommonExtensions/Microsoft/CMake/cmake/Microsoft/SegmentHeap.cmake"
            }
        },
        {
            "name": "x86-debug",
            "displayName": "x86 Debug",
            "inherits": "windows-base",
            "architecture": {
                "value": "x86",
                "strategy": "external"
            },
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug",
                "CMAKE_PROJECT_TOP_LEVEL_INCLUDES": "$env{VSINSTALLDIR}Common7/IDE/CommonExtensions/Microsoft/CMake/cmake/Microsoft/SegmentHeap.cmake"
            }
        },
        {
            "name": "x86-release",
            "displayName": "x86 Release",
            "inherits": "x86-debug",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Release",
                "CMAKE_PROJECT_TOP_LEVEL_INCLUDES": "$env{VSINSTALLDIR}Common7/IDE/CommonExtensions/Microsoft/CMake/cmake/Microsoft/SegmentHeap.cmake"
            }
        }
    ]
}

CMakeProject1.cpp

// CMakeProject1.cpp: 定义应用程序的入口点。
//

#include "CMakeProject1.h"

using namespace std;

int main()
{
	cout << "Hello CMake.你好CMake." << endl;
	return 0;
}

CMakeProject1.h

// CMakeProject1.h: 标准系统包含文件的包含文件
// 或项目特定的包含文件。

#pragma once

#include <iostream>

// TODO: 在此处引用程序需要的其他标头。

vcpkg

下载: https://github.com/microsoft/vcpkg

解压到某目录,如 C:\vcpkg-2026.05.25,进入vcpkg目录,启动power shell(建议),执行

.\bootstrap-vcpkg.bat

顺利完成后,目录下会出现vcpkg.exe文件。(也可以直接下载该exe放入该目录)

将 vcpkg 与开发环境(例如 Visual Studio 和 MSBuild)进行全局集成,使 vcpkg 安装的库能自动被项目识别和使用

.\vcpkg.exe integrate install

Applied user-wide integration for this vcpkg root.
CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg-2026.05.25/scripts/buildsystems/vcpkg.cmake"

All MSBuild C++ projects can now #include any installed libraries. Linking will be handled automatically. Installing new libraries will make them instantly available.

示例,搜索和安装boost库,不用全部安装,量太大了。

.\vcpkg.exe search boost
.\vcpkg.exe install boost-multiprecision

有些文件下载比较慢,可以用其他工具下载放入 C:\vcpkg-2026.05.25\downloads 目录重新执行安装boost 的命令

cmake配置,参考: https://www.cnblogs.com/quantoublog/articles/18820438

CMakeLists.txt

cmake_minimum_required (VERSION 3.10)

# 设置自己的 vcpkg 路径
set(VCPKG_PATH "C:/vcpkg-2026.05.25")

# 如果支持,请为 MSVC 编译器启用热重载。
if (POLICY CMP0141)
  cmake_policy(SET CMP0141 NEW)
  set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()

project ("CMakeProject3")

# vcpkg
set(CMAKE_TOOLCHAIN_FILE "${VCPKG_PATH}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "Vcpkg toolchain file")
set(CMAKE_PREFIX_PATH "${VCPKG_PATH}/installed/x64-windows;${CMAKE_PREFIX_PATH}")
find_package(Boost REQUIRED COMPONENTS multiprecision)

# 将源代码添加到此项目的可执行文件。
add_executable (CMakeProject3 "CMakeProject3.cpp" "CMakeProject3.h")

# vcpkg
target_link_libraries("CMakeProject3" PRIVATE Boost::multiprecision)

if (CMAKE_VERSION VERSION_GREATER 3.12)
  set_property(TARGET CMakeProject3 PROPERTY CXX_STANDARD 23)
endif()

float128:

#include <iostream>
#include <boost/multiprecision/cpp_bin_float.hpp>

using namespace boost::multiprecision;

int main() {
    // Defines a 128-bit quadruple precision float (equivalent to binary128)
    cpp_bin_float_quad a = 2.0;
    cpp_bin_float_quad b = 3.0;
    cpp_bin_float_quad c = a / b;

    // Set high formatting precision for output
    // std::setprecision(std::numeric_limits<cpp_bin_float_quad>::digits10) 的含义是:
    // 将当前输出流的浮点精度设置为 cpp_bin_float_quad 类型所能精确表示的十进制数字的最大位数。
    // 这样在输出该四精度类型的变量时,既不会遗漏有效数字,也不会因精度不足而丢失信息,同时避免了输出多余的无效数字,是一种精确且保守地展示高精度浮点数的方式。
    std::cout << std::setprecision(std::numeric_limits<cpp_bin_float_quad>::digits10) << c << std::endl;
    return 0;
}

输出: 0.666666666666666666666666666666667 ,总有效数字位数:33位

clion使用 visual studio 工具链

  • Toolset : C:\VisualStudio\18\Community
  • CMake: C:\VisualStudio\18\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe
  • Build tool:C:\VisualStudio\18\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja\ninja.exe

正文完
 0
评论(没有评论)