Arm Mbed OS support forum

What's the recommended way to setup vscode completion with mbed-tools?

Previously i had a config that relied on a forcedInclude of mbed_config.h that included all the defines, but such a file does not seem to be generated under cmake_build.

We use a combo of CMake-Tools and Clangd:

You’ll need to generate a compilation database with the following in your main CMakeLists.txt

# Generate compile commands database
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "")

Then we have the following settings, you’ll need to adapt to your project:

	"cmake.buildDirectory": "${workspaceFolder}/_build_cmake_tools",
	"cmake.configureArgs": [
		"-DTARGET_BOARD=LEKA_V1_2_DEV",
		"-DCMAKE_CONFIG_DIR=${workspaceFolder}/_build_cmake_tools/cmake_config"
	],
	"cmake.configureOnOpen": true,
	"clangd.checkUpdates": true,
	"clangd.arguments": [
		"--query-driver=/usr/local/bin/arm-none-eabi*",
		"--log=verbose",
		"--completion-style=detailed",
		"--clang-tidy",
		"--clang-tidy-checks=-*,modernize*"
	]

That works pretty well so far, so thank you! It’s too bad that this and the official C++ extension don’t like each other very much atm. I hope they fix it.

What is CMAKE_CONFIG_DIR for though?

By official, you mean this one? C/C++ - Visual Studio Marketplace

You’re not supposed to use both at the same time. C/C++ Intellisense works great as well if you project is not that big (though with mbed-os, it always big because there are so many files). IMHO it has the following drawbacks:

  • completion was not working all the time
  • completion doesn’t show the function/method arguments
  • the extension needs to index all the files of the project, which means all the files you don’t need from all the targets in mbed-os
  • it consumes a lot of CPU (on macOS at least)

With clangd, you don’t need indexing, as it uses the compilation database so only the files your actually using. You need to generate this database but with CMake this comes for free.

The completion is also a lot smarter, you get the argument that you can choose with another Tab key press.

The refactoring tools are quite nice, though I did not use the Intellisense ones so I can’t really compare.

Finally, you also get clang-tidy for free and this has had a very good impact on our code base! :slight_smile:

It’s a custom path variable that we use to put the cmake_config directory for different settings: build, unit tests and tools. You can have build and tools using the same (which is the default I think) but in our case:

  • we have a lot of targets and executables
  • the tools are configured a little differently (with the debug profile for example)
  • so separating both saves us some time