Cmake Cppcheck

/ Comments off

Setting up your development environment is not a quick task, but if done right it can save you hundreds of hours.

Example, compiling Cppcheck with cmake: mkdir build cd build cmake. If you want to compile the GUI you can use the flag.

  1. Cppcheck is a static analysis tool for C/C code. It provides unique code analysis to detect bugs and focuses on detecting undefined behaviour and dangerous coding constructs. The goal is to have very few false positives. Cppcheck is designed to be able to analyze your C/C code even if it has non-standard syntax (common in embedded projects).
  2. Cppcheck is an analysis tool for C/C code. It provides unique code analysis to detect bugs and focuses on detecting undefined behaviour and dangerous coding constructs. The goal is to detect only real errors in the code, and generate as few false positives (wrongly reported warnings) as possible.
Cmake

My preferred development environment for C++ is CLion running on Mac. I have also included details of my Windows development environment since lots of people use Windows. Visual Studio, Eclipse and other IDEs have similar features to CLion, so modify the instructions as needed to get the same functionality.

Cppcheck Github

Msys2 (Windows)

CLion relies on external programs for compilation which means you need to have g++ and gcc already installed. There are multiple options (MinGw, Cygwin, etc) but my preferred one is Msys2 which provides a unix like environment under Windows. It also helps install additional programs easily.

  1. Right Click on Start > System > Search for View Advanced System Settings > Environment Variables, Add new user variable HOME with %USERPROFILE% - this will make Msys2 start in the windows directory (if you do this after installing Msys2, move files from C:msys64homepisan to C:Userspisan manually https://github.com/valtron/llvm-stuff/wiki/Set-up-Windows-dev-environment-with-MSYS2
  2. Install Git for Windows https://git-scm.com/downloads
  3. Install MSys2 https://www.msys2.org/ on default directory C:msys64 Follow the instructions on the web page, especially about 'pacman -Syu' to update package database and core system
  4. pacman -S --needed mingw-w64-x86_64-emacs mingw64/mingw-w64-x86_64-gcc python3 mingw64/mingw-w64-x86_64-putty mingw64/mingw-w64-x86_64-cmake mingw-w64-x86_64-toolchain mingw64/mingw-w64-x86_64-cppcheck msys/unzip python3-pip mingw32/mingw-w64-i686-make msys/git
  5. pip install --upgrade pip
  6. pip install cpplint
  7. Run c:msys64mingw64.exe to get started and pin it to the taskbar for future
  8. Make sure the compilers are installed, try 'which g++' to find the location of g++
  9. You can configure Putty under Windows as well as ssh under Msys2 to use public/private keys to allow login without using password. Run C:Program FilesPuTTYputtygen.exe to generate a private key. Save it to your windows machine. Copy the 'public key' (starts with ssh-rsa ..). On the linux server, such as uw1-320-01.uwb.edu, edit the file ~/.ssh/authorized_keys and paste the 'public key' to this file. Configure Putty on windows to use your netid as the auto-login username (under Connection > Data) and Connection > SSH > Auth should have the private key file for authentication. You can now login to uw1-320-01 without entering a password. Repeat editing the authorized_keys file for each of the CSS Linux machines as needed.

Cmake Cppcheck Output

Mac

Mac relies on Xcode for compilers or you can install g++ and gcc via brew

  1. Install Xcode -- we need it for the command line tools
  2. Start XCode and agree to Terms and Conditions
  3. Open terminal use: 'xcode-select --install' This will install comman-line interface (CLI)
  4. Install homebrew
  5. Open terminal use: brew install valgrind cppcheck gcc make cmake emacs gdb wget python3
  6. pip3 install cpplint

CLion

Install CLion, assuming Msys2 has already been set as above for Windows or necessary programs installed for Mac

  1. Settings > Editor > Live Templates > CMake -- create a new shortcut named 'wall' as follows
  2. Clion add plugins cpplint and cppcheck
  3. Configure cppcheck
    • C:msys64mingw64bincppcheck.exe
      Cppcheck should give a warning for 'int *x = new int[100];' indicating memory leak
    • set cppcheck options
      --enable=all --inconclusive --language=c++ --std=posix --suppress=missingIncludeSystem
  4. Configure cpplint
    • python C:msys64usrbinpython.exe and cpplint at C:msys64usrlibpython3.7site-packagescpplint.py ( setting it to C:msys64usrbincpplint is reported not to work)
    • Cpplint should give a warning about no copyright message found at the top of file
    • set cpplint options (can also create a file CPPLINT.cfg in the project):
      --filter=-legal/copyright,-whitespace/tab,-build/namespaces,-build/include,-build/header_guard,-whitespace/newline,-whitespace/braces,-build/c++11,-runtime/threadsafe_fn
  5. Add Ctrl-R as a Clion shortcut for Run
  6. Ctrl-I as Auto-Indent Lines (instead or inaddition to Ctrl-Alt-I)
  7. Check that you can use different toolchaings
    1. g++ for regular Windows development
    2. wsl for compiling thinghs under ubuntu and using valgrind
    3. Visual Studio's compiler -- if necessary
  8. If you are using CATCH framework: Download Catch2 testing framework and integrate it with CLion following instructions at https://www.jetbrains.com/help/clion/catch-tests-support.html xx
  9. If you are using GitHub
    • Download gitignore for JetBrains.CLion from https://github.com/github/gitignore/tree/master/Global and save it to ~/.gitignore_global
    • File > Settings > Plugins -- enable GitHub and Git Integration plugins
    • File > Settings > Version Control > GitHub -- add your github account
    • File > Settings > Version Control > Git -- add executable to git program, something like c:msys64usrbingit.exe
    • Confirm that cpplint and cppcheck are working from CLion
    • Confirm that Using VCS menu option, files can be committed and pushed to repository
    • If you have installed WSL (Windows Subsystem for Linux)
      • Follow instructions on https://www.jetbrains.com/help/clion/how-to-use-wsl-development-environment-in-clion.html and https://blog.jetbrains.com/clion/2018/01/clion-and-linux-toolchain-on-windows-are-now-friends/ to setup ubuntu-clion connection
      • sudo apt-get install cmake gcc clang gdb build-essential
      • wget https://raw.githubusercontent.com/JetBrains/clion-wsl/master/ubuntu_setup_env.sh && bash ubuntu_setup_env.sh


Windows - New Computer

Cmake Cppcheck

Getting a new Windows computer ready.

Cppcheck Visual Studio

For some reason a new Windows computrer comes with all types of unnecessary stuff. These are all the steps I had to take to get my computer into a usable state. Some of them may be applicable to you.

  1. Preparation
    1. IMPORTANT. By default Windows Explorer hides file extensions. While this might be OK for a regular user, it is a nightmare for a developer where you enddp with multiple files with the same name: Readme.txt (a text file that can be opened and edited in Notepad or any text editor), Readme.pdf (PDF file that can be opened in Acrobat), Readme.exe (an executable file, can be run by double clicking, an unusual name for a program), README.cfg (probably a configuration file), Readme.md (a markdown file, can be edited by any text editor), ..
      Show file extensions - Search for “file explorer options” under Windows Setting from Start button. Full path in title bar, Uncheck hide extensions
    2. Download install Huskey OnNet. You need this to connect to CSS Linux Labs when you are not on the UW network. https://itconnect.uw.edu/connect/uw-networks/about-husky-onnet/
    3. Install basic version of FileZilla. Need this to transfer files to CSS Linux Labs. https://filezilla-project.org/download.php?type=client
    4. Uninstall Dell Mobile Connect, Candy Crush, Microsoft News, Minecraft, Netflix, weather, Xbox, Xbox Game Speech Windows, Xbox Live, Dropbox Promotion, Fitbit Coach, LinkedIn, Solitaire Collection, Mobile Plans
    5. Create a second admin account - eventually make this account a regular account and use the admin account when installing new software is required
    6. Windows Update - if you need it
    7. Add c:Userspisan to Quick Access in File Explorer
    8. Right Click on Start > System > Search for View Advanced System Settings > Advanced > Visual Effects -- modify as needed
    9. Right Click on Start > System > Search for View Advanced System Settings > Computer Name - somename -- need to reboot after this
    10. Create a restore point, Search “restore point”, allocate 1-2% for System, and create the first restore point
    11. Turn off Right Click on Start > System > Search for View Advanced System Settings > Remote Assistance
    12. (Search) Notifications and Actions, Disable most notifications
    13. Kill apps on shutdown - maybe, https://www.tomshardware.com/picturestory/853-windows-10-settings-you-should-change.html#s4
    14. Themes - set theme, back to colors to set the default color to blue rather than switching with theme/background
    15. Taskbar - remove people
    16. Sign-in options, create a pin
    17. Do not 'Use my sign-in to automatically finish setting up my device and reopen my apps after an update or restart.'
    18. Privacy - turn it all off
    19. Wifi - turn off 2.0, turn off allow another device to turn mobile hotspot on
    20. Video Playback - optimize for movie quality. If I am watching a movie, I’ll eventually plug things in
    21. Cortana - my cloud content off, device history off, no cortana on locked screen
    22. No web search from Cortane/local search - https://www.thewindowsclub.com/disable-web-search-in-windows-10 (did not work for me, testing https://pureinfotech.com/disable-cortana-windows-10/ via regedit)
    23. Disable onedrive starting at startup - from Task Manager, revisit later
    24. Turn off - account info for apps
    25. Turn off - apps accessing contacts, calendar, call history, tasks, email, text messages, radios, other devices, feedback and diagnostics
    26. Minimize background apps
    27. Don’t let apps access diagnostic info
    28. Automatic downloads off
    29. Start > Live Tiles, right click on each one and unpin from start. Once all the tiles are gone, use a mouse or trackpad to drag the right edge of Start all the way to the left.
    30. Settings > Personalization > Start - show app list on start menu
  2. Download programs
    1. Chrome -- set as default browser, sync settings
      1. Add a new person to chrome, UW
    2. Install Google Drive Stream for UW account
    3. Install Office 365 - UW subscription
    4. Install Visual Studio Community - project properties > c++ > precompiled headers - not using them. Remove pch.h and cpp
    5. Install Adobe Acrobat DC
    6. Install Microsoft Subsystem for Linux (WSL) such as Ubuntu from Windows Store https://www.microsoft.com/en-us/p/ubuntu/9nblggh4msv6?activetab=pivot:overviewtab
      1. sudo apt-get update
      2. sudo apt install g++, git, python3, python3-pip, cmake, cppcheck, emacs25, valgrind, cmake, gdb
      3. pip install cpplint
      4. Ubuntu can edit Windows files by accessing /mnt/c but Windows cannot access Ubuntu files -- learned it the hard way!
    7. Install CodeBlocks -- toolchain executables is in C:msys64mingw64bin
    8. Download Unity Hub
    9. github for desktop
  3. Pin to Taskbar: Chrome, Emacs, Msys2, putty, powershell
    1. For emacs, edit properties to start runemacs from C:msys64mingw64bin. Change emacs start directory to be c:
    2. For Msys2, edit properties to run C:msys64msys2_shell.cmd

General

Cppcheck Rules

CherrySim uses Cppcheck, a static code analysis tool that helps us to spot common coding errors quickly.

Prerequisite

Usage

Cppcheck is used in our pipeline. For the most accurate results for the internal development environment, it is recommended to do small commits often so that the pipeline spots error as soon as possible. If you want to execute Cppcheck locally however, you have the following options (both have limitations, see below):

  • (Recommended) Use the CMake GUI to analyze the Visual Studio Solution.

    • Copy cppcheck.cppcheck.template to cppcheck.cppcheck inside your Fruitymesh repository

    • Modify cppcheck.cppcheck.

      • All occurrences of PATH_TO_CMAKE_DIRECTORY must be replaced with your CMake build directory. For example, PATH_TO_CMAKE_DIRECTORY/CherrySim.sln may become D:/projects/cmakebuild/CherrySim.sln.

      • All occurrences of PATH_TO_FRUITYMESH_REPOSITORY must be replaced with the path of the Fruitymesh Repository. For example, PATH_TO_FRUITYMESH_REPOSITORY/cherrysim/json.hpp may become D:/projects/fruitymesh/cherrysim/json.hpp.

    • Open the CppCheck GUI

    • Make sure that the following is ticked: Edit → Preferences → General → 'Enable inline suppressions'

    • File → Open Project File → Select the modified cppcheck.cppcheck

    • If asked if the build dir should be created, click 'Yes'

    • Cppcheck now scans the repository. This might take some time.

  • (Advanced) Use CMake to execute Cppcheck on Windows.

    • According to the CMake docs, this is only supported with Makefile and Ninja Generators but will produce the closest result to the one of our pipeline. Cppcheck must be placed in your PATH variable.

    • Download and install clang 9.0 32-bit (part of LLVM). We highly recommend not changing the default installation path so you can directly use the commands down below as they are.

    • Create a sibling directory next to the Fruitymesh repository

    • Download boxcryptor. Inside that directory execute the following command in a terminal: cmake ./fruitymesh -G 'Unix Makefiles' -DFORCE_CPPCHECK=ON -D CMAKE_C_COMPILER='C:/Program Files (x86)/LLVM/bin/clang.exe' -D CMAKE_CXX_COMPILER='C:/Program Files (x86)/LLVM/bin/clang++.exe'. If you changed the default installation path of clang, replace the values of CMAKE_C_COMPILER and CMAKE_CXX_COMPILER to the paths to clang on your machine.

    • Execute the following and look out for the occurring warnings: cmake --build .

Limitations

  • Both approaches don’t mimic the behavior of the pipeline exactly, so both have the potential to show some warnings that are not on the pipeline and vice versa.