Jean-Baptiste Keck – Personal website

M1 SIAM

Introduction to C++

Lab sessions teaching materials

Lab Solutions:

FAQ Quick Jump:

  1. Where can I get access to the course handouts and lab instructions ?
  2. I'm new to linux, how can I get started ?
  3. What text editor should I use to code ?
  4. How to write a generic Makefile ?
  5. How to build my code without using the terminal ? (updated for codeblocks)
  6. How to work on my personal computer ?
  7. How to work remotely from home ?
  8. How to download my code from the school server ?
  9. How to document my code with doxygen ?
  10. How to debug segfaults and memory leaks ?
  11. How to debug my code ?
  12. Where can I find a free book to learn C++ ? (new)

Where can I get access to the course handouts and lab instructions ?

Everything is available on Laurence Pierre personal website.

I'm new to linux, how can I get started ?

Here is a short article on how to use the linux terminal and the basics to work with directories and files.

What text editor should I use to code ?

Please do not use gedit for C++ projects, you will miss all modern features a decent text editor should have.
I would recommend one of those three editors:

Atom is already installed on school computers, you can launch it with the following command in a terminal:

  • atom

Here is an example of what you will get for free with Atom (Tab-completion):

Autocompletion in Atom

Of course if you are already using vim or emacs, you can stick with it !

How to write a generic Makefile ?

Here is a sample Makefile that should work for every exercices.
  1. SRC=src
  2. INC=include
  3. BIN=bin
  4. SRCS=$(wildcard ${SRC}/*.cpp)
  5. OBJS=$(patsubst ${SRC}/%.cpp, ${BIN}/%.o, ${SRCS})
  6. TARGET_BINARY=${BIN}/main #[replace by target_binary_name]
  7. CXX ?= g++ # or clang++
  8. CXXFLAGS += -I${INC} -std=c++14 -pedantic
  9. CXXFLAGS += -Wall -Wextra -Wno-unused
  10. all: ${TARGET_BINARY}
  11. ${TARGET_BINARY} : ${OBJS}
  12. ${CXX} $^ -o $@
  13. ${BIN}/%.o: ${SRC}/%.cpp
  14. ${CXX} ${CXXFLAGS} -c $< -o $@
  15. .PHONY: clean
  16. clean:
  17. rm -f ${OBJS}
  18. rm -f ${TARGET_BINARY}

This Makefile enables C++14 and extra warnings to help you to debug your code.
It should be placed in the root folder that contains src, include and bin (unlike the one in the first lab).

Example usage:

  • make
  • make clean
  • CXX=clang++ make
  • CXXFLAGS=-Wsign-compare make

How to build my code without using the terminal ?

Every editor should provide a way to build and run external programs.
Here is how to call make within some editors:
  • Atom:
    1. Go to Edit > Preferences > +Install
    2. Search and install the following packages (and dependencies):
      • build
      • build-make-file
    3. Use F7 to choose build target (all or clean for example).
    4. Use F9 to build.
  • Sublime Text:
    1. Go to Tools > Build System and tick Make.
    2. Open the Makefile in sublime and make it active.
    3. Use Ctrl+b to build.
  • Vim:
    1. Make is a builtin feature of vim, just use Esc:make
  • Codeblocks
    1. Create a C++ console project:
      • File > New > Project > Console application
      • Click on Go, select C++, click on Next.
      • Choose a project title, for example exercise1.
      • Carefully check 'Folder to create project in' and 'Resulting filename'.
      • Click on Next.
      • Select GNU GCC Compiler and click on Finish.
    2. Import .cpp and .h files if any:
      • Rightclick on project name and use Add files.
      • You can create empty files by using File > New > Empty file.
    3. Add your custom Makefile as build target:
      • Rightclick on project name and click on Properties.
      • In Project settings under the Makefile entry, tick 'This is a custom Makefile'.
      • In Build targets, click on Add, enter 'all' and click Ok.
      • Delete the Debug and Release build targets.
      • Modify the 'Type' entry of your new target to Console application.
      • Modify the 'Output filename' entry of your new target to the name of the output executable given by your Makefile (bin/ex1_date for the first exercice of first lab).
      • Finally click on Ok to close the Project properties.
      • Execute the program by using a terminal.
    4. Modify the Makefile:
      • Rename the clean rule to cleanall.

How to work on my personal computer ?

The only dependencies for the labs are make and a C++ compiler (gcc or clang).
You need root access on your machine to be able to install them.
  • Mac OS X: You need to install Xcode and the command line tools, see this link.
  • Linux (ubuntu, arch, debian, centos, ...):
    • sudo apt-get update
    • sudo apt-get install make g++ clang
    If apt-get is not available, use the package manager offered by your operating system.
  • Windows 10: You need to install WSL (Windows Linux Subsystem), this will require ~700MB of free space.
    1. First fully update your Windows 10.
    2. Got to the Microsoft Store and search for Ubuntu 18.04 LTS.
    3. Install it.
    4. Open the Windows Powershell with administrative rights.
      To open it as administrator look for it with the search bar and then use Ctrl + Shift + Left Click.
      Once launched, enter his command to enable the linux subsystem:
      • Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
    5. If the command fails, this means you did not run the powershell as administrator.
    6. You will be prompted to reboot, juste type Y to reboot.
    7. Launch Ubuntu 18.04 LTS, you will have to choose a login and a password.
    8. You will get access to a linux terminal.
    9. Fully upgrade the linux subsystem and install what we need with the following commands:
      • sudo apt-get update
      • sudo apt-get full-upgrade
      • sudo apt-get install make g++ clang
    10. From here you can use ssh and scp to get your code from the school server.
    11. Create a shortcut to your linux home folder:
      1. Navigate to C:\Users\[windows username]\AppData\Local\Packages\ CanonicalGroupLimited.Ubuntu18.04onWindows_[...]\LocalState\rootfs\home
      2. Right click on the folder named as the linux login you just chose and send it to your Desktop (this will create a shortcut to your linux home folder on your Windows desktop).
    12. Open you project folder in your favourite text editor for Windows (Atom for example) by using this shortcut.
    13. Use make and execute binaries in the linux terminal.
In a terminal you can try
  • g++ --version
  • clang++ --version
  • make --version
to see if installation was successful.

How to work remotely from home ?

You can access the school server mandelbrot from anywhere by using the ssh protocol.
  1. If using Windows, install an use a GUI ssh client (for example PuTTY).
  2. Connect to the server mandelbrot.e.ujf-grenoble.fr with your usual logins.
On a linux machine this would give (replace username with your login):
  • ssh -X username@mandelbrot.e.ujf-grenoble.fr
You will get access to all the files stored in your home folder through a terminal. The -X option will grant you a GUI environment on the remote server, this means that you will be able to launch graphical applications as if you were using the computer locally.
Update: Atom does not work on mandelbrot, you can only use gedit, emacs and vim (and it is really slow).

How to download my code from the school server ?

You use scp (secure copy protocol) or sftp (ssh file transfer protocol) from and to the school server.
  1. If using Windows, install and use a GUI sftp client (for example FileZilla).
  2. Connect to mandelbrot.e.ujf-grenoble.fr with your usual logins.
If your code is located at ~/Documents/CPP/TP1 (on the school server) and you want to get a copy on your personal computer, just execute this command on your machine:
  • scp -r username@mandelbrot.e.ujf-grenoble.fr:~/Documents/CPP/TP1 .
This will copy the remote source folder recursively into the current local directory.

How to document my code with doxygen ?

  1. In your project folder (where you have the src and include folders for example), create a docs folder, a README.md file and a Doxyfile with the following commands:
    • mkdir docs
    • touch README.md
    • doxygen -g
    Your folder should now contain:
         bin docs Doxyfile include Makefile README.md src
  2. Modify the Doxyfile with your favorite text editor and edit the following variables:
    • PROJECT_NAME: Project name
    • PROJECT_BRIEF: Project description
    • OUTPUT_DIRECTORY: ./docs
    • INPUT: ./README.md ./src ./include
    • USE_MDFILE_AS_MAINPAGE: ./README.md
    • EXTRACT_PRIVATE: YES
  3. Add the following rules to your Makefile:
    1. DOCS=docs
    2. doc:
    3. doxygen
    4. html: doc
    5. $(BROWSER) ${DOCS}/html/index.html
    6. pdf: doc
    7. $(MAKE) -C ${DOCS}/latex
    8. evince ${DOCS}/latex/refman.pdf
    Those rules are doing the following:
    • make doc will build the documentation using doxygen and compile the generated latex files.
    • make html will open your documentation in your browser.
    • make pdf will open your documentation with a pdf viewer.
  4. Try it, you should get a basic documentation with the title of your project.
  5. Add doxygen comments in your code as documented here.
    You can put doxygen documentation in both your headers and your source code files.
  6. The main page of your documentation is determined by the markdown file README.md.
    Here is a sample README.md for the first exercice of lab 2:
    1. Daily trip price calculator {#mainpage}
    2. ===========================
    3. About
    4. -----
    5. This amazing program computes the average price of a trip spanning
    6. between two user given dates.
    7. Build
    8. -----
    9. You will need a C++14 compatible compiler.
    10. To build this code you just need to do the following:
    11. mkdir bin
    12. make -jN
    13. where N is the number of available processes.
    14. Documentation
    15. -------------
    16. To build this documentation you can use the following commands:
    17. * `make doc`: will build the documentation.
    18. * `make html`: open the `html` documentation in your browser.
    19. * `make pdf`: open the `pdf` documentation using `evince`.
    20. Program usage
    21. -------------
    22. `./bin/ex1_trip [sday] [smonth] [syear] [eday] [emonth] [eyear]`
    23. Quick documentation overview:
    24. -----------------------------
    25. Here are the two main classes used by this project:
    26. 1. #Date ([date.h](@ref date.h) and [date.cpp](@ref date.cpp)):
    27. Implementation of basic dates functionalities
    28. 2. #Trip ([trip.h](@ref trip.h) and [trip.cpp](@ref trip.cpp)):
    29. Implementation of trips.
    30. Some utility function are hidden in [utils.h](@ref utils.h)
    31. and [utils.cpp](@ref utils.cpp).
    32. The main function is hidden in [main.cpp](@ref main.cpp).

How to debug segfaults and memory leaks ?

On linux you can use valgrind:
  1. Add the debug flag -g to your CXXFLAGS variable in your Makefile.
  2. Recompile your whole project (clean and build):
    • make clean && make
  3. Launch your programm with valgrind
    • valgrind [binary] [args]

How to debug my code ?

On linux you can use gdb:
  1. Add the debug flag -g to your CXXFLAGS variable in your Makefile.
  2. Recompile your whole project (clean and build):
    • make clean && make
  3. Debug your programm with gdb
    • gdb -tui --args [binary] [args]
  4. Type run to execute your program.

Where can I find a free book to learn C++ ?

You can use the 'C++ Primer' which is freely available here.