Personal template for starting a C++ project.
This template works under GNU+Linux distributions, however, this template should work under Windows and other UNIX based operating systems (e.g. macOS, FreeBSD, etc.) with the following utilities:
extern/
: Contains imported submodulesinclude/
: Contains header filessrc/
: Contains cpp source files.gitmodules
: Stores submodules repository informationCMakeLists.txt
: CMake configMakefile
: Makefile which contains tasks for CMake
.
+--extern
| +--...
+--include
| +--...
+--src
| +--main.cc
| +--...
+--.gitignore
+--.gitmodules
+--CMakeLists.txt
+--Makefile
+--README.md
You can use make
to run the compile process which is a make
task called
build
, this is the default task:
make build
or
make
However, the task build
will not run unless the output of CMake does not
exist. To recompile changes one must use the rebuild
task:
make rebuild
Like with make build
, you can use make
to run the compiled executable by
using the task called run
:
make run
We can clean the generated files and directories from our compiling by using the
clean
task:
make clean
This template uses the common methods of bring in libraries. In the
CMakeLists.txt
file:
- We must include the libraries using add_subdirectory statements for each library.
- We must link the includes and binaries of the libraries by defining:
- A target_include_directories block that contains paths to libraries include directories for each library.
- target_link_libraries statements for each library.
cmake_minimum_required(VERSION <cmake-version-req>)
project(
<project-name>
VERSION <version>
LANGUAGES <c-or-cxx>)
set(<var-name> <val>)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(SOURCE_DIR "${PROJECT_SOURCE_DIR}/src/")
set(INCLUDE_DIR "${PROJECT_SOURCE_DIR}/include/")
add_subdirectory(extern/<submodule>)
...
include_directories(<dir-with-includes>)
file(GLOB_RECURSE SOURCES <src-patterns>)
add_executable(${PROJECT_NAME} ${SOURCES})
target_include_directories(${PROJECT_NAME}
PUBLIC extern/<submodule>/include
...
)
target_link_libraries(${PROJECT_NAME} <submodule_binary_name>)
...