/cpp_tutorials

This repository contains my C++ snippets code on C++ concepts/ idioms, optimized C++, modern C++ and advance C++

Primary LanguageC++

build workflow alt text

This repository contains my C++ snippets code on C++ concepts/ idioms, optimized C++, modern C++ and advance C++. I have included snippets and sample code for using third-party libs to parse CSV, YAML, and JSON files. An example of code benchmarking with Google Benchmark is available. There is also a tutorial on using CMake to build and export your project.

Building and Installation

if you need to update your CMake:

Navigate to CMake's official website to get the link for the latest source tarball:

./bootstrap
make -j$(nproc)
sudo make install
sudo update-alternatives --install /usr/bin/cmake cmake /usr/local/bin/cmake 1 --force

How to build on your machine

configure it:

cmake -G "Ninja Multi-Config"  -S . -B build

or specify where to install it:

cmake -G "Ninja Multi-Config" -S . -B build -DCMAKE_INSTALL_PREFIX=/path/to/install

build it:

cmake --build build --config Release

or

cmake --build build --config Debug

or be more specific:

cmake --build build --target all --config Release

If you prefer preset use:

cmake --preset ninja-multi

and

cmake --build --preset ninja-multi-debug

or

cmake --build --preset ninja-multi-release

1. Building the Image

There is a docker file for this project that contains all dependencies and you build the image with:

docker build -t cpp_tutorials .

2. Creating the container

Create a container where you mount the checkout code into your container:

docker run --name <continer-name> -v <checked-out-path-on-host>:<path-in-the-container> -it <docker-image-name>

for instance:

docker run --name cpp_container -v /home/behnam/workspace/cpp_tutorials:/cpp_tutorials -it cpp_tutorials

3. Starting an existing container

If you have already created a container from the docker image, you can start it with:

docker start -i cpp_container

4. Removing unnecessary images and containers

You can remove unnecessary images and containers by:

docker image prune -a

docker container prune

GUI application with docker

  1. You need to run:

docker run --name cpp_container -v /home/behnam/workspace/cpp_tutorials:/cpp_tutorials --env="DISPLAY" --env="QT_X11_NO_MITSHM=1" --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" -it cpp_tutorials

  1. On the host run the following (every time you run your container):

export containerId=$(docker ps -l -q)

xhost +local: docker inspect --format='{{ .Config.Hostname }}' $containerId

read more here

Configure VSCode to use Ninja Multi-Config for all CMake projects

To configure VSCode to use CMake with the "Ninja Multi-Config" generator for all CMake projects, you can modify the VSCode settings. Here's how you can do it:

  1. Open your VSCode workspace or project.
  2. Press Ctrl + , to open the settings, or navigate to File > Preferences > Settings.
  3. In the settings search bar, type "cmake generator".
  4. Locate the "Cmake: Generator" setting.
  5. Click on "Edit in settings.json" or manually edit your settings.json file.

In your settings.json file, add or modify the "cmake.generator" setting to specify "Ninja Multi-Config" as the default generator for all CMake projects:

{
    "cmake.generator": "Ninja Multi-Config"
}

Save the settings.json file.

This change ensures that VSCode uses the "Ninja Multi-Config" generator by default for all CMake projects you work on in that workspace or project.