A base C++ project built with documented CMakeLists.txt
This project is meant to be a ready-built setup that can be used to build a C++ project with a library, tests and executables that can be installed and exported for other users to import with find_package(CMakeDemo)
.
Tests are written with the Catch library.
There is a simple version of a find module in cmake
for the project, FindCMakeDemo.cmake
, that searches several environment variables and /usr/local
for installations of CMakeDemo
.
There is also a baseline CMakeDemo-config.cmake
file to support transitive dependencies when the project is found via find_package
.
- CMake >=3.1
- A C/C++ compiler (tested with gcc 7.5.0)
- Catch (v2.11.3 is included in
tests/
; overwrite with a more updated version)
- Fork the repo
- Add your source files to the project. Place source code in
src/
, header files ininclude/
, and test code intests/
. - Add the names of all source files in the project to the
CMakeLists.txt
in thesrc/
,include/
andtests/
directories. - Rename all instances of
CMakeDemo
andCMAKEDEMO
in all CMake-related files with your desired project name. This includes allCMakeLists.txt
and the files in `cmake/'. - Build, compile and run your code.
To compile the project, run the following in the project root:
mkdir build
cd build
cmake ..
make
To install the project in /usr/local/
, run the following in the build/
directory created above:
make install
To run unit tests via CTest, again run the following in the build/
directory:
make test
or
ctest
To add additional dependencies via find_package
, be sure to also include them in cmake/CMakeDemo-config.cmake
.
LAPACK
is included as an example. To import a project built with this template in another CMake project, be sure to add the cmake/FindCMakeDemo.cmake
file to that project's cmake/
directory.
File structure:
├── CMakeLists.txt
├── LICENSE
├── README.md
├── cmake
│ ├── CMakeDemo-config.cmake
│ └── FindCMakeDemo.cmake
├── include
│ ├── CMakeLists.txt
│ └── source_file.hpp
├── src
│ ├── CMakeLists.txt
│ └── source_file.cpp
└── tests
├── CMakeLists.txt
├── catch.hpp
└── test_cmake_demo.cpp
Helpful links: