Lazy loaded image
🥳嵌入式 Linux开发
Lazy loaded imageGuide to VSCode
Words 2445Read Time 7 min
2024-11-29
2024-11-29
type
date
slug
category
icon
password

一、Windows 环境安装

1.1 下载 Visual Studio Code

1.2 安装 VS Code,注意 添加到 PATH(重启后生效)

1.3 获取 MinGW-w64 编译器软件包

  1. 下载 MSYS2 并安装
  1. 开始菜单运行“MSYS2 MSYS”,按照教程添加mingw-w64 编译工具链 $ pacman -S mingw-w64-ucrt-x86_64-gcc
    1. 终端输入 gcc --version 检查是否安装成功
    1. C:\msys64\mingw64\bin 路径添加到Path 的环境变量

    1.4 安装“C/C++“ 插件

    The C/C++ extension adds language support for C/C++ to Visual Studio Code, including editing (IntelliSense) and debugging features.
    Here is a list of compilers and architectures per platform officially supported by the extension. These are reflected by the available IntelliSense modes from the extension's IntelliSense configuration. Note that support for other compilers may be limited.
    Platform
    Compilers
    Architectures
    Windows
    MSVC, Clang, GCC
    x64, x86, arm64, arm
    Linux
    Clang, GCC
    x64, x86, arm64, arm
    macOS
    Clang, GCC
    x64, x86, arm64
    设置C/C++,配置标识和编译器路径,这里添加C:/msys64/mingw64/bin/gcc.exe
    notion image
    notion image
    会在 .vscode 文件夹下生成 c_cpp_properties.json
    • 操作平台 Win32
    • 头文件为工作文件夹及其子文件夹
    • 定义 _DEBUG UNICODE 宏
    • 编译器路径 compilerPath
    • C/CPP 标准
    • 智能提示模式 gcc-x64

    1.5 添加调试文件Launch.json

    最新版本VS Code,创建出的 launch.json 文件为空文件,内容需要自己填。
    notion image
    1. 范例
    • name :指定 launch.json 的名称。
    • type:告诉vscode编译器的类型,我用的MinGW64也就是g++,这里是cppdgb,这个是规定的,不是随便写,比如msvc编译器就是cppvsdbg
    • program:这个是你的可执行程序位置,这里可以根据自己的tasks.json生成
      • 程序的位置自定义修改,等会参照后面的tasks.json内容。"program": "${workspaceFolder}\\build\\${workspaceRootFolderName}.exe"
      • 单个文件调试:"program": "${fileDirname}/${fileBasenameNoExtension}"
      • 专门指定文件:"${workspaceFolder}/a.out",
    • vscode内置的变量说明
      • preLaunchTask:表示在执行调试前要完成的任务,是 tasks.json 中 lable 标记的任务名称
      • request:指明 JSON 文件的类型。
      • cwd:表示当前工作目录。请注意,launch.json 文件中的所有地址都是通用形式,它们不特定于任何文件。
      • externalConsole:是否使用外部控制台,如果是 true,则启动外部控制台,否则使用 VS Code 的集成终端。
      • miDebuggerPath:指明调试器的位置,这个也是因用户而异的。
       
      1. 常见错误
      here is some problem with PowerShell being run as in internal console (running within VSCode).
      I made the following change in launch.json:
      before:
      after:
      Now, I see the output of the program in cmd.exe outside VSCode. But, the debugger runs fine inside VSCode.
       
      在的vscode调试并不依赖于launch.json,有tasks.json就行(保证了可以运行),没有launch.json的话会vscode也会用默认的debug方法来调试。
      通过顶部选项 run下的add configuration 只会
      运行文件有中文路径在编译的时候就会出错,同时在点编译或者运行的时候,需要在cpp文件页面,否则也会报错,因为需要active file是cpp文件。
       
      最新最全vscode配置c/c++教程,可调试项目_哔哩哔哩_bilibili
      1. vscode一键配置C/C++多个C及CPP文件编译与tasks.json和launch.json原理
      1. VScode tasks.json和launch.json的设置 - 知乎 (zhihu.com)

      方法1: 用 vscode 自带

      1. 点击 Run | Run Without Debugging,选择 C/C++: g++.exe 生成和调试活动文件,会在 .vscode 文件夹生成 tasks.json
        1. notion image
          notion image
      1. 选择调试文件,按 F5 键,主程序打断点,进入调试模式。

      方法2:使用插件 C/C++ Runner

       
      构建、运行和调试(不需要配置调试launch.json)
      notion image
       

      方法3:使用插件 C/C++ project generator

      notion image
      command platte | create c++ project | 生成目录结构(包括tasks.json - build,build&run,clean 和 launch.json - 可进入gdb调试模式,适用于linux, osx 和windows)

      方法4:使用插件 c/c++ Makefile Project

      notion image
      command platte | c/c++ Makefile Init Project
      notion image
       
       

      二、Linux 环境安装

      2.1 Prerequisites

      1. 安装 VS Code
      1. 安装 C/C++ 扩展插件

      2.2 确保 GCC 安装

      2.3 Create Hello World

      1. 创建 projects/helloworld
      1. .vscode 文件夹下创建3个文件
          • tasks.json (compiler build settings)
          • launch.json (debugger settings)
          • c_cpp_properties.json (compiler path and IntelliSense settings)
      1. Add hello world source code file

        2.4 Explore IntelliSense

        自动补全功能

        2.5 Run helloworld.cpp

        1. Open helloworld.cpp so that it is the active file.
        1. Press the play button in the top right corner of the editor.
          1. notion image
        1. Choose g++ build and debug active file from the list of detected compilers on your system.
          1. notion image
        1. After the build succeeds, your program's output will appear in the integrated Terminal.
        Note1 : You can learn more about tasks.json variables in the variables reference.
        Note2 : build multiple C++ files by using an argument like "${workspaceFolder}/*.cpp" instead of "${file}".This will build all .cpp files in your current folder.
        This task tells g++ to take the active file (${file}), compile it, and create an executable file in the current directory (${fileDirname}) with the same name as the active file but without an extension (${fileBasenameNoExtension}), resulting in helloworld for our example.

        2.6 Explore the debugger

        • Debug Output
        • Run and Debug
        • a debugging control panel
        If you already have a launch.json file in your workspace, the play button will read from it when figuring out how run and debug your C++ file. If you don’t have launch.json, the play button will create a temporary “quick debug” configuration on the fly, eliminating the need for launch.json altogether!

        2.7 Customize debugging with launch.json

        2.8 C/C++ configurations

        2.8 Reusing your C++ configuration

        VS Code is now configured to use gcc on Linux. The configuration applies to the current workspace. To reuse the configuration, just copy the JSON files to a .vscode folder in a new project folder (workspace) and change the names of the source file(s) and executable as needed.

        三、Makefile 管理编译嵌入式工程

         
         
        vscode 有哪些让人眼前一亮的插件? - 韦易笑的回答 - 知乎 https://www.zhihu.com/question/311803609/answer/2387914071
        上一篇
        嵌入式硬件基础
        下一篇
        嵌入书专家和经典书籍

        Comments
        Loading...