A guide for useful extensions and configuration examples to unleash the power of Visual Studio Code for C/C++ development. And damn, it's becoming better and better. With the use of extensions it's a really great tool that supports your everyday developing.
- 1. About the project
- 2. Getting started
- 3. CMake
- 4. Debugging
- 5. Testing
- 6. Docker
- 7. Documentation
- 8. Remote development
- 9. Static analysis
- 10. Code formatting & cleanup
- 11. Code Coverage
- 12. Integrate Clang sanitizers
- 13. Git integration
- 14. GitHub integration
- 15. Other nice extensions
- 16. Logfile handling
- 17. Some shortcuts and features
- 18. About CMake variants
- 19. References
This project should give an overview over some extensions and examples for C/C++ development with VS Code. There are a lot extensions around that really improve working with VCS. I want to show which of them are really valuable and give some examples how to use/integrate them.
Mostly i'm using CMake and Linux. So the main focus will be on this combination. Maybe i will extend this to the Windows-Subsystem for Linux (WSL) in future.
You can get the latest VS Code version at https://code.visualstudio.com/. Simply download and install the package type that's supported by system/distribution.
A detailed description how to install it under linux could be found here.
To make VS Code familiar with C++, i.e. language support, debugging, etc., you need the C/C++ extension found at the marketplace. Simply install it via the extension panel from the activity bar.
- An official overview of editing C++ with VS Code: https://code.visualstudio.com/docs/cpp/cpp-ide
- A general introduction to develop C/C++ with VS Code under Linux: https://code.visualstudio.com/docs/cpp/config-linux
How to manage CMake projects with VS Code could be found in the CMake section. If you're not keen on using CMake, you can have a look at https://code.visualstudio.com/docs/cpp/config-linux how to setup a plain compiling environment.
Some debugging related stuff can be found in the Debugging section.
VS Code has some nice extensions to handle testing very comfortable. See the Testing documentation for details.
The docker section describes how to use extensions for building, managing and running containers from VS Code.
Current topics:
- Installing docker extension
- Develop within docker and configure it with
devcontainer.json
- Using remote containerd instances
Some extensions can greatly enhance your documentation capabilities. See the documentation section for details.
If you are developing for remotely connected devices (e.g. RPi), the Remote - SSH extension saves a lot of time. It enables you to work with VS Code like you are used to work locally. The communication is done via SSH. So also autologin via certificates is possible.
Also a nice feature is the possibility to configure port forwarding if you are connected to the remote machine.
To use on-the-fly static analysis during development, see the Static analysis documentation.
The Code formatting & cleanup documentation shows some tools to do general automated cleanup and formatting of you're code.
To setup your CMake builds with coverage instrumentation and evaluate the coverage results, see the coverage documentation.
More about the integration of the Clang sanitizers could be found in the Clang sanitizer documentation.
See helpful extensions that are dealing with git in the Git documentation.
An overview how to use GitHub together with VS Could be found within the GitHub integration documentation.
There are several, generally supporting extensions that leverage your effort in various ways. See the Extension documentation for details on other supportive extensions that can enhance your development workflow.
Often you have to deal with logfiles for debugging purposes. If you know the analyze plugin from Notepad++ you will have fun with the Text Power Tools extension. Filtering larger text files, also with regular expressions, is possible with this nice plugin. Additionally you can restructure your data with an AWK like extraction.
Another nice extension that adds highlighting to logfiles is Logfile Highligher. It is costumizable and has a lot of nice features to support you digging through your logs.
The basic VS Code shortcuts can be found here
That is a really cool feature, especially for refactoring/renaming tasks. So don't miss to use it with opening the command palette (Ctrl+Shift+P
) and run Toggle column selection mode.
The cmake-variants.yaml
is a nice tool to setup different compiler instrumentations and generally changing the flags. But there's one problem currently. If you use the same CMake definitions in different combined variants, only the last one will be taken. E.g. if you enable coverage and the address sanitizer, only one of it will work because they share the same CMAKE_CXX_FLAGS
definition.
Another drawback is the use of the settings within a CI pipeline. So using an additional CMake include file might be a better approach. Then you can activate this over a variant definition and it it is also usable by a pipeline. I will provide an example if i got time to do it.
- More on VS Code, C/C++ & Linux: https://code.visualstudio.com/docs/cpp/config-linux
- Overview of C++ editing in VS Code: https://code.visualstudio.com/docs/cpp/cpp-ide
- Debugging on Linux with VS Code: https://code.visualstudio.com/docs/cpp/cpp-debug
- More on CMake variants and CMake extension: https://code.visualstudio.com/docs/cpp/cmake-linux#_select-a-variant
- CTest: https://cmake.org/cmake/help/latest/manual/ctest.1.html
- Installing docker: https://docs.docker.com/get-docker/
- Working with containers in VS Code: https://code.visualstudio.com/docs/containers/overview