C++ Setup

Starter

Jacob Xie published on
4 min, 714 words

Categories: Doc

Tags: C++

C++ and WSL

These documentation is completely followed by the official VsCode document.

Using VsCode to edit source code, compiling the source code on Linux using the g++ compiler and debugging on Linux using GDB. They are not installed by default on Ubuntu.

Environment

  1. update sys dependencies:

    sudo apt-get update
    

    or download the latest versions of the system packages as well by:

    sudo apt-get update && sudo apt-get dist-upgrade
    
  2. install GNU compiler tools and the GDB debugger:

    sudo apt-get install build-essential gdb
    
  3. verify g++ and gdb:

    whereis g++
    whereis gdb
    

After setting up the environment, create a .cpp file and vscode will pop up a hint window to ask you to install C/C++ extensions. Let's omit this step and head forward to the next step.

Alternative

  1. Install the desired version:

    sudo apt install gcc-10
    sudo apt install g++-10
    
  2. Switch to the version:

    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 10
    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 10
    
  3. Test with:

    gcc --version
    g++ --version
    

Build

Choose Terminal > Configure Default Build Task and in the dropdown select C/C++: g++ build active file. A tasks.json file should be created in the .vscode folder. Learn more tasks.json variables in the variables reference.

Run

Choose Tasks: Run Build Task and will see an executable file after the build is done.

Debug

Press F5 and choose C++ (GDB/LLDB), then choose g++ build and debug active file from the dropdown. This will create a launch.json file in the .vscode folder. More details please visit debug-helloworldcpp.

Configurations

In order to get more control over the C/C++ extension, we can create a c_cpp_properties.json file in .vscode folder via C/C++: Edit Configuration (UI) command.

CMake

  1. dependencies:

    sudo apt-get install build-essential libssl-dev
    
  2. check the latest released version and download:

    wget https://github.com/Kitware/CMake/releases/download/v3.25.1/cmake-3.25.1.tar.gz
    
  3. extract:

    tar -zxvf cmake-*
    
  4. install:

    cd cmake-3.25.1
    ./bootstrap
    
  5. make and install:

    make
    sudo make install
    
  6. check installed version:

    cmake --version
    

Create a CMake project

reference

Clangd

Installation

Check version: clangd --version

Clang-format:

apt install clang-format

VsCode settings.json:

{
   "editor.formatOnSave": false,
   "clangd.path": "/opt/clangd_18.1.3/bin/clangd",
   "clangd.arguments": [
      "--compile-commands-dir=${workspaceFolder}/build",
      "--completion-style=detailed",
      "--header-insertion=never"
   ],
}

Useful packages/tools

Catch2

git clone https://github.com/catchorg/Catch2.git
cd Catch2
cmake -Bbuild -H. -DBUILD_TESTING=OFF
sudo cmake --build build/ --target install

Arrow

Check the official site for detailed info.

Ubuntu version:

sudo apt update
sudo apt install -y -V ca-certificates lsb-release wget
wget https://apache.jfrog.io/artifactory/arrow/$(lsb_release --id --short | tr 'A-Z' 'a-z')/apache-arrow-apt-source-latest-$(lsb_release --codename --short).deb
sudo apt install -y -V ./apache-arrow-apt-source-latest-$(lsb_release --codename --short).deb
sudo apt update
sudo apt install -y -V libarrow-dev # For C++
sudo apt install -y -V libarrow-glib-dev # For GLib (C)
sudo apt install -y -V libarrow-dataset-dev # For Apache Arrow Dataset C++
sudo apt install -y -V libarrow-dataset-glib-dev # For Apache Arrow Dataset GLib (C)
sudo apt install -y -V libarrow-flight-dev # For Apache Arrow Flight C++
sudo apt install -y -V libarrow-flight-glib-dev # For Apache Arrow Flight GLib (C)
# Notes for Plasma related packages:
#   * You need to enable "non-free" component on Debian GNU/Linux
#   * You need to enable "multiverse" component on Ubuntu
#   * You can use Plasma related packages only on amd64
sudo apt install -y -V libplasma-dev # For Plasma C++
sudo apt install -y -V libplasma-glib-dev # For Plasma GLib (C)
sudo apt install -y -V libgandiva-dev # For Gandiva C++
sudo apt install -y -V libgandiva-glib-dev # For Gandiva GLib (C)
sudo apt install -y -V libparquet-dev # For Apache Parquet C++
sudo apt install -y -V libparquet-glib-dev # For Apache Parquet GLib (C)