MY_LIB 0.1.0
Repository template for C++ projects
|
An opinionated template repository for C++20 static library.
I'm using it as a continuous learning process. Suggestions are always welcome !
This is for continuous benchmarking. Why in the same repo ? Long story short, having it in the same repo made it easier to include it the documentation. For now, my side project to generate charts from these benchmarks is not yet finished. The goal is to include this tool in CI, generate interactive charts, and add them to the documentation.A target called "run_benchmarks" will run benchmarks with some predefined settings and store the result inside your source dir :
benchmarks/data/...
.
Documentation example available here.
M.CSS was initially used, but I prefer Doxygen Awesome CSS. It was also easier to integrate interactive charts in it.
Go to the github repository and click on Use this template
Once the setup is done, replace the following identifiers.
my_lib
: cmake target, folder nameMY_LIB
: cmake project name and cmake variables prefixmy_namespace
: namespace, only in source files.my_lib_source
: in src/
my_lib_header
: in include/my_lib
If you wish to reuse parts of the README.md
, make sure to also replace :
TBlauwe/cpp_lib_starter
with your github repository (so badges are linked to correct workflows).https://tblauwe.github.io/cpp_lib_starter/
with your github pages link (so Documentation link
badges redirects to your site).Also, in the root CMakeLists.txt
, make sure to replace project information to yours. They will be used for the generated documentation.
It should be safe to do a "Replace All". Still, make sure that #include <my_lib/my_lib_header.hpp>
are correctly replaced.
That should be it for building the main target, tests and benchmarks. Documentation requires a bit more installation user-side.
You also probably don't need docs/pages/reference.md
.
The following CMake options are available:
MY_LIB_SKIP_DEPENDENCIES
: Disable automatic dependencies downloading with CPMtrue
, false
otherwise.CPM_MY_DEPENDENCY_VERSION
: Specify a dependency version. It is not something added by CPM but by me. It adds an option for overriding the version of a dependency. The value is a git tag, e.g master
, v3.12
, 1.0
, etc.Executables location are specified by variable ${PROJECT_EXE_DIR}
in the main CMakeLists.txt
. By default, it is set to : ${PROJECT_SOURCE_DIR}/bin
. Note that it is not prefixed by MY_LIB
. As of now, it is only set when the library is built as the main project. So I thought it would be nice to follow cmake convention like ${PROJECT_SOURCE_DIR}
.
Inside cmake/utility.cmake
, you will find several functions and macros. Most aren't noteworthy. They are mainly used to organise and build prettier output. They are by no means necessary. If you don't like them, feel free to skip them.
Still, here are some noteworthy functions/macros.
Define an option to set the version for a library.
Usage :
This will add a cmake option CPM_FMT_VERSION
set to 10.0.0
. It will then be used by the following macro :
Download a library using CPM. It is a wrapper of CPMAddPackage
and used the same. The reason to add this wrapper is to play nicely with options/version added through use_version
and also with cmake output.
Usage :
As you can see, it is almost exactly the same as CPMAddPackage
, but we didn't have to specify a version. The conjunction of these two macros/functions may and are most likely overkill/useless. Still, I like the readability of these cmake files and verbosity.
TARGETS
is a multi-value arguments where you can pass targets. By default, download_library
will try to suppress warnings when building those targets (or the target of the same name as NAME
if no TARGETS
are provided).
Sadly, suppressing warnings like this doesn't work for now (see this article).
To prevent this behaviour, you can pass the options NO_SUPPRESS_WARNINGS
, like so :
No need to pass options, default should be good. You can also use CMakePresets.json
instead, see below.
Available targets are :
my_lib
: main librarytests
: testsbenchmarks
: benchmarksrun_benchmarks
: run benchmarks through a python scriptdocs
: docsFor cross-platform compiling, we use CMakePresets.json
(see ref) to share settings.
<preset-name>
: name of a configuration preset, see below for a list.<preset-build-directory>
: build folder. By default and for all configuration presets, it is set to out/build/<preset-name>
.<a-target>
: name of a target, see above.Here are some configurations provided:
NOTE 1: Clang-cl
refers to the clang toolchain provided by Visual Studio 2022
Thanks to the structure of the file (credits to DirectXTK), you can easily add other configurations, by inheritings relevant configurations.
If you need to specify some cache variables for CMake, you can add them to the base
configuration :
Some configurations may not work if some binaries and libraries are not in your PATH
. For example, by default with Visual Studio 2022, all windows specific configurations works but GCC
. Vice-versa, only GCC
works in CLion but not the others (unless you tweak your path).
If you wish, they are some additional functionality that requires a bit more work from you.
By default, CPM download source files in the output directory. If you have several projects that use the same libraries, it may be favorable to download them in one place.
CPM documentation is far more comprehensive, but you can set the cmake variable CPM_SOURCE_CACHE
to an adequate location. Personally, I recommend to set it in your path, rather than in your .cmake files or in your preset.
By default and when configured as the main project, if CCache is installed, it will be activated. Otherwise it will be ignored
To install CCache on windows, you can use chocolatey (need elevated privileges) like so :
Documentation is built with Doxygen and Doxygen Awesome CSS
docs
: utility target to build the documentationopen_docs
: utility target to open docs without the hassle of finding it.Documentation is only built when the project is the main project !
Documentation is built through github actions and push in github pages when commiting on master. You don't need to setup your github pages, it's done automatically. If you wish to built it localy, the following tools are needed :
On Ubuntu :
On MacOs :
On windows using chocolatey (need elevated privileges) :
Make sure to add doxygen to your path !
To help you write docs, this page is a reference of some commands.
The library used for testing is Doctest.
One target is provided : tests
.
Tests are only configured when the project is the main project !
The library used for benchmarking is Google benchmark.
One target is provided benchmarks
.
If you want to pass more options to tune the benchmarking, see Google benchmark usage guide.
You can use provided run_benchmarks.py
python script, to run benchmarks with a predefined set of options.
Launch with -h
, if you need to see available arguments.
By default, benchmarks' results will be outputted to a file with following name :
For example :
You can specify the name with option -n
or --name
:
The goal is to be able to store results in the long run to compare evolution of performance.
To compare two benchmarks, you can use the following command :
Replace <baseline>
and <comparison>
with .json
files obtained when running your benchmarks.
To use this tools, requirement must be installed :
CMake:
Benchmarks:
Tests:
Documentation:
These are the main ressources I used to organize CMake files: