vscode-linux-kernel icon indicating copy to clipboard operation
vscode-linux-kernel copied to clipboard

Visual Studio Code project template for Linux kernel source development and navigation.

Visual Studio Code project template for Linux kernel

Visual Studio Code project template for Linux kernel source development and navigation.

Preparation

Download and install Visual Studio Code

Install C/C++ extension for Visual Studio Code (In VSCode, Ctrl+Shift+X, search "C/C++")

Build your kernel first (important, it will generate some files that VSCode needed)

make <your-defconfig> # usually in the arch/<arch>/configs/ folder
make -j$(nproc)

VSCode setup

Copy .vscode to the linux kernel folder

Alternatively, you could use existing templates x86_64.vscode or arm64.vscode

Modify .vscode/c_cpp_properties.json to make IntelliSense work

  • Change <arch> to target architecture, for examples:

    • arm for ARM 32bit
    • arm64 for ARM 64bit
    • x86 for both x86 32bit and 64bit
  • Change <gcc-compile-path> to your gcc compile path, for examples:

    • /usr/bin/aarch64-linux-gnu-gcc for ARM 64bit
    • /usr/bin/x86_64-linux-gnu-gcc for x86 64bit
  • Change <IntelliSenseMode> to the one of following:

    • linux-gcc-arm
    • linux-gcc-arm64
    • linux-gcc-x86
    • linux-gcc-x64

Modify .vscode/tasks.json to make build tasks work (Optional)

  • Change <arch> to target architecture, for examples:

    • arm for ARM 32bit
    • arm64 for ARM 64bit
    • x86 for both x86 32bit and 64bit
  • Change <cross-compile> to your cross compile path, for examples:

    • aarch64-linux-gnu- for ARM 64bit
    • x86_64-linux-gnu- for x86 64bit
  • Change defconfig to your own kernel config

Open with Visual Stdio Code

code .

Appendix

kernel build

  • Show kernel build commands and defines

    make V=1
    
  • Specific kernel build output directory

    make O=<output-dir> [extra_args] ...
    
  • Change to kernel directory

    make -C <kernel-src-dir> [extra_args] ...
    
  • For more info

    make help
    

VSCode

  • Ctrl+P: Search files by name
  • Ctrl+Shift+F: Search text in files
  • F1, type Restart IntelliSense For Active File (type Restart and it should autosuggest)

References

  • https://stackoverflow.com/a/64479335
  • https://github.com/amezin/vscode-linux-kernel
  • https://github.com/microsoft/vscode-cpptools/issues/5588
  • https://stackoverflow.com/questions/5820303/how-do-i-force-make-gcc-to-show-me-the-commands