找回密码
 立即注册
首页 业界区 安全 C++ 开发环境配置

C++ 开发环境配置

句惫 9 小时前
C++ 开发环境配置

我们使用主流的开源软件来配置C++开发环境

  • vscode
  • cmake
以上均是跨平台的开源软件,也是最主流的开发环境
环境安装

在linux需要安装以下软件

  • gcc
  • g++
  • cmake
  • gdb
可以使用以下命令安装
  1. 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 等,仅举几例。
查看电脑架构:
  1. PS C:\Users\29051> [Environment]::GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")
  2. 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.
安装开发环境
  1. $ pacman -S mingw-w64-ucrt-x86_64-gcc
  2. $ pacman -S mingw-w64-ucrt-x86_64-g++
  3. $ pacman -S mingw-w64-ucrt-x86_64-gdb
  4. $ pacman -S mingw-w64-ucrt-x86_64-cmake
  5. $ pacman -S mingw-w64-ucrt-x86_64-python3
  6. # 查看版本
  7. $ gcc --version
  8. gcc.exe (Rev8, Built by MSYS2 project) 15.1.0
  9. $ g++ --version
  10. g++.exe (Rev8, Built by MSYS2 project) 15.1.0
复制代码
使用msys2可以方便升级、卸载、集成各种库。集成库可以配置cmake的find_package使用三方库.
记得安装的命令都以mingw-w64-ucrt-x86_64开头,不要安装错误。写错的话,会被安装在其他地方。
常用命令
  1. # 更新已安装的软件包
  2. $ pacman -Syu
  3. # 安装更新软件包
  4. $ pacman -S mingw-w64-ucrt-x86_64-gcc
  5. # 卸载软件包
  6. $ pacman -R mingw-w64-ucrt-x86_64-gcc
  7. # 搜索软件包
  8. $ pacman -Ss mingw-w64-ucrt-x86_64-gcc
复制代码

  • -S: -Sync 代表安装、更新。
  • y: 代表刷新(refresh) 软件包数据库。
  • u: upgrade更新已安装的软件包。
  • s: search搜索数据包
  • -R: -Remove 代表移除。
基础环境搭建

ctrl + shift + A选择Cmake: Quick Start,一步一步开始创建项目,等到最后一步:
1.png

这一步不要管按Esc退出,新项目就创建好了。
写第一个Hello World

创建如下项目机构:
2.png

配置CmakeLists.txt
  1. cmake_minimum_required(VERSION 3.10.0)
  2. project(learn01 VERSION 0.1.0 LANGUAGES C CXX)
  3. # ✅ 设置 C++ 标准
  4. set(CMAKE_CXX_STANDARD 26)  # 使用 C++26 标准
  5. set(CMAKE_CXX_STANDARD_REQUIRED ON)  # 强制使用指定标准
  6. set(CMAKE_CXX_EXTENSIONS OFF)        # 禁用编译器扩展(使用纯标准)
  7. # 如果是单配置生成器(Makefile/Ninja),在未指定时默认使用 Release
  8. if(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
  9.   message(STATUS "No build type specified, default to Release")
  10.   set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type" FORCE)
  11. endif()
  12. # 查找源文件
  13. file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS
  14.         "src/*.cpp"
  15.         "src/*.c"
  16. )
  17. add_executable(learn01 main.cpp ${SOURCES})
  18. # 设置头文件包含路径
  19. target_include_directories(${CMAKE_PROJECT_NAME}
  20.     PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include/learn01
  21.     PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include/learn02
  22. )
  23. # —— 为目标按配置设置编译选项 ——
  24. # Release: -O2 (GCC/Clang) 或 /O2 (MSVC),并加上 -DNDEBUG
  25. # Debug: 指定更合适的 Debug 标志
  26. if (MSVC)
  27.     message(STATUS "Using MSVC compiler settings")
  28.     target_compile_options(learn01 PRIVATE
  29.         $<$<CONFIG:Release>:/O3 /DNDEBUG>
  30.         $<$<CONFIG:Debug>:/Od /Zi>
  31.     )
  32. else()
  33.     message(STATUS "Using GCC/Clang compiler settings")
  34.     target_compile_options(learn01 PRIVATE
  35.         $<$<CONFIG:Release>:-O3 -DNDEBUG>
  36.         $<$<CONFIG:Debug>:-Og -g>
  37.     )
  38. endif()
  39. # 可选:对 Release 开启跨模块优化(LTO),如果编译器/链接器支持
  40. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE ON)
  41. include(CTest)
  42. enable_testing()
  43. set(CPACK_PROJECT_NAME ${PROJECT_NAME})
  44. set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
  45. include(CPack)
复制代码
learn01.hpp
  1. #ifndef LEARN01_HPP
  2. #define LEARN01_HPP
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6. #include <coroutine>
  7. #include <exception>
  8. #include <thread>
  9. #include <chrono>
  10. #include <future>
  11. #include <vector>
  12. #include <queue>
  13. #include <mutex>
  14. #include <condition_variable>
  15. #include <functional>
  16. #include <memory>
  17. #include <format>
  18. #include <istream>
  19. #include <fstream>
  20. namespace learn01{
  21.     void learn02();
  22.     void learn01();
  23. }
  24. #endif // LEARN01_HPP
复制代码
learn01.cpp
[code]#include "learn01.hpp"namespace learn01{    void learn02(){        std::cout

相关推荐

您需要登录后才可以回帖 登录 | 立即注册