Goal of the Config C++ is to provide a library like node-config for C++ community.
Config C++ organizes hierarchical configurations for your app deployments.
It lets you define a set of default parameters, and extend them for different deployment environments (development, testing, staging, production, etc.).
Configurations are stored in configuration files within your application, and can be overridden and extended by environment variables.
The following examples are in JSON format (YAML in progress).
- Add config to git submodules (execute in project root):
$ mkdir externals && cd externals
$ git submodule add https://github.com/cieslarmichal/config-cxx.git
$ git submodule update --init --recursive
- Link with library:
set(BUILD_CONFIG_CXX_TESTS OFF)
add_subdirectory(externals/config-cxx)
add_executable(main Main.cpp)
target_link_libraries(main config-cxx)
$ mkdir config
$ vi config/default.json
{
"db": {
"name": "users",
"host": "localhost",
"port": 3306,
"user": "default",
"password": "default"
}
}
$ vi config/production.json
{
"db": {
"host": "postgres",
"user": "admin",
"password": "secretpassword"
}
}
#include <iostream>
#include "config-cxx/Config.h"
config::Config config;
auto dbName = config.get<std::string>("db.name"); // "users"
auto dbHost = config.get<std::string>("db.host"); // "postgres"
auto dbPort = config.get<int>("db.port"); // 3306
auto dbUser = config.get<int>("db.user"); // "admin"
auto dbPassword = config.get<int>("db.password"); // "secretpassword"
auto authEnabled = config.get<bool>("auth.enabled"); // true
config.get()
will throw an exception for undefined keys to help catch typos and missing values.
Use config.has()
to test if a configuration value is defined.
$ export CXX_ENV=production
Running in this configuration, the port
and name
elements of db
will come from the default.json
file, and the host
, user
and password
elements will
come from the production.json
override file.
Check out example project in examples directory.
- MSVC➚ version 143 or newer.
- GCC➚ version 13 or newer.
- Clang➚ version 16 or newer.
- Apple Clang➚ version 16 or newer.
- GTest (
BUILD_CONFIG_CXX_TESTS=OFF
CMake flag to disable) - nlohmann/json
Feel free to join Config C++ development! 🚀
Please check CONTRIBUTING guide.
Discord Channel for contributors.