I. Preparatory work
1. Kernel source code version selection
Be sure to have a copy of the Kernel Source Code that compiles and passes, this time choosing version 5.10.
#Note: In the kernel source code of version 5.10, there is a file "gen_compile_commands.py" in the scripts/clang-tools directory, which also generates the file "compile_commands.json".
It has been determined that version 4.19 does not have it, so please try to choose a higher version of the kernel source code.
2、VScode
2.1. Install the Clangd plugin, making sure to install it in a virtual machine that is already remotely connected.
Hereinafter: VSC
After tips from other articles, it is recommended to always disable the vsc plugin as: <C/C++ Extension>.
II. clang+bear
2.1 Install Bear
sudo apt-get install -y bear
bear --version #Verify that bear is installed, if the version number appears then it is OK.
2.2. Kernel source code directory operations
Go to the kernel source directory
bear make ARCH=arm64 CROSS_COMPILE=/home/topeet/Linux/rk3568_linux_5.10/prebuilts/gcc/linux-x86/aarch64/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu- rockchip_linux_defconfig
#Or use the following
bear make ARCH=arm64 CROSS_COMPILE=/home/topeet/Linux/rk3568_linux_5.10/prebuilts/gcc/linux-x86/aarch64/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu- rockchip_defconfig
bear make ARCH=arm64 CROSS_COMPILE=/home/topeet/Linux/rk3568_linux_5.10/prebuilts/gcc/linux-x86/aarch64/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu- -j8
2.3. Products
The compile_commands.json file is generated in the kernel folder.
2.4. VSC operation
Directly open your vscode, with vsc just open the kernel source code directory, and then arbitrarily click on a C code file will trigger clang, in the terminal page of VSC click on "Output", and then select "clangd", you can see the process of triggering clang. You can see the process of triggering clang. At the same time, you will see a prompt like the following in the bottom left corner of VSC:
# Numbers can be other than what is written below, for case studies only
<spinning> indexing:98/3695
When the index is finished, it is ready to use.
2.5、Project catalog operation
Just copy the compile_commands.json file to the current project directory.
III. clang+source built-in scripts
3.1 Kernel source code directory operations
1.1, enter the kernel source code directory, get the configuration file
make ARCH=arm64 CROSS_COMPILE=/home/topeet/Linux/rk3568_linux_5.10/prebuilts/gcc/linux-x86/aarch64/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu- rockchip_linux_defconfig
1.2. Enter the kernel source directory and compile the kernel source code.
make ARCH=arm64 CROSS_COMPILE=/home/topeet/Linux/rk3568_linux_5.10/prebuilts/gcc/linux-x86/aarch64/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu- -j8
3.2. Products
# Execute in the directory where the kernel source code is located:
python scripts/clang-tools/gen_compile_commands.py
# Generate compile_commands.json file
3.4. VSC operation
Directly open your vscode, with vsc just open the kernel source code directory, and then arbitrarily click on a C code file will trigger clang, in the terminal page of VSC click on "Output", and then select "clangd", you can see the process of triggering clang. You can see the process of triggering clang. At the same time, you will see a prompt like the following in the bottom left corner of VSC:
# Numbers can be other than what is written below, for case studies only
<spinning> indexing:98/3695
When the index is finished, it is ready to use.
3.5、Project catalog operation
Just copy the compile_commands.json file to the current project directory.
IV. Reference documents
1, "[vscode based on clangd to realize Android/Linux code jump]
/
2, "look at the code artifacts: vscode + clangd easy to realize linux kernel code jump (graphic)
/ludaoyi88/article/details/135051470
3. "VSCode+clangd reading linux kernel source code".
/weixin_40209493/article/details/128175003?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_baidulandingword~default-0-128175003-blog-135051470.235v43pc_blog_bottom_relevance_base5&spm=1001.2101.3001.4242.1&utm_relevant_index=1