Giter Club home page Giter Club logo

cpp-project-template's Introduction

C++ project template

Template Initialization

This project is a template that cannot be used before initialization. To initialize your project, you must run the following shell command:

cmake -P init.cmake --project <name> --module <name> --header <name>
  • Project name will become your top-level CMake project name
    • Must be alphanumeric and begin with a character.
  • Module name will be concatenated with your project name to determine the library's namespace (${project}::${module})
  • Header name will become the name of your library's main include header

Building

To build the project locally, you will need to select a CMake preset that matches your system configuration. Your system's configuration is described by a triplet. This is inspired by rust triples.

Presets for the most common system triplets are defined in cmake/presets/ and presented via CMakePresets.json.

Notice that each system triplet defines a preset for multiple compilers. If you have a compiler preference, you can pick the respective preset. If you do not have a preference, you can choose the following reccomendation (depending on your OS):

  • Windows: msvc
  • MacOS: clang
  • Linux: gcc

Now you can configure and build your project:

cmake --workflow --preset=<PRESET>

This is equivalend to running the following in a step-by-step proceedure:

# Configure
cmake --preset=<PRESET>
# Build
cmake --build --preset=<PRESET>
# Test
ctest --preset=<PRESET>

Regardless of how you build, the build/<PRESET> folder will be populated with the binaries for all of your CMake targets.

Usage

By default, this template comes with a CLI entrypoint defined in src/cli/src/main.cpp, and one module/library defined in your src folder. The Command Line Interface contains a very basic main function, and can be run after building by the build/<PRESET>/src/cli/<Debug|Release|RelWithDebInfo>/<PROJECT_NAME>_cli executable(s).

Tests are run with Catch2. They can be written in the tests subdirectory, and run with CTest:

ctest --preset <PRESET>

For usage within another C++ project, you can add the following to your CMake configuration:

find_package(<PROJECT_NAME> CONFIG REQUIRED)
target_link_libraries(<TARGET> PUBLIC <PROJECT_NAME>::<MODULE_NAME>)

This will require that your library is published and installed via vcpkg or found locally by setting the CMAKE_PREFIX_PATH environment variable in your other project during configure.

If you wish to expose parts of your library as a WebAssembly module, you can add the extern "C" __attribute__((export_name("<my_function_name>"))) annotation to any function you wish to expose in src/wasm/interface.cpp, and compile using the wasm32-wasi-clang preset. This will generate a minimal WebAssembly binary exposing your exported functions at build/<PRESET>/src/wasm/<Debug|Release|RelWithDebInfo>/lib<PROJECT_NAME>.wasm. This binary conforms to the WASI WebAssembly standard, so it can be utilized in any WASI-supporting runtime like wasmer.io or wasmtime.

A WebAssembly version of your CLI will also be available:

wasmtime run build/<PRESET>/src/cli/<Debug|Release|RelWithDebInfo>/<PROJECT_NAME>_cli

# Prints:
# <PROJECT_NAME> version: 0.0.1

Note

Exposed WebAssembly functions currently only accept parameters and return values of numerical type. This is how core WebAssembly files work, and without additional glue-code or binding generators like wit-bindgen, complex data-types cannot be sent over the runtime's ABI boundary. Complex data must be transferred via pointer and serialized bytes in exported/shared memory.

cpp-project-template's People

Contributors

msvetkin avatar rioam2 avatar tkk2112 avatar fahrradflucht avatar nm-remarkable avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.