# Setup C++ 20 VSCode environment on Ubuntu This article will set up following tools to develop with C++ 20 in VSCode. My Ubuntu version is 22.04. >gcc 13.1.0 / g++ 13.1.0 >clang-format >C/C++ Settings >C/C++ Advanced Lint ## GCC The default gcc on Ubuntu is only at 11, which is not suitable if you want to use C++ >=20 features like `std::format`. Source: [Install gcc 13 on Ubuntu 22.04 ](https://www.mindthink.me/2024/03/14/install-gcc-13-on-ubuntu-22-04/) 1. add the repository ``` sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test ``` 2. install gcc 13 ``` sudo apt-get install gcc-13 ``` 3. install g++ 13, because g++ 13 is not the same as gcc 13 ``` sudo apt-get install g++-13 ``` 4. update the default gcc/g++ version to 13 ``` sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 130 --slave /usr/bin/g++ g++ /usr/bin/g++-13 ``` 5. Verify the installed version ``` gcc --version ``` Following text should be shown. ``` gcc (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0 Copyright (C) 2023 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ``` The `gcc` and `g++` should be installed at `/usr/bin/gcc` and `usr/bin/g++` respectively. ## Clang-format Clang-format helps to format C++ code. Unformatted code harms readability! Install clang-format ```bash sudo apt install clang-format ``` Create a `.clang-format` in your project root directory, and paste the content from [this clang format template gist](https://gist.github.com/gelldur/d7bc3ea226aebcf8cc879df1e8524236). Try it on your unformatted code by clicking save! ## C/C++ Settings Press `Ctrl+Shift+P` to open the panel, then search C/C++ to find `C/C++ Edit Configurations (JSON)`, so we don't need to search for each setting, instead we just type it out. ![image](https://hackmd.io/_uploads/B1sY6tAklg.png) Paste the following contents to the opened json. The `/usr/include/c++/**` contains the c++ header files. If you have installed `gcc/g++ 13.1.0`, you will have `/usr/include/c++/13` in your computer. ```json= { "configurations": [ { "name": "Linux", "includePath": [ "${workspaceFolder}/**", "/usr/include/c++/**", "/usr/include/**" ], "defines": [], "compilerPath": "/usr/bin/g++", = "cppStandard": "c++20", "intelliSenseMode": "linux-gcc-x64" } ], "version": 4 } ``` ## C/C++ Advanced Lint `C/C++ Advanced Lint` is a vscode extension, please install it in your vscode. ![image](https://hackmd.io/_uploads/BJ7Wl5Rkxg.png) It would need a static code analyzer to work. The extension description has listed the supported analyzers, including `Clang`, `CppCheck` etc. Personally I choose `Clang`. Please install it with following command: ```bash sudo apt-get install clang ``` Same as above, we would edit the setting json to configure the linter. Before that, I would suggest saving the existing project as a vscode workspace, so the settings can be restricted to this project only. Press `Ctrl+Shift+P` to toggle the panel, then search for `save work` to save the workspace. Please save the workspace to the project root directory. After that, a [ProjectName].code-workspace file will be created. ![image](https://hackmd.io/_uploads/SJ2VJqA1xl.png) Open the workspace file, it is a json. Paste the following contents to it. The setting names are quite descriptive. For the clang executable, it is usually at `/usr/bin/clang`, if not, please find it with `whereis clang`. Since I only installed one static code analyzer, I disable others in the last few lines such as `"c-cpp-flylint.cppcheck.enable": false,`, this is to prevent the frequent warning about xxx analyzer is not found by the extension. ```json! { "folders": [ { "path": "." } ], "settings": { "C_Cpp.default.cppStandard": "c++20", "c-cpp-flylint.clang.enable": true, "c-cpp-flylint.clang.executable": "/usr/bin/clang", "c-cpp-flylint.debug": true, "c-cpp-flylint.clang.standard": ["c++20"], "c-cpp-flylint.includePaths": [ "/usr/include/c++/*", "/usr/include" ], "c-cpp-flylint.clang.includePaths": [ "/usr/include/c++/*", "/usr/include" ], "C_Cpp.default.intelliSenseMode": "linux-gcc-x64", "C_Cpp.inlayHints.autoDeclarationTypes.enabled": true, "c-cpp-flylint.cppcheck.enable": false, "c-cpp-flylint.flexelint.enable": false, "c-cpp-flylint.flawfinder.enable": false, "c-cpp-flylint.lizard.enable": false, } } ``` ## About Me A self-motivated programmer who is exploring low-latency C++. >Personal Website: https://jacky-chen-portfolio.vercel.app/ >GitHub: https://github.com/TypeErrorEngine2022 >Email: jackychenworkcontact@gmail.com