Using Clangd for Developing with RT-Thread: How to Achieve a Clean and Clear Workspace
This article is contributed by RTTxiaoyao
Introduction:
VSCode is hailed as the best editor in the universe, but when it comes to developing with RT-Thread on master, many users encounter several common issues:The bsp directory under master contains too many files.
● Using Keil provides a much cleaner environment, but the older editor is not visually appealing.
● Opening VSCode in the root directory results in an overwhelming number of files.
● Opening VSCode in the bsp directory restricts access to RT-Thread’s src code and other driver files.
● Using scons — dist leads to isolated project development but loses Git version control.
● When using Clangd, not all files are visible.
The challenge is how to develop on RT-Thread’s master while using VSCode, allowing easy function navigation and searches without excessive irrelevant function names.
If you want to achieve this, you can follow the steps below to:
Have a clean view, displaying only the files you want, much like when developing with Keil.
Develop within VSCode
Ensure macro definitions and navigation are accurate and aligned with your build.
Recently, a pull request was submitted to meet everyone’s expectations for using VSCode, which can significantly fulfill the requirements for a compiler in VSCode. The results are as follows:
Do you want to have such a clean workspace?
You might think that this could be complicated, but developing on RT-Thread is not troublesome at all; it only requires a few simple scons commands.
Please use a version of RT-Thread after the following commit. If you encounter issues later, you can easily reset to this version to try again.
https://github.com/RT-Thread/rt-thread/commit/7ff75e21fad3e851090a9f9fcb14d0411949f2f8
Just a few simple steps will get you started.
Now, let’s follow the steps below to proceed:
Steps
Step 1: Install Clangd
If you frequently use Clangd or have already installed it, you can skip this step.
In the VSCode extension marketplace, install the Clangd extension. Once you open a .c file, you will see the icon shown below. Click on the “Install” button to proceed with the installation.
Step 2: Generate compile_commands.json File
This step references other articles from the forum, which can be found in the citations at the bottom.
It only takes a few steps. First, make sure your scons version is 4.0 or above. You can check the version by executing scons — version. If your version is below 4.0, update the environment to the latest version (2.0) so that you can use it directly without needing to install anything.
# scons --versionSCons by Steven Knight et al.: SCons: v4.8.1.08661ed4c552323ef3a7f0ff1af38868cbabb05e, Tue, 03 Sep 2024 17:46:32 -0700, by bdbaddog on M1Dog2021 SCons path: ['J:\\06_software\\01_green_software\\env-windows-v2.0.0\\env-windows\\.venv\\Lib\\site-packages\\SCons']Copyright (c) 2001 - 2024 The SCons Foundation
Then, locate the SConstruct file in your commonly used BSP (Board Support Package) directory.
For example, I navigate to the following directory: bsp\stm32\stm32f103-blue-pill.
At the bottom of the SConstruct file, simply add the following two lines. After that, execute scons to generate the compile_commands.json.
env.Tool('compilation_db')env.CompilationDatabase()
The position for adding the lines is shown below.
objs.extend(SConscript(os.path.join(libraries_path_prefix, 'HAL_Drivers', 'SConscript'),variant_dir='build/libraries/'+'HAL_Drivers', duplicate=0))env.Tool('compilation_db')env.CompilationDatabase()# make a buildingDoBuilding(TARGET, objs)
Don’t forget to execute scons here to confirm that compile_commands.json can be generated successfully.
Step 3: Execute scons — target=vsc
The final step is simple: run scons — target=vsc to generate the workspace.
When you run this command, a code-workspace file will be created.
Step 4: Open the stm32f103-blue-pill.code-workspace file.
Finally, double-click on the stm32f103-blue-pill.code-workspace file. Once it’s opened in VSCode, you can start using it! At this point, all references will be based on what was compiled by SCons.
At this point, you can easily navigate as shown below, and when you search, it will also exclude other BSPs and libcpu.
Add Folders
If you want to view other folders, you can directly open stm32f103-blue-pill.code-workspace with VSCode. You can modify it to add the folders you need. If you want to add .git, simply delete the corresponding folder from file.exclude or set it to false.