17 Ecosystem
17 Ecosystem
Programming
16. C++ Ecosystem
Cmake and Other Tools
Federico Busato
2024-11-05
Table of Contents
1 CMake
ctest
2 Code Documentation
doxygen
3 Code Statistics
Count Lines of Code
Cyclomatic Complexity Analyzer
1/41
Table of Contents
4 Other Tools
Code Formatting - clang-format
Compiler Explorer
Code Transformation - CppInsights
AI-Powered Code Completion
Local Code Search - ugrep, ripgrep, hypergrep
Code Search Engine - searchcode, grep.app
Code Benchmarking - Quick-Bench
Font for Coding
2/41
CMake
CMake Overview
CMake is used to control the software compilation process using simple platform and
compiler independent configuration files, and generate native Makefile/Ninja and
workspaces that can be used in the compiler environment of your choice
CMake features:
• Turing complete language (if/else, loops, functions, etc.)
• Multi-platform (Windows, Linux, etc.)
• Open-Source
• Generate: makefile, ninja, etc.
• Supported by many IDEs: Visual Studio, Clion, Eclipse, etc.
3/41
CMake Books
4/41
CMake - References
• Awesome CMake
• Useful Variables
5/41
Install CMake
6/41
A Minimal Example
CMakeLists.txt:
CMakeLists.txt:
project(my_project)
add_executable(program program.cpp)
if (VAR)
message("VAR is set, NUM is ${NUM}")
else()
message(FATAL_ERROR "VAR is not set")
endif()
$ cmake ..
VAR is not set
$ cmake -DVAR=ON -DNUM=4 ..
VAR is set, NUM is 4
...
[100%] Built target program 8/41
Language Properties
project(my_project
DESCRIPTION "Hello World"
HOMEPAGE_URL "github.com/"
LANGUAGES CXX)
cmake_minimum_required(VERSION 3.15)
add_executable(program ${PROJECT_SOURCE_DIR}/program.cpp) #$
# PROJECT_SOURCE_DIR is the root directory of the project
9/41
Target Commands
target_include_directories(program
PUBLIC include/
PRIVATE src/)
# target_include_directories(program SYSTEM ...) for system headers
add_executable(program program.cpp)
$ cmake -DCMAKE_BUILD_TYPE=Debug ..
11/41
Custom Targets and File Managing
project(my_project)
add_executable(program)
$ cmake ..
$ make echo_target
12/41
Local and Cached Variables
Cached variables can be reused across multiple runs, while local variables are only
visible in a single run. Cached FORCE variables can be modified only after the
initialization
project(my_project)
$ ccmake . # or 'cmake-gui'
14/41
Find Packages
add_executable(program program.cpp)
find_package(Boost 1.36.0 REQUIRED) # compile only if Boost library
# is found
if (Boost_FOUND)
target_include_directories("${PROJECT_SOURCE_DIR}/include" PUBLIC ${Boost_INCLUDE_DIRS})
else()
message(FATAL_ERROR "Boost Lib not found")
endif()
15/41
Compile Commands
project(my_project)
cmake_minimum_required(VERSION 3.15)
add_executable(program program.cpp)
16/41
ctest 1/2
CTest is a testing tool (integrated in CMake) that can be used to automate updating,
configuring, building, testing, performing memory checking, performing coverage
project(my_project)
cmake_minimum_required(VERSION 3.5)
add_executable(program program.cpp)
enable_testing()
set_tests_properties(Test2
PROPERTIES PASS_REGULAR_EXPRESSION "Correct") 17/41
ctest 2/2
ctest usage:
Each ctest command can be combined with other tools (e.g. valgrind)
18/41
ctest with Different Compile Options
It is possible to combine a custom target with ctest to compile the same code with
different compile options
add_custom_target(program-compile
COMMAND mkdir -p test-release test-ubsan test-asan # create dirs
COMMAND cmake .. -B test-release # -B change working dir
COMMAND cmake .. -B test-ubsan -DUBSAN=ON
COMMAND cmake .. -B test-asan -DASAN=ON
COMMAND make -C test-release -j20 program # -C run make in a
COMMAND make -C test-ubsan -j20 program # different dir
COMMAND make -C test-asan -j20 program)
enable_testing()
add_test(NAME Program-Compile
COMMAND make program-compile)
19/41
CMake Alternatives - xmake
20/41
Code
Documentation
doxygen 1/6
Doxygen is the de facto standard tool for generating documentation from annotated
C++ sources
Doxygen usage
• comment the code with /// or /** comment */
• generate doxygen base configuration file
$ doxygen -g
$ doxygen <config_file>
21/41
doxygen 2/6
22/41
doxygen 3/6
23/41
doxygen - Features 4/6
• Latex/MathJax $<code>$
/// @brief
void related_function(); 25/41
doxygen - Call Graph 6/6
26/41
Doxygen Alternatives
cloc counts blank lines, comment lines, and physical lines of source code in many
programming languages
$cloc my_project/
Features: filter by-file/language, SQL database, archive support, line count diff, etc. 28/41
Cyclomatic Complexity Analyzer - lyzard 1/3
CCN = 3
30/41
Cyclomatic Complexity Analyzer - lyzard 3/3
CC Risk Evaluation
1-10 a simple program, without much risk
11-20 more complex, moderate risk
21-50 complex, high risk
> 50 untestable program, very high risk
CC Guidelines
1-5 The routine is probably fine
6-10 Start to think about ways to simplify the routine
> 10 Break part of the routine
$ clang-format <file/directory>
32/41
Compiler Explorer (assembly and execution)
Compiler Explorer is an interactive tool that lets you type source code and see
assembly output, control flow graph, optimization hint, etc.
34/41
AI-Powered Code Completion 1/2
AI-Powered Code Completion tools help writing code faster by drawing context
from comments and code to suggest individual lines and whole functions
Common features:
• Semantic completion
• Recognize common language patterns
• Use the documentation to infer this function name, return type, and arguments
• Suggest bug fixes
• Generate comments, documentation, and even Pull Request text
35/41
AI-Powered Code Completion 2/2
They are commonly provided as plug-in for the most popular editors and IDE
• CoPilot
• TabNine
• Codeium
• Replit Ghostwriter
• CodeWhisperer
36/41
Local Code Search - ugrep, ripgrep, hypergrep
37/41
Code Search Engine - searchcode
38/41
Code Search Engine - grep.app
39/41
Code Benchmarking - Quick-Bench
40/41
Font for Coding
Many editors allow adding optimized fonts for programming which improve legibility
and provide extra symbols (ligatures)
Some examples:
• JetBrain Mono
• Fira Code
• Microsoft Cascadia
• Consolas Ligaturized
41/41