Tools for TSP-CSC5061: cmake, IDE, SonarLint, GoogleTest

Table of Contents

1 Introduction

This document gives precisions on the usage of the different tools used during TSP-CSC5061:

  • cmake
  • IDE Visual Studio or CLion, more precisely how to add files in a project generated by cmake
  • SonarLint
  • GoogleTest

2 cmake

All C++ project canvas given during TSP-CSC5061 are based on cmake. cmake goal is to configure project environment, so that you do not need to configure your IDE.

First, read instructions common to all IDE/OS, then read section corresponding to your IDE/OS.

2.1 Instructions common to all IDE/OS

  • Uncompress given archive (e.g. SampleSFML.zip in introduction exercise) in the directory of your choice.
  • Rename root directory of the directory you just extracted (e.g. SampleSFML)
  • In file name_of_new_toot_directory\CMakeLists.txt, in line project(nom_projet VERSION 1.0.0 LANGUAGES CXX), replace nom_projet by a name (without space) which corresponds to your project.
  • Go to section corresponding to your IDE/OS.

2.2 Clion (Linux)

  • In Clion, Menu File > Open and select absolute_path_to_your_project/CMakeLists.txt. Click on "OK": A window "Open Project" opens. Click on "Open as Project"
  • The loading of your project should run correctly in CLion, unless it is the first time that you run this operation in the context of TSP-CSC5061 and your project uses SFML library. In this case, in a terminal:

    cd absolute_path_to_your_project
    rm -Rf cmake-build-debug
    mkdir build
    cd build
    cmake .. # Do not forget the " .." after path to c++ compiler
    # If cmake displays one of the following error message :
    #    - "cmake not found"
    #    - "Error during cmake"
    #    - "No CMAKE_CXX_COMPILER"
    #    - "Could NOT find X11"
    # Type the following "sudo apt" command
    sudo apt -y install git cmake g++ libx11-dev libxrandr-dev libxi-dev libudev-dev mesa-common-dev libfreetype6-dev libopenal-dev libvorbis-dev libflac-dev
    cmake .. # Do not forget the " .." after path to c++ compiler
    # You should get the message "-- Build files have been written to: absolute_path_to_your_project". It means everything went OK.
    cd ..
    rm -Rf build
    
    • If you had to make all these instructions in a terminal, in CLion, menu Build > Rebuild Project
    • Menu Build > Build Project: This operation may take a while, in particular if your project uses SFML so that CLion has to download SFML. Eventually, CLion displays "Build finished": Everything went OK.
    • Menu Run > Run 'sample': Message window displays compilation information (the first compilation may take time, as CLion must compile the libraries). Eventually, window "SFML works !" opens:

      fenêtre_SFML_works image

      Figure 1: Window "SFML works!"

    • Click on the cross on top-left of the window to quit your application.

2.3 CLion (MacOS)

  • In Clion, Menu File > Open and select absolute_path_to_your_project/CMakeLists.txt. Click on "OK": A window "Open Project" opens. Click on "Open as Project"
  • The loading of your project should run correctly in CLion, unless it is the first time that you run this operation in the context of TSP-CSC5061 and your project uses SFML library. In this case, in a terminal:

    • In a terminal, type command brew install sfml
    • In CLion, menu Build > Rebuild Project
    • Menu Run > Run 'sample': Message window displays compilation information (the first compilation may take time, as CLion must compile the libraries). Eventually, window "SFML works !" opens:

      fenêtre_SFML_works image

      Figure 2: Fenêtre "SFML works!"

2.4 Visual Studio (Windows)

  • In Visual Studio, menu File > Open > CMake...: A window "Open CMake projects" opens.
  • Select CMakeLists.txt file in the directory of your project and click on "Open": Visual Studio output show that the IDE is processing. Eventually, it displays "End of CMake generation". Note:
    • This operation may take some time, in particular if your project is using SFML (thus Visual Studio needs to download SFML).
    • If cmake displays -- Configuring incomplete, errors occurred! with error messages, see if you do not have message error: could not find git for clone of sfml-populate or a message about a problem with git clone: This would mean that git is not installed on your OS or it is not recent enough:
      • Install latst version of https://gitforwindows.org/
      • Stop and start Visual Studio, open a developer command window in Visual Studio and type git --version (Doing so, you will be sure that Visual Studio takes into account the correct version of GitForWindows).
      • Restart the procedure.
    • cmake may display a warning concerning pugixml: This warning is normal.
  • Menu Build > Build all
  • When generation is over, left-click on the right of the button "Select a start element" (on the 2nd line of the menu bar) and select "sample.exe", "mainLauncher.exe" or "unitTests.exe"
  • Click on the button (or Ctrl-F5) to launch the chosen application: Your program runs. In the case of canvas SampleSFML, window "SFML works !" is displayed:

    fenêtre_SFML_works image

    Figure 3: Window "SFML works!"

  • Click on the red cross to close the program.
  • In "Microsoft Visual Studio debugging console", press a key to shut the console.

For your information, this procedure creates .vs and out directories in your project directory.

3 IDE CLion or Visual Studio : Add source files to a project generated by cmake

In the case you have to add source files, there is a special procedure to respect.

3.1 CLion (Linux ou MacOS)

With CLion, you can use creation menu for .h and .cpp files. Pay attention that CLion will also modify src/CMakeLists.txt to add the names of the added files. You can manually suppress these useless operations.

3.2 Visual Studio (Windows)

We assume that your project was created with menu File > Open > CMake...:

  • In Visual Studio, in solution explorer, right-click on src, then left-click on Add > Add a new element...: A window "Add a new src element" opens.
  • In left frame, select "Visual C++" and the type of file you want to create. Finally, click on button "Open": Your file is created in src directory.
  • Specify its name: A window "Preview of changes - CMake" opens.
  • Click on button "Cancel" (Indeed, Visual Studio proposes to add this new file to CMakeLists.txt ; This is useless,as CMakeLists.txt takes into account any new file thanks to line file(GLOB SOURCES CONFIGURE_DEPENDS *.h *.cpp)

4 SonarLint

  • Once you have installed SonarLint in your IDE (VisualStudio or CLion ; see installation procedure for each of these IDE), each time you open or save a source file, this file will be analyzed by SonarLint. Detected problems will be listed in IDE's error window as warnings.
  • If you want to disable some SonarLint warnings (e.g. warning "S106: Replace this usage of std::cout by a logger" which is too complex to solve for our "toy" applications), right-click on the rule (in VisualStudio error window or for CLion, in SonarLint subwindow) and select "Disable SonarLint rule".
  • If you want to reenable a rule
    • For VisualStudio
      • Menu Tools > Options: An Options window opens.
      • In search field, type SonarLint: The SonarLint submenu appears.
      • Select General and click on Edit rules settings: A settings.json file opens in VisualStudio file browser.
      • Replace "Off" by "On" for the rule you want to reactivate and save the file.
    • For CLion
      • Menu Window > Editor Tabs > Configure Editor Tabs: A Settings window opens.
      • Search for SonarLint and select the one which is in Tools submenu.
      • Click on Rules tab, then C++ and click on Filter button to select Show only Changed.
      • Reenable your rule.

5 Google Test and your IDE

Google Test is an environment to ease writing and execution of unit test. This section presents the use of Google Test depending on the IDE you are using. Note: We suppose that you have applied cmake on a project canvas containing unit tests.

5.1 With CLion (Linux ou MacOS)

  • Menu Build > Build Project: Output window shows teh processing done by CLion, then displays "Build finished"
  • Menu Run > Edit configurations...: A window "Run/Debug configurations" opens.
  • In the left frame of this window, unfold "GoogleTest", then select "UnitTests" and click on button "OK": This window closes.
  • Menu Run > Run 'unitTests': On the bottom of the screen, result of test is displayed with a synthesis (which you can unfold) in the left frame.
  • If the test revealed an error, in the right frame, click on the blue link signaling the erroneous line: CLion opens the file unitTests.cpp at the level of the erroneous unit test.

5.2 With Visual Studio (Windows)

  • In Visual Studio, menu Build > Build solution. After a moment, Visual Studio output window displays that the build is done.
  • Left-click on the right of the button "Select a start element" (on 2nd line of menu bar) and select "unitTests.exe".
  • Menu Test > Execute all the tests: A window "Test explorer" opens. After a few seconds, a red cross appears in front of the line unitTests (1): You passed your first unit test.
  • If a test reveals an error, in test explore, unfold line unitTest (1) and double-click on line TestName: Visual Studio opens the file unitTests.cpp at the level of the erroneous unit test.

Date: 01 october 2021

Author: Michel SIMATIC

Created: 2021-10-04 lun. 09:24

Validate