CMake requires a CMakeLists.txt file in the root directory of the project. This file contains the project name. The CMakeLists.txt file can also include other CMakeLists.txt files in subdirectories with source files to build. Configuration and build options can be specified in the CMakePresets.json file.
Define the project name and language in CMakeLists.txt file.
cmake_minimum_required(VERSION 3.23)
# Specify project and language
project(bit)
# Build the test
if(BIT_BUILD_TEST)
enable_testing()
# Create target for testing
add_executable(${PROJECT_NAME}-test)
# Add a test to the project to be run by `ctest()`
add_test(NAME ${PROJECT_NAME}-test COMMAND ${PROJECT_NAME}-test)
# External dependencies
find_package(GTest CONFIG REQUIRED)
# Link the static libraries to this target
target_link_libraries(
${PROJECT_NAME}-test PRIVATE GTest::gtest GTest::gtest_main GTest::gmock
GTest::gmock_main ${PROJECT_NAME})
# Specify the required C++ standard for this target
target_compile_features(${PROJECT_NAME}-test PUBLIC cxx_std_20)
endif()
# Add source directories
add_subdirectory(src)
src/CMakeLists.txt
Define the source files and target in src/CMakeLists.txt file.
Compiler Cache for C/C++, Rust with cloud storage support (AWS S3, Google Cloud Storage, Azure Blob Storage).
Files and Folders
CMakePresets.json
Define the cache variables in CMakePresets.json file. The cache preset can be inherited by other presets.
"configurePresets": [
{
"name": "cache",
"displayName": "Compiler Cache",
"description": "Avoid recompilation for the same C and C++ object files by fetching result from a cache directory",
"hidden": true,
"cacheVariables": {
"CMAKE_C_COMPILER_LAUNCHER": "ccache",
"CMAKE_CXX_COMPILER_LAUNCHER": "ccache"
}
}
]
1.1.3. Sanitizer
Sanitizers are tools that help find memory errors, undefined behavior, and other problems in C and C++ source code.
ThreadSanitizer (TSan) is a data race detector for C/C++. A data race occurs when two threads access the same variable concurrently and at least one of the accesses is write.
UndefinedBehaviorSanitizer (UBSan)
UndefinedBehaviorSanitizer (UBSan) is a fast undefined behavior detector. It finds undefined behavior bugs in C and C++ programs.
A lightweight system for writing, administering, and running unit tests in C.
Files and Folders
CMakeLists.txt
The CMakeLists.txt file with test framework variables. The BUILD_TEST variable is used to enable the test framework.
if(BUILD_TEST)
enable_testing()
# Create target for testing
add_executable(${PROJECT_NAME}-test)
# Add a test to the project to be run by `ctest()`
add_test(NAME ${PROJECT_NAME}-test COMMAND ${PROJECT_NAME}-test)
# External dependencies
find_package(GTest CONFIG REQUIRED)
# Link the static libraries to this target
target_link_libraries(
${PROJECT_NAME}-test PRIVATE GTest::gtest GTest::gtest_main GTest::gmock
GTest::gmock_main ${PROJECT_NAME})
# Specify the required C++ standard for this target
target_compile_features(${PROJECT_NAME}-test PUBLIC cxx_std_20)
endif()
CMakePresets.json
Define the test framework variables in CMakePresets.json file. The testing preset can inherit other presets, such as default, coverage, and sanitizer.
Ninja is a build system designed for speed and simplicity. It's particularly efficient for large projects with numerous source files.
Layout and Structure
Project Layout
Ninja requires a build.ninja file in the root directory of the project. This file contains the build rules, dependencies, and build targets. The build.ninja file can also include other build.ninja files in subdirectories with source files to build.
A Makefile is a build automation tool used to compile and build projects.
Layout and Structure
Project Layout
Makefile requires a Makefile file in the root directory of the project. This file contains the build rules, dependencies, and build targets. The Makefile file can also include other Makefile files in subdirectories with source files to build.
Gradle is a build system commonly used in Java and Android development. It offers a flexible and powerful build automation framework. Gradle uses a Groovy-based domain-specific language (DSL) to define the build scripts. It supports dependency management, task parallelization, and incremental builds.
Layout and Structure
Project Layout
Gradle requires a build.gradle file in the root directory of the project. This file contains the project configuration, dependencies, and build tasks. The build.gradle file can also include source code in the src/ directory with the main and test source files.
build.gradle: The Gradle build script file that defines the project configuration, dependencies, and tasks.
src/: The directory that contains the source code for the project.
main/: The directory for main source code.
java/: The directory for Java source files.
com/: The package structure for the project.
example/: The package for the project's classes.
MyApplication.java: The main Java class file for the project.
test/: The directory for test source code.
java/: The directory for Java test source files.
com/: The package structure for the test code.
example/: The package for the project's test classes.
MyApplicationTest.java: The Java test class file for the project.
1.5. Maven
Maven is a build automation and dependency management tool primarily used for Java projects. It emphasizes convention over configuration and follows the Project Object Model (POM) approach. Maven manages project dependencies, builds the project using predefined lifecycle phases, and provides plugins for various tasks.
Layout and Structure
Project Layout
Maven requires a pom.xml file in the root directory of the project. This file contains the project configuration, dependencies, and build settings. The pom.xml file can also include source code in the src/ directory with the main and test source files.
src/: The directory that contains the source code for the project.
main/: The directory for main source code.
java/: The directory for Java source files.
com/: The package structure for the project.
example/: The package for the project's classes.
MyApp.java: The main Java class file for the project.
test/: The directory for test source code.
java/: The directory for Java test source files.
com/: The package structure for the test code.
example/: The package for the project's test classes.
MyAppTest.java: The Java test class file for the project.
pom.xml: The Project Object Model (POM) file that contains project configuration and dependencies.
1.6. Ant
Ant (Another Neat Tool) is a Java-based build system that uses XML-based build scripts. It provides a flexible and customizable approach to building, testing, and deploying applications.
Layout and Structure
Project Layout
Ant requires a build.xml file in the root directory of the project. This file contains the project configuration and build targets. The build.xml file can also include source code in the src/ directory with the main source files.
build.xml: The Ant build script file that contains the project configuration and build targets.
src/: The directory that contains the source code for the project.
com/: The package structure for the project.
example/: The package for the project's classes.
MyApp.java: The Java source file for the project.
1.7. Bazel
Bazel is an open-source build system developed by Google. It focuses on scalability and supports large-scale projects with multiple programming languages. Bazel uses a declarative language to define build targets and dependencies. It provides caching, parallel execution, and incremental builds.
Bazel allows to customize the build configuration based on the project's specific requirements. It allows to define additional build targets, specify compiler flags, include additional libraries. Bazel's flexibility and extensibility provide the ability to create complex build configurations for large-scale projects.
Layout and Structure
Project Layout
Bazel requires a WORKSPACE file and BUILD files in the project directory structure. The WORKSPACE file defines the project and external dependencies, while the BUILD files specify build targets and dependencies.
In the example, we define a Bazel workspace named "MyProject" and declare an external dependency on Google Test (googletest). The http_archive rule is used to download and configure the external dependency.
BUILD
The project-level Bazel build file that specifies build targets and dependencies.
In the project-level BUILD file, we define a C++ binary target named "myapp" using the cc_binary rule. The srcs attribute specifies the source files, and the deps attribute declares the dependency on the Google Test framework.
src/BUILD
The package-level Bazel build file that specifies build targets and dependencies for the src/ directory.
Build Systems
A build system is a set of tools and processes designed to automate the compilation and assembly of source code into executable programs or libraries.
1. Category
1.1. CMake
Layout and Structure
Project Layout
Commands and Operations
List Presets
Configure Presets
Build Presets
Execute Tests
Files and Folders
CMakeLists.txt
src/CMakeLists.txt
CMakePresets.json
1.1.1. Dependency Manager
Dependency manager is a tool that automates the process of installing, upgrading, configuring, and removing dependencies.
Tools and Frameworks
vcpkg
conan
Files and Folders
CMakePresets.json
1.1.2. Compiler Cache
Compiler cache is a tool that speeds up recompilation by caching previous compilations and detecting when the same compilation is being done again.
Tools and Frameworks
ccache
sccache
Files and Folders
CMakePresets.json
1.1.3. Sanitizer
Sanitizers are tools that help find memory errors, undefined behavior, and other problems in C and C++ source code.
Tools and Frameworks
AddressSanitizer (ASan)
ThreadSanitizer (TSan)
UndefinedBehaviorSanitizer (UBSan)
MemorySanitizer (MSan)
Files and Folders
CMakePresets.json
1.1.4. Coverage
Coverage is a measure used to describe the degree to which the source code of a program is executed when a particular test suite runs.
Tools and Frameworks
gcov
lcov
gcovr
Commands and Operations
Generate Coverage Report
Files and Folders
gcovr.cfg
CMakePresets.json
1.1.5. Test Frameworks
Test frameworks are used to write and run unit tests.
Tools and Frameworks
Google Test
CUnit
Files and Folders
CMakeLists.txt
CMakePresets.json
1.2. Ninja
Ninja is a build system designed for speed and simplicity. It's particularly efficient for large projects with numerous source files.
Layout and Structure
Project Layout
Commands and Operations
Build Project
Parallel Builds
Clean Build Artifacts
Files and Folders
build.ninja
1.3. Makefile
A Makefile is a build automation tool used to compile and build projects.
Layout and Structure
Project Layout
Commands and Operations
Build Project
Clean Build Artifacts
Files and Folders
Makefile
1.4. Gradle
Gradle is a build system commonly used in Java and Android development. It offers a flexible and powerful build automation framework. Gradle uses a Groovy-based domain-specific language (DSL) to define the build scripts. It supports dependency management, task parallelization, and incremental builds.
Layout and Structure
Project Layout
Files and Folders
build.gradle
build.gradle
: The Gradle build script file that defines the project configuration, dependencies, and tasks.src/
: The directory that contains the source code for the project.main/
: The directory for main source code.java/
: The directory for Java source files.com/
: The package structure for the project.example/
: The package for the project's classes.MyApplication.java
: The main Java class file for the project.test/
: The directory for test source code.java/
: The directory for Java test source files.com/
: The package structure for the test code.example/
: The package for the project's test classes.MyApplicationTest.java
: The Java test class file for the project.1.5. Maven
Maven is a build automation and dependency management tool primarily used for Java projects. It emphasizes convention over configuration and follows the Project Object Model (POM) approach. Maven manages project dependencies, builds the project using predefined lifecycle phases, and provides plugins for various tasks.
Layout and Structure
Project Layout
Files and Folders
pom.xml
src/
: The directory that contains the source code for the project.main/
: The directory for main source code.java/
: The directory for Java source files.com/
: The package structure for the project.example/
: The package for the project's classes.MyApp.java
: The main Java class file for the project.test/
: The directory for test source code.java/
: The directory for Java test source files.com/
: The package structure for the test code.example/
: The package for the project's test classes.MyAppTest.java
: The Java test class file for the project.pom.xml
: The Project Object Model (POM) file that contains project configuration and dependencies.1.6. Ant
Ant (Another Neat Tool) is a Java-based build system that uses XML-based build scripts. It provides a flexible and customizable approach to building, testing, and deploying applications.
Layout and Structure
Project Layout
Files and Folders
build.xml
build.xml
: The Ant build script file that contains the project configuration and build targets.src/
: The directory that contains the source code for the project.com/
: The package structure for the project.example/
: The package for the project's classes.MyApp.java
: The Java source file for the project.1.7. Bazel
Bazel is an open-source build system developed by Google. It focuses on scalability and supports large-scale projects with multiple programming languages. Bazel uses a declarative language to define build targets and dependencies. It provides caching, parallel execution, and incremental builds.
Bazel allows to customize the build configuration based on the project's specific requirements. It allows to define additional build targets, specify compiler flags, include additional libraries. Bazel's flexibility and extensibility provide the ability to create complex build configurations for large-scale projects.
Layout and Structure
Project Layout
Files and Folders
WORKSPACE
In the example, we define a Bazel workspace named
"MyProject"
and declare an external dependency on Google Test (googletest). Thehttp_archive
rule is used to download and configure the external dependency.BUILD
In the project-level BUILD file, we define a C++ binary target named "myapp" using the
cc_binary
rule. Thesrcs
attribute specifies the source files, and thedeps
attribute declares the dependency on the Google Test framework.src/BUILD