C++ 开发环境配置
我们使用主流的开源软件来配置C++开发环境
以上均是跨平台的开源软件,也是最主流的开发环境
环境安装
在linux需要安装以下软件
可以使用以下命令安装- sudo apt install gcc g++ cmake gdb
复制代码 在系统使用apt安装三方库之后还可以配合cmake的find_package使用三方库.
在windows上可以使用msys2
msys2官网
MSYS2 提供了许多最新版本的原生构建软件,例如 GCC、MinGW-w64、CPython、CMake、Meson、OpenSSL、FFmpeg、Rust、Ruby 等,仅举几例。
查看电脑架构:- PS C:\Users\29051> [Environment]::GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")
- AMD64
复制代码 AMD64即为x86_64.
下载x86_64版本的MSYS2然后后傻瓜式安装。打开UCRT64 shell.
官网如下介绍:
UCRT (Universal C Runtime) is a newer version which is also used by Microsoft Visual Studio by default. It should work and behave as if the code was compiled with MSVC.
安装开发环境- $ pacman -S mingw-w64-ucrt-x86_64-gcc
- $ pacman -S mingw-w64-ucrt-x86_64-g++
- $ pacman -S mingw-w64-ucrt-x86_64-gdb
- $ pacman -S mingw-w64-ucrt-x86_64-cmake
- $ pacman -S mingw-w64-ucrt-x86_64-python3
- # 查看版本
- $ gcc --version
- gcc.exe (Rev8, Built by MSYS2 project) 15.1.0
- $ g++ --version
- g++.exe (Rev8, Built by MSYS2 project) 15.1.0
复制代码 使用msys2可以方便升级、卸载、集成各种库。集成库可以配置cmake的find_package使用三方库.
记得安装的命令都以mingw-w64-ucrt-x86_64开头,不要安装错误。写错的话,会被安装在其他地方。
常用命令- # 更新已安装的软件包
- $ pacman -Syu
- # 安装更新软件包
- $ pacman -S mingw-w64-ucrt-x86_64-gcc
- # 卸载软件包
- $ pacman -R mingw-w64-ucrt-x86_64-gcc
- # 搜索软件包
- $ pacman -Ss mingw-w64-ucrt-x86_64-gcc
复制代码
- -S: -Sync 代表安装、更新。
- y: 代表刷新(refresh) 软件包数据库。
- u: upgrade更新已安装的软件包。
- s: search搜索数据包
- -R: -Remove 代表移除。
基础环境搭建
ctrl + shift + A选择Cmake: Quick Start,一步一步开始创建项目,等到最后一步:
这一步不要管按Esc退出,新项目就创建好了。
写第一个Hello World
创建如下项目机构:
配置CmakeLists.txt- cmake_minimum_required(VERSION 3.10.0)
- project(learn01 VERSION 0.1.0 LANGUAGES C CXX)
- # ✅ 设置 C++ 标准
- set(CMAKE_CXX_STANDARD 26) # 使用 C++26 标准
- set(CMAKE_CXX_STANDARD_REQUIRED ON) # 强制使用指定标准
- set(CMAKE_CXX_EXTENSIONS OFF) # 禁用编译器扩展(使用纯标准)
- # 如果是单配置生成器(Makefile/Ninja),在未指定时默认使用 Release
- if(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
- message(STATUS "No build type specified, default to Release")
- set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type" FORCE)
- endif()
- # 查找源文件
- file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS
- "src/*.cpp"
- "src/*.c"
- )
- add_executable(learn01 main.cpp ${SOURCES})
- # 设置头文件包含路径
- target_include_directories(${CMAKE_PROJECT_NAME}
- PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include/learn01
- PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include/learn02
- )
- # —— 为目标按配置设置编译选项 ——
- # Release: -O2 (GCC/Clang) 或 /O2 (MSVC),并加上 -DNDEBUG
- # Debug: 指定更合适的 Debug 标志
- if (MSVC)
- message(STATUS "Using MSVC compiler settings")
- target_compile_options(learn01 PRIVATE
- $<$<CONFIG:Release>:/O3 /DNDEBUG>
- $<$<CONFIG:Debug>:/Od /Zi>
- )
- else()
- message(STATUS "Using GCC/Clang compiler settings")
- target_compile_options(learn01 PRIVATE
- $<$<CONFIG:Release>:-O3 -DNDEBUG>
- $<$<CONFIG:Debug>:-Og -g>
- )
- endif()
- # 可选:对 Release 开启跨模块优化(LTO),如果编译器/链接器支持
- set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE ON)
- include(CTest)
- enable_testing()
- set(CPACK_PROJECT_NAME ${PROJECT_NAME})
- set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
- include(CPack)
复制代码 learn01.hpp- #ifndef LEARN01_HPP
- #define LEARN01_HPP
- #include <iostream>
- #include <string>
- #include <vector>
- #include <coroutine>
- #include <exception>
- #include <thread>
- #include <chrono>
- #include <future>
- #include <vector>
- #include <queue>
- #include <mutex>
- #include <condition_variable>
- #include <functional>
- #include <memory>
- #include <format>
- #include <istream>
- #include <fstream>
- namespace learn01{
- void learn02();
- void learn01();
- }
- #endif // LEARN01_HPP
复制代码 learn01.cpp
[code]#include "learn01.hpp"namespace learn01{ void learn02(){ std::cout |