【C++学习笔记】九、vscode配置C++环境

1 配置说明

对于visual studio编译巨慢,不是一些大型项目的时候,编译几个破文件也慢的一批。这个时候就体现到vscode的强大。

vscode只是一个文本编译器,配合上一些插件也能做到完整编译调试c++相关的代码。
使用系统win10/ubuntu

  • VSCode:
  • cmake
  • 编译器/调试器

2 cmake

选自百度百科:
CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似UNIX下的automake。只是 CMake 的组态档取名为 CMakeLists.txt。Cmake 并不直接建构出最终的软件,而是产生标准的建构档(如 Unix 的 Makefile 或 Windows Visual C++ 的 projects/workspaces),然后再依一般的建构方式使用。这使得熟悉某个集成开发环境(IDE)的开发者可以用标准的方式建构他的软件,这种可以使用各平台的原生建构系统的能力是 CMake 和 SCons 等其他类似系统的区别之处。
win10官网上直接下载https://cmake.org/
linux系统

sudo apt install cmake

检查cmake是否安装成功并且环境变量配置正确。

cmake --version
会打印如下的内容
cmake version 3.26.1
CMake suite maintained and supported by Kitware (kitware.com/cmake).

3 VSCode

VSCode官方下载地址:https://code.visualstudio.com/
然后安装c++和cmake相关支持插件
在这里插入图片描述

4 配置

编译器和调试器可以用MinGW或者直接下载使用visual studio里的编译器(下载visual studio的时候会自己加入到环境变量中)
创建CMakeLists.txt和main.cpp文件
CMakeLists.txt文件中添加如下内容:

# cmake最低版本需求
cmake_minimum_required(VERSION 3.10)

# 工程名称
project (demo)

# 设置C标准还是C++标准
set(CMAKE_C_STANDARD 11)

add_executable(demo
        main.cpp)

在这里插入图片描述
ctrl + shift + p 打开命令终端输入:

CMake:select a Kit
在这里插入图片描述
选择对应的kit.
linux下使用gcc工具

配置完cmake 确定无报错信息即可。编译快捷键f5, 调试f7
在这里插入图片描述
如何想打断点debug 还得安装这个插件
@category:debuggers CMake
这个是cmake tool kits配置文件。可以使用上面的任意kit
C:\Users\Administrator\AppData\Local\CMakeTools\cmake-tools-kits.json

[
  {
    "name": "GCC 7.3.0 x86_64-w64-mingw32",
    "compilers": {
      "C": "D:\\Qt\\Tools\\mingw730_64\\bin\\gcc.exe",
      "CXX": "D:\\Qt\\Tools\\mingw730_64\\bin\\g++.exe"
    },
    "preferredGenerator": {
      "name": "MinGW Makefiles"
    },
    "environmentVariables": {
      "CMT_MINGW_PATH": "D:\\Qt\\Tools\\mingw730_64\\bin"
    }
  },
  {
    "name": "Visual Studio Community 2022 Release - amd64",
    "visualStudio": "4ad9cc0b",
    "visualStudioArchitecture": "x64",
    "preferredGenerator": {
      "name": "Visual Studio 17 2022",
      "platform": "x64",
      "toolset": "host=x64"
    }
  },
  {
    "name": "Visual Studio Community 2022 Release - amd64_x86",
    "visualStudio": "4ad9cc0b",
    "visualStudioArchitecture": "x64",
    "preferredGenerator": {
      "name": "Visual Studio 17 2022",
      "platform": "win32",
      "toolset": "host=x64"
    }
  },
  {
    "name": "Visual Studio Community 2022 Release - x86",
    "visualStudio": "4ad9cc0b",
    "visualStudioArchitecture": "x86",
    "preferredGenerator": {
      "name": "Visual Studio 17 2022",
      "platform": "win32",
      "toolset": "host=x86"
    }
  },
  {
    "name": "Visual Studio Community 2022 Release - x86_amd64",
    "visualStudio": "4ad9cc0b",
    "visualStudioArchitecture": "x86",
    "preferredGenerator": {
      "name": "Visual Studio 17 2022",
      "platform": "x64",
      "toolset": "host=x86"
    }
  },
  {
    "name": "gcc",
    "compilers": {
      "C": "D:\\VS\\IDE\\VC\\Tools\\MSVC\\14.35.32215\\bin\\Hostx86\\x86\\cl.exe",
      "CXX": "D:\\VS\\IDE\\VC\\Tools\\MSVC\\14.35.32215\\bin\\Hostx86\\x86\\cl.exe"
    }
  }
]

猜你喜欢

转载自blog.csdn.net/qq_38753749/article/details/129887507