'How to use the Visual Studio Code to navigate Linux kernel source
I am converting from Eclipse CDT. The preprocessor macro feature is a must when navigating through C/C++ code with preprocessor defines (whether explicitly specified in Makefile or included through an external auto-generated header file). Without this, navigating the Linux source would be impossible, as described on this seminal wiki page on using Eclipse to study the Linux code. I am looking for an equivalent feature for Visual Studio Code. Would appreciate a pointer.
Solution 1:[1]
- Install ms-vscode.cpptools extension.
- Open kernel source folder in VSCode.
- Follow the instructions, add "${workspaceFolder}/include" and "${workspaceFolder}/arch/{your arch}/include" to includePath, "your arch" is x86/arm etc.
- Wait for IntelliSence indexing.
Solution 2:[2]
As of today I have found the use of the vscode-linux-kernel project to be superior to both answers listed above, as navigation through the code and Intellisense works very well.
The project is released to the public domain.
Solution 3:[3]
This is what worked best for me (fixing some issue of the snippet in a previous answer):
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/include",
"${workspaceFolder}/arch/<arch>/include",
"${workspaceFolder}/arch/<arch>/include/generated"
],
"forcedInclude": [
"${workspaceFolder}/BUILD/include/generated/autoconf.h"
],
"defines": [
"__KERNEL__"
],
"compilerPath": "/usr/bin/gg", # replace this with your compiler (also gcc cross-compiler)
"cStandard": "c11",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
Where you may replace <arch> with your actual architecture.
Solution 4:[4]
I did't need to add the arch folder (for x86_64 it was actually empty), but add some basic defines to make compiler specific types and macros visible to intellisense. My minimal config is shown below. For perfect results you would have to add all defines as configured in your kernel .config file (e.g. CONFIG_MMU). There are many of those, so typically you will only focus on the few you really care about.
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/include"
],
"defines": [
"__GNUC__",
"__KERNEL__"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
Solution 5:[5]
These responses are helpful, but https://www.kernel.org/doc/html/latest/process/programming-language.html would indicate that cStandard should be gnu89.
Solution 6:[6]
Makefile Tool provides IntelliSense configurations to the VS Code C/C++ Extension for Makefile projects. We dont have to manage the c_cpp_properties.json manually.
It's the best solution I'm aware of to work with the Linux kernel code in VSCode.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | tinytaro |
| Solution 2 | Marc.2377 |
| Solution 3 | |
| Solution 4 | |
| Solution 5 | Brian Topping |
| Solution 6 | ouflak |
