I am looking for a library that can handle unit conversions. I came across boost::units and unitscpp, however they only seem to cover simple units -- like length and mass. And they also do not seem to have much support in the way of decimal prefixes -- milli (m), giga (G), etc. I am looking for something more comprehensive, like the Python library Pint, that has the extensive units list, decimal prefixes and easy parsing of strings containing the value and the units to some usable object.

  • I think you might get better answers on StackOverflow since this forum is more for software recommendations than for programming library recommendations. It's a very fine line since what you are seeking is technically software! OTOH, since Gilles has already provided an answer, perhaps this is a good place too! :) Commented Dec 13, 2014 at 2:10
  • I was going to post on SO, however I feared that this topic would be flagged as off topic because it is pretty subjective, and not really about fixing a coding problem. Commented Dec 15, 2014 at 16:13
  • I'm glad you decided to post it here, because you got the answer you needed! Overly aggressive 'off-topic' flagging is one of the problems with many StackExchange sites. As you discovered, even when a question may be slightly more 'on-topic' on one site, you will often receive better answers on another site. Commented Dec 15, 2014 at 22:56

3 Answers 3


The Units program (now a GNU project) does just that, as a standalone program. It can do units conversion and simple arithmetic and understands SI prefixes (e.g. you can ask it how much 42 inches + 3 meters is in mli). The set of units and prefixes is defined via a simple text file read at runtime, which is convenient if you need to fine-tune it (e.g. to update currencies based on the current rates). It's portable C and should work just about anywhere.

You can call units as a standalone program and retrieve its output via popen and read with via Boost.Process. Pass the -t option, the original value and the target unit (omit that if you want to convert to the standard unit). This has a significant overhead; if you're going to do a lot of conversions, launch one instance of units, pass it conversion requests on its standard input, and read back the result from its standard output.

While the program doesn't come with a librar, you can tweak the code a little and link it into your program. Omit the main function, change the printf calls into sprintf, and call processunit, showdefinition and showanswer directly. That's a bit of maintenance work, but the code doesn't change often, and if you're lucky the upstream author will accept your patches to build a separate library. Do note that the code is licensed under the GNU GPL, so if you link it directly into your program, your code will have to be licensed under the GNU GPL as well (if you call units as an external program, there is no license constraint).

  • 1
    Thanks, this looks like exactly what I am looking for. It is just going to take a bit of work to work it into my existing code and add the ability to do unit comparison. Commented Dec 15, 2014 at 20:58
  • @AeroBuffalo: If you've made that into a usable library rather than just something which calls the binary, consider publishing the source and linking from here.
    – einpoklum
    Commented Dec 22, 2015 at 11:36
  • 1
    @einpoklum I will put a reminder in my calendar for this week to look at what I ended up developing and make a github repository (and post the link here) should it be something that others can use. Commented Jan 4, 2016 at 16:39

If all else fails you can always embed python and Pint into your C/C++ program and use them from within the C/C++ code. If you do this you will have access to python and it's libraries from anywhere in your code.

See https://docs.python.org/2/extending/embedding.html for details.

  • 1
    I thought about doing that, given how well Pint performs and how capable it is. However, I am not the biggest fan of Python, and we are trying to minimize the necessary number of languages used in the project. Commented Dec 15, 2014 at 21:01
  • Then you could always port pint into C/C++ there are some tools that can help with that like Cython were you may be able to automate the generation of C from the pint source so as to be able to follow bugfixes/upgrades in pint. Commented Dec 15, 2014 at 21:36
  • Oh, I never knew that was possible. I'll definitely look into that, thanks. Commented Dec 16, 2014 at 0:40

I'm just looking for the same kind of units-conversion library as you, but I also have the additional requirement that the license cannot be copyleft. After some search, I found UDUNITS, which isn't copyleft and moreover even has an API to use it as a library. I also found that the original units program wasn't copylefted before the GNU Units implementation. The original version can still be found if you search, but however, I find the UDUNITS library very interesting and I feel it's just what I'm looking for.

Also of interest: The FreeBSD implementation of the units program. Reading at the commits comments in the source repository, it seems they try to get close to the GNU units behavior, while not being copylefted. You can browse the repository (the link points to the r11 FreeBSD release).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.