16

I'm looking for a cross-platform C++ lighweight configuration library with non restrictive licence. I need something more complex than standard properties file with sections, but I don't want to use XML (too much writing :-)).

I would like to write configuration this way:

render = 
{
    window = 
    {
        width = 800,
        height = 600
    }
}
runnydead
  • 618
  • 1
  • 7
  • 17
  • 2
    I have found [libconfig](http://www.hyperrealm.com/libconfig/). I looks good, but it uses exceptions for error reporting... – runnydead Feb 12 '12 at 11:27
  • 2
    I also think that libconfig is pretty nice. There are a few issues that I have with the C++ API and there doesn't appear to be an implementation for Java (which means only C/C++ applications can read the config files), but for now it's still my go-to config library. – m-renaud Feb 09 '14 at 01:42
  • 1
    [libconfig](http://www.hyperrealm.com/libconfig/) appears to be dead. The manual pages and download link all go to an unconfigured WordPress site. – jwm Oct 11 '18 at 17:29
  • 1
    although Google turned up https://github.com/hyperrealm/libconfig which is still active – jwm Oct 11 '18 at 17:31

2 Answers2

15

There's boost's property_tree. The license allows commercial use.

Your example:

ptree pt;
pt.put("render.window.width", 800);
pt.put("render.window.height", 600);

This can e.g. be exported to JSON

write_json("my_config.json", pt);

which will then look like

{
  "render":
  {
    "window":
    {
      "width": 800;
      "height": 600;
    }
  }
}

The same way you can export to XML, INI and INFO.

Karl von Moor
  • 8,484
  • 4
  • 40
  • 52
  • 3
    In my experience .. Boost is far from lightweight library, but I will look at it, because it looks promising – runnydead Feb 12 '12 at 11:24
  • 6
    @hubrobin: You don't need the entire Boost library to make this work, IIRC. Boost has a tool called [bcp](http://www.boost.org/doc/libs/release/tools/bcp/doc/html/index.html) designed specifically to extract individual libraries. – In silico Feb 12 '12 at 13:18
5

You can also try JsonCpp and write your configuration files in Json, which has a very similar syntax to the one you like:

// Configuration options
{
    // Default encoding for text
    "encoding" : "UTF-8",

    // Plug-ins loaded at start-up
    "plug-ins" : [
        "python",
        "c++",
        "ruby"
        ],

    // Tab indent size
    "indent" : { "length" : 3, "use_space": true }
}

Is under the MIT License so it's very permissive.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
Daniele Santi
  • 771
  • 3
  • 24
  • 31
  • 6
    Since this answer was written, [JSON for Modern C++](https://github.com/nlohmann/json) has become a very popular JSON library for C++ and is worth a look as well. – Rotsiser Mho Mar 12 '19 at 20:04