This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| pub:software:development:vscodium:cpp:get_started [2026/08/26 02:01] – created admin | pub:software:development:vscodium:cpp:get_started [2026/08/26 07:49] (current) – [Get started with VSCodium C++] admin | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Get started with vscodium | + | ====== Get started with C++ and VSCodium |
| - | This page describes how to get started with C++ development, | + | This page describes how to get started with C++ development, |
| + | |||
| + | |||
| + | ===== Creating a Hello World makefile project ===== | ||
| + | This section shows how to setup a Hello World project from scratch, to begin with without VSCodium, using a makefile for building the application with four targets: | ||
| + | * make | ||
| + | * make all | ||
| + | * make run | ||
| + | * make clean | ||
| + | |||
| + | |||
| + | ==== HelloWorld.cpp ==== | ||
| + | Copy paste the following lines into your command line: | ||
| + | |||
| + | cd ../ | ||
| + | mkdir HelloWorld | ||
| + | cd !$ | ||
| + | # NB. !$ repeats the 2nd part of the previous command (See bash history substitution) | ||
| + | mkdir build src | ||
| + | |||
| + | <code bash> | ||
| + | cat << ' | ||
| + | #include < | ||
| + | |||
| + | using namespace std; | ||
| + | |||
| + | int main(int argc, char** argv) | ||
| + | { | ||
| + | cout << "Hello World!" | ||
| + | } | ||
| + | EOF | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== makefile ==== | ||
| + | (NB. For the makefile, if necessary, use [[https:// | ||
| + | <code bash> | ||
| + | cat << ' | ||
| + | # | ||
| + | # A simple makefile for compiling a c++ project | ||
| + | # | ||
| + | |||
| + | # The compiler to use, per default this is already CXX for g++ | ||
| + | # as can be seen with "make -p", so no additional definition | ||
| + | # necessary for CXX (like defining: CXX = g++) | ||
| + | |||
| + | # compiler flags: | ||
| + | # -g adds debugging information to the executable file | ||
| + | # -Wall turns on most, but not all, compiler warnings | ||
| + | CCFLAGS = -g -Wall | ||
| + | |||
| + | # paths -L | ||
| + | LDFLAGS += | ||
| + | |||
| + | # libraries -l: | ||
| + | LDLIBS += | ||
| + | |||
| + | # Source directory | ||
| + | SRCDIR = src | ||
| + | |||
| + | # Output directory | ||
| + | BUILDDIR = build | ||
| + | |||
| + | # Find C source files recursively | ||
| + | SOURCES = $(shell find $(SRCDIR) -type f -name ' | ||
| + | |||
| + | # Generate object file names based on source file names | ||
| + | OBJECTS = $(patsubst $(SRCDIR)/ | ||
| + | |||
| + | # Main target | ||
| + | TARGET = HelloWorld | ||
| + | |||
| + | .PHONY: all | ||
| + | |||
| + | all: $(BUILDDIR)/ | ||
| + | |||
| + | # Note 1: PHONY is important here. Without it, implicit rules will try to build the | ||
| + | # | ||
| + | # Note 2: See https:// | ||
| + | # for the meaning of expressions like: @D, $<, and $@ | ||
| + | |||
| + | $(BUILDDIR)/ | ||
| + | $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@ | ||
| + | |||
| + | # Compile each source file | ||
| + | $(BUILDDIR)/ | ||
| + | @mkdir -p $(@D) | ||
| + | $(CXX) $(CCFLAGS) -c $< -o $@ | ||
| + | |||
| + | run: $(BUILDDIR)/ | ||
| + | @$(BUILDDIR)/ | ||
| + | |||
| + | clean: | ||
| + | rm -rf $(BUILDDIR)/ | ||
| + | EOF | ||
| + | </ | ||
| + | |||
| + | When done, try to make HelloWorld: | ||
| + | |||
| + | make | ||
| + | |||
| + | Which will default to the target make all. If no error appears, try to run: | ||
| + | |||
| + | make run | ||
| + | |||
| + | |||
| + | ===== Installing and setting up VSCodium ===== | ||
| + | This section describes how to install and setup VSCodium for writing C++ code. Once the installation/ | ||
| + | |||
| + | |||
| + | ==== VSCodium Installation ==== | ||
| + | VSCodium can be installed with apt on Debian/ | ||
| + | |||
| + | If you are using Ubuntu MATE with the global menu bar enabled, a system component (vala-appmenu-applet) crashes when placing VSCodium into the background with the keyboard shortcut ALT + TAB. When this happens the menu bar (mate-panel) suddenly disappears but quickly comes back. A workaround is to change in VSCodium the titlebar style to: native in vscode preferences. With the " | ||
| + | |||
| + | |||
| + | ==== VSCodium Configuration ==== | ||
| + | In order to work with and debug C++ code with VSCodium, two extensions, clangd and codelldb needs to be installed. The standard way to install an extension in VSCodium is to click on the Extensions tab and search and install accordingly. Alternatively, | ||
| + | |||
| + | Start VSCodium (from the command line by typing //codium//) press Ctrl + Shift + X and search and install the following extensions: | ||
| + | * clangd (llvm-vs-code-extensions) IDE features like autocompletion and bug detection for C++ using the Clang compiler | ||
| + | * CodeLLDB (vadimcn) A native debugger extension for VSCode/ | ||
| + | |||
| + | ^ // | ||
| + | | To kind of automate this and prevent VSCodium to download additionally a 32MB file once CodeLLDB is installed (and prevent a required restart of this extension), you can install all extensions directly from the command line. First download the vsix extensions. For this create a target directory for your extension (vsix) files. Use for example $HOME/ | ||
| + | codium --install-extension $HOME/ | ||
| + | codium --install-extension $HOME/ | ||
| + | </ | ||
| + | |||
| + | The extension clangd depends on the clangd language server [[https:// | ||
| + | |||
| + | Download clangd-linux-22.1.6.zip from [[https:// | ||
| + | |||
| + | And extract this to: | ||
| + | |||
| + | $HOME/ | ||
| + | |||
| + | Then configure the clangd.path in VSCodium to: | ||
| + | |||
| + | $HOME/ | ||
| + | |||
| + | ^ // | ||
| + | | < | ||
| + | # To execute, copy paste al lines in a terminal | ||
| + | CLANGDPATH=" | ||
| + | if [ -z " | ||
| + | echo " | ||
| + | CLANGDVER=" | ||
| + | if [ -n " | ||
| + | mkdir -p " | ||
| + | mkdir -p " | ||
| + | cd !$ | ||
| + | wget --quiet --show-progress -O " | ||
| + | unzip -d " | ||
| + | CLANGDPATH=" | ||
| + | else | ||
| + | echo " | ||
| + | sleep infinity | ||
| + | fi | ||
| + | fi | ||
| + | |||
| + | # $1: variable to configure (E.g. clangd.path ) | ||
| + | # $2: parameter to set (double quotes need to be included if required) | ||
| + | # $3: target file | ||
| + | replace_or_set_json_entry () { | ||
| + | # removing current entry from file | ||
| + | sed -i "/ | ||
| + | # add a line under the first curly brace _with_ a trailing comma | ||
| + | sed -i "/^{/a \ \ \ \ \" | ||
| + | # then remove only the very last comma | ||
| + | sed -i ': | ||
| + | } | ||
| + | |||
| + | modify_vscodium_settings () { | ||
| + | # take care of case when settings.json is empty | ||
| + | if [ ! -s " | ||
| + | printf " | ||
| + | fi | ||
| + | replace_or_set_json_entry " | ||
| + | replace_or_set_json_entry " | ||
| + | replace_or_set_json_entry " | ||
| + | } | ||
| + | |||
| + | # set clangd.path in VSCodium accordingly | ||
| + | if [ -f " | ||
| + | # verify leading { and trailing } brackets | ||
| + | if [ ! -s " | ||
| + | modify_vscodium_settings | ||
| + | else | ||
| + | printf " | ||
| + | fi | ||
| + | else | ||
| + | # file settings.json is not present | ||
| + | mkdir -p " | ||
| + | modify_vscodium_settings | ||
| + | fi | ||
| + | </ | ||
| + | |||
| + | Once done, continue with creating a C++ Hello World makefile project as described below. | ||
| + | |||
| + | |||
| + | ===== Using VSCodium with the makefile project ===== | ||
| + | Once the C++ code can be compiled and executed with the makefile, it is then possible to use VSCodium and assign the makefile accordingly. As a whole, VSCodium may offer some advantages for editing and troubleshooting your code. | ||
| + | |||
| + | To integrate a makefile project into VSCodium, it is necessary to provide a special (hidden) directory with the name .vscode, at the root of the project directory, with the following json files((Taken from: https:// | ||
| + | |||
| + | ^ Configuration file ^ Description | ||
| + | | [[https:// | ||
| + | | [[https:// | ||
| + | |||
| + | To create these files for the HelloWorld project, cd into the root directory of your HelloWorld project and copy-paste the following: | ||
| + | |||
| + | |||
| + | === # create a .vscode directory === | ||
| + | <code bash> | ||
| + | mkdir .vscode | ||
| + | </ | ||
| + | |||
| + | |||
| + | === # tasks.json === | ||
| + | <code bash> | ||
| + | cat << ' | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | }, | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | ] | ||
| + | } | ||
| + | EOF | ||
| + | </ | ||
| + | |||
| + | |||
| + | === # launch.json === | ||
| + | <code bash> | ||
| + | cat << ' | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | }, | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | ], | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | ] | ||
| + | } | ||
| + | EOF | ||
| + | </ | ||
| + | |||
| + | While still being at the root directory of your project, start VSCodium with providing the project path: | ||
| + | |||
| + | codium . | ||
| + | |||
| + | You should see something like the following window: | ||
| + | |||
| + | <columns 100% *100%*> | ||
| + | | {{: | ||
| + | | //VSCodium first screen after opening via command line"// | ||
| + | </ | ||
| + | |||
| + | If it looks differently, | ||
| + | |||
| + | |||
| + | ==== Build your project the first time ==== | ||
| + | Open the HelloWorld.cpp file by navigating to it. Then type Ctrl + Shift + B. It should now build your project, using your makefile. | ||
| + | |||
| + | To run your project without debugging, you can hit F5 (or Ctrl + F5). | ||
| + | |||
| + | |||
| + | ===== VSCodium - further steps ===== | ||
| + | Instead of using a makefile for the project, consider using cmake instead, because there is better support for it in vscodium. To understand how to use a cmake hello world project in vscodium, [[http:// | ||
| + | |||
| + | |||
| + | Also, there are more configuration files, which can be used for certain | ||
| + | ^ Configuration file ^ Description | ||
| + | | c_cpp_properties.json | ||
| + | | settings.json | ||
| + | |||
| + | |||
| + | |||
| + | References: | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | |||
| + | |||
| + | ===== Tips and Tricks ===== | ||
| + | ==== Using the format library ==== | ||
| + | If you need to use [[https:// | ||
| + | |||
| + | sudo apt update && sudo apt install libfmt-dev | ||
| + | |||
| + | Then include this into your project with the following include directive: | ||
| + | |||
| + | < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | using namespace std; | ||
| + | using namespace fmt; // Note that fmt::format or fmt::print will not work with V10 anymore as it did with v9 | ||
| + | |||
| + | int main(int argc, char** argv) | ||
| + | { | ||
| + | int a = 5; | ||
| + | cout << format(" | ||
| + | print(" | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | And in the makefile, inform the compiler about the usage of the fmt library by adding it to LDLIBS, like: | ||
| + | |||
| + | < | ||
| + | LDLIBS += -lfmt | ||
| + | </ | ||
| + | |||
| + | If you do not want or are unable to change the makefile, you can add "# | ||
| + | |||
| + | < | ||
| + | #define FMT_HEADER_ONLY | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | using namespace fmt; // Note that fmt::format or fmt::print will not work with V10 anymore as it did with v9 | ||
| + | |||
| + | int main() | ||
| + | { | ||
| + | print(" | ||
| + | } | ||
| + | </ | ||