Learning Resources
Online material for modern CPP.
Compact Data Structures - Setup

Learning Objective:
In this short guide you will learn how to set up your work environment for working through this nancourse.

DifficultyEasy
Duration30 Minutes
Prerequisite tutorialsNo prerequisites
Recommended reading


Software

Requirements:

  • gcc >= 7
  • cmake >= 3.4
  • wget

We will briefly explain how to install GCC-7 on some popular operating systems, but we recommend using the latest version of GCC available. For more information refer to your operating system's documentation.

Ubuntu >= 18.04

sudo apt install g++

Ubuntu < 18.04

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update
sudo apt install g++-7

MacOS using Homebrew

brew install gcc@7

MacOS using MacPorts

sudo port install gcc-7

Linux using conda

conda create -n gcc7 -c quantstack gcc-7 libgcc-7
conda activate gcc7

This will put GCC-7 in a separate environment called gcc7 which can be activated via conda activate gcc7 and deactivated via conda deactivate gcc7.

Note
Known Issue: If you encounter the error /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version 'CXXABI_1.3.11' not found, you have to set the LD_LIBRARY_PATH:
export LD_LIBRARY_PATH=/home/user/miniconda3/envs/gcc7/lib/
where /home/user/miniconda3/ is the path to your conda installation.
Attention
After installing, g++ --version should print the desired version. If not, you may have to use, for example, g++-7 --version or even specify the full path to your executable.

Similarly you need to install cmake, e.g. apt install cmake.

Directory Structure

For this project, we recommend the following directory layout:

nanocourse
├── build
└── source
├── CMakeLists.txt
└── hello_world.cpp

To create these directories, you can follow this script:

mkdir nanocourse
cd nanocourse
mkdir source build

Now we need to create a file named CMakeLists.txt inside source with the following contents:

cmake_minimum_required (VERSION 3.4)
project (nanocourse CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS_BASE "-pedantic -Wall -Wextra -Werror")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${CMAKE_CXX_FLAGS_BASE}")
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} ${CMAKE_CXX_FLAGS_BASE}")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${CMAKE_CXX_FLAGS_BASE}")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -march=native ${CMAKE_CXX_FLAGS_BASE}")
add_executable (hello_world hello_world.cpp)

Alternatively, you can download the file here.

Compiling and Running

To test whether everything works, we will now compile and run a small example.

First we create the file hello_world.cpp in the source directory with the following contents:

#include <iostream>
int main()
{
std::cout << "Hello world\n";
return 0;
}

Alternatively, you can download the file here.

To compile it, we run the following commands inside the build directory:

cmake ../source
make
./hello_world

The output should be Hello world.

Remarks
Depending on the standard C++ on your system, you may need to specify the compiler via -DCMAKE_CXX_COMPILER=, for example:
cmake ../source -DCMAKE_CXX_COMPILER=/path/to/executable/g++-7
Note
In some situations it can happen that the correct assembler is not found. You will see an error during the cmake configuration that says something like: ... could not understand flag m .... In this case you can try to export the Path:
export PATH=/util/bin:$PATH
and try running cmake again.

Adding a new source file to your project

For example, to add the new task1.cpp file to your CMakeLists.txt, append the following line to your file:

add_executable (task1 task1.cpp)

To compile the new file, run cmake . and make inside the build folder.