- CMakePresets.json defines various debug and release configurations, including the compiler flags.
scriptsdirectory contains scripts to build, test, and benchmark the project.
git clone git@github.com:linem-davton/cpp-playground.git
cd cpp-playground
mkdir build
./scripts/install_dep.sh # install dependencies
./scripts/build.sh # build the project
./scripts/test_all.sh # run all tests
./scripts/bench_all.sh # run all benchmarks , can be time consuming
The executables are generated in the build/{preset}/bin directory.
Install Script installs the following requirements (apt package manager). Perf is not available on WSL2. Works on native Linux.
- Place the header files in the
includedirectory. - Place the source files in the
srcdirectory, or in a new subdirectory in thesrcdirectory. - Configure CMake by Adding the following to the CMakeLists.txt file in
srcdir.
add_executable(a.out a.cpp) # add executable
target_link_libraries(a.out PRIVATE <libs>) # link libraries- Add header files in the
includedirectory. - Place the source files in a new subdirectory in the
srcdirectory. - Configure CMake by Adding the following to the CMakeLists.txt file in
srcdir or create a newCMakeLists.txtfile in the newly created subdirectory.
add_library(lib_name STATIC lib.cpp <other.cpp>) # <other.cpp> is placeholder for other source filesNote
The project uses Google Test for testing.
- Place the test files in the
testsdirectory. - Add the following to the CMakeLists.txt file in the
testsdirectory to build the test.
add_executable(test_name test.cpp) # add test
target_link_libraries(test_name PRIVATE <libs> GTest::gtest_main) # <libs> is place holder for other libsSee testing.md for more.
The project uses Google Benchmark for benchmarking. Place the benchmark files in the benchmarks directory.
Add the following to the CMakeLists.txt file in the benchmarks directory to build the benchmark.
add_executable(benchmark_name benchmark.cpp) # add benchmark
target_link_libraries(benchmark_name PRIVATE <libs> benchmark::benchmark) # <libs> is place holder for other libsSee benchmark.md for more.
- Add the benchmark to the list of benchmarks in
bench_all.shscript.
I have not be able to get perf to work on WSL2. Works on native Linux.
perf stat ./matrices # get performance statistics
perf stat -e cache-references,cache-misses,LLC-loads,LLC-load-misses ./matrices # get cache statisticsSample perf stat outputs for matrix multiplication, in the matrices directory.
Build with -fno-omit-frame-pointer to disable frame pointer optimization, to make call graphs more accurate.(Not tested if disabling frame pointer optimization affects the call graph accuracy.)
# Call graph
perf record --call-graph dwarf -F 99 ./a.out # record call graph
perf script | speedscope - # Vizualize the call graph and flame graphPerf benchmarks for matrix multiplication are available in the matrices directory.
Speedscope: https://www.speedscope.app/
strace ./a.out # trace system calls
strace -c ./a.out # get summary of system calls
strace -f ./a.out # trace child processes
strace -e trace=open,close ./a.out # trace only open and close system calls
strace -e trace=network ./a.out # trace only network system calls
strace -k ./a.out # print call stacksudo apt install heaptrack
heaptrack ./a.out # get heap memory statistics
heaptrack --analyze heaptrack*.zst # analyze the heap memory statisticsHeaptrack is a heap memory profiler that tracks all memory allocations and de-allocations in a program. Gui is available in the heaptrack-gui package.
To see how many times a line was executed in the program.
Must compile with --coverage flag.
g++ a.cpp -o a.out --coverage # creates a.out and .gcno file
./a.out # generates coverage file , .gcda
lcov --capture --directory . --output-file coverage.info # Capture coverage data from curr and sub dirs .gcda and .gcno files
genhtml coverage.info --output-directory out # generate html report
google-chrome out/index.html # open the html reporTip
To remove third party code (gtest) and systems libraries from the coverage report, use the
lcov --capture --directory . --output-file coverage.info --no-external --exclude '_/googletest/_' command.
Google Benchmark is a microbenchmark library for C++. Test performance of functions.
./matrices_benchmark --benchmark_filter=BM_MatrixMul # run only the benchmark with the name BM_MatrixMulDetailed documentation is available in the docs directory.