C++ Application Configuration: Libraries and Tools for Flexible Setup

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

C++ Application Configuration: Overview of Tools and Libraries



Modern C++ applications rarely operate without external settings. Configuration files allow you to change program behavior without recompilation: from resource paths and database connection parameters to algorithm selection and enabling/disabling features. In this article, we will look at four popular tools for working with configuration in C++: Boost.Program_options, libconfig, nlohmann/json, and yaml-cpp.



1. Boost.Program_options — Universal Command Line and Config Parser



Boost.Program_options is part of the Boost library. It allows you to uniformly handle command-line arguments, environment variables, and configuration files. It supports several formats (INI, simple text), but not JSON/YAML.



Key Features:

  • Automatic help generation (--help);
  • Type validation (int, double, string, vector);
  • Hierarchical options (groups);
  • Reading from multiple sources with priority.


Usage Example (reading from an INI file):

#include <boost/program_options.hpp>#include <iostream>#include <fstream>

namespace po = boost::program_options;

int main(int argc, char* argv[]) { po::options_description config("Configuration"); config.add_options() ("database.host", po::value<std::string>()->default_value("localhost"), "DB Host") ("database.port", po::value<int>()->default_value(3306), "DB Port") ("logging.level", po::value<std::string>()->default_value("info"), "Log Level");

po::variables_map vm; std::ifstream ifs("app.ini"); if (ifs) { po::store(po::parse_config_file(ifs, config), vm); po::notify(vm); }

std::cout << "Host: " << vm["database.host"].as<std::string>() << ""; std::cout << "Port: " << vm["database.port"].as<int>() << ""; return 0;}


2. libconfig — Lightweight Library with Its Own Format



libconfig is a C library (with a C++ wrapper) with its own compact configuration format. It supports hierarchy, arrays, groups, and comments. It is known for high parsing speed.



File Format (example app.cfg):

database = {    host = "localhost";    port = 3306;    users = [ "admin", "guest" ];};logging = {    level = "info";    file = "/var/log/app.log";};


Reading Example in C++:

#include <libconfig.h++>#include <iostream>

int main() { libconfig::Config cfg; try { cfg.readFile("app.cfg"); std::string host = cfg.lookup("database.host"); int port = cfg.lookup("database.port"); std::cout << "Connecting to " << host << ":" << port << ""; } catch (const libconfig::FileIOException &e) { std::cerr << "File read error"; } catch (const libconfig::ParseException &e) { std::cerr << "Parse error: " << e.what() << ""; } return 0;}


3. nlohmann/json — Modern JSON Parser



The nlohmann/json library (JSON for Modern C++) is one of the most popular. It works with the JSON format, which is widely used in web applications and microservices. It features intuitive syntax (operator [], iterators).



Example of Reading a JSON Config:

#include <nlohmann/json.hpp>#include <fstream>#include <iostream>

using json = nlohmann::json;

int main() { std::ifstream f("config.json"); json data = json::parse(f);

std::strin

Recommendations