Clone, replace "Mylib" with the desired name, and it's done.
Or generate a new project with a script: https://github.com/izvolov/mylib-gen/blob/master/README.en.md
Building this project, like any other CMake project, consists of two stages:
cmake -S path/to/sources -B path/to/build/directory [options ...]
cmake --build path/to/build/directory [--target target]
cmake -S ... -B ... -DMYLIB_COVERAGE=ON [other options ...]
Turns on the coverage
target which performs code coverage measurement.
cmake -S ... -B ... -DMYLIB_TESTING=OFF [other options ...]
Provides the ability to turn off unit testing and hence the check
target. As a result, the code coverage measurement is also turned off (see Code coverage).
Also, testing is automatically disabled if the project is included to another project as a subproject using the add_subdirectory
command.
cmake -S ... -B ... -DDOXYGEN_OUTPUT_LANGUAGE=English [other options ...]
Switches the language of the documentation generated by the doc
target. For a list of available languages, see Doxygen site.
Default language is Russian.
cmake --build path/to/build/directory
cmake --build path/to/build/directory --target all
If a target is not specified (which is equivalent to the all
target), it builds everything possible including unit tests and also calls the check
target.
cmake --build path/to/build/directory --target mylib_library
Compiles the mylib_library
library. Enabled by default.
cmake --build path/to/build/directory --target mylib-unit-tests
Builds unit tests. Enabled by default.
cmake --build path/to/build/directory --target check
Launches built (and builds if not yet) unit tests. Enabled by default.
See also mylib-unit-tests
.
cmake --build path/to/build/directory --target coverage
Analyzes run unit tests (and runs is not yet) using gcovr.
Target is only available if MYLIB_COVERAGE
option is on.
See also check
.
cmake --build path/to/build/directory --target doc
Generates source code documentation using Doxygen.
cmake --build path/to/build/directory --target wandbox
The Wandbox service is used. Please, don't abuse it.
cmake -S path/to/sources -B path/to/build/directory -DCMAKE_BUILD_TYPE=Debug -DMYLIB_COVERAGE=ON
cmake --build path/to/build/directory --target coverage --parallel 16
cmake -S path/to/sources -B path/to/build/directory -DMYLIB_TESTING=OFF -DCMAKE_INSTALL_PREFIX=path/to/install/directory
cmake --build path/to/build/directory --target install
cmake -S path/to/sources -B path/to/build/directory -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=g++-8 -DCMAKE_PREFIX_PATH=path/to/installed/dependencies
cmake --build path/to/build/directory --parallel 4
cmake -S path/to/sources -B path/to/build/directory -DCMAKE_BUILD_TYPE=Release -DDOXYGEN_OUTPUT_LANGUAGE=English
cmake --build path/to/build/directory --target doc
One of the ways to use the module is to install it into the system.
cmake --build path/to/build/directory --target install
After that, all the libraries from the Mylib::
namespace can be used from any other project using the find_package
command:
find_package(Mylib 1.0 REQUIRED)
add_executable(some_executable some.cpp sources.cpp)
target_link_libraries(some_executable PRIVATE Mylib::library)
Mylib::headers
library is used for the headers only, and Mylib::library
library is used when it is also needed to link with the libmylib_library
library.
The project can also be used by another project as a submodule using the add_subdirectory
command:
In this case, libraries Mylib::library
and Mylib::headers
will be available in the same manner.
-
CMake 3.14
CMake 3.14 is required because of incorrect work of the command
install(TARGETS ... EXPORT ...)
: is does not set default install paths properly. -
doctest testing framework
Testing might be turned off (see Testing).
-
Switching the language of the generated documentation is provided by the
DOXYGEN_OUTPUT_LANGUAGE
option. -
Python 3 interpreter
Used to generate an online sandbox.
With CMake and a couple of good tools, you can get a static analysis with minimal effort.
CMake has build-in support for Cppcheck.
It is provided by the CMAKE_CXX_CPPCHECK
option:
cmake -S path/to/sources -B path/to/build/directory -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_CPPCHECK="cppcheck;--enable=all;-Ipath/to/sources/include"
After that, static analysis will be automatically run every time the source code is compiled and recompiled.
With scan-build
you can run static analysis easily, too:
scan-build cmake -S path/to/sources -B path/to/build/directory -DCMAKE_BUILD_TYPE=Debug
scan-build cmake --build path/to/build/directory
Here, unlike the case of Cppcheck it is required to build the project using scan-build
each time.