alvas alvas - 5 months ago 20
Linux Question

How to get the boost_major_version number automatically?

Previously on Ubuntu, we can do the following to get the

boost_major_version
:

echo "$boost_cv_lib_version" | sed 's/_//;s/_.*//'


For whatever reason, Ubuntu changes when upgrading from version 14.04 to 16.04 and now I have to do the following to find my Boost version:

dpkg -s libboost-dev | grep 'Version'


And the current version of boost installed using
sudo apt install libboost-all-dev
is:

Version: 1.58.0.1ubuntu1


Given this, I would require the major version, i.e.
158
.

Other than manually looking at it knowing it's 158? I've tried some regexes but I couldn't get it correct to strip the string:
Version: 1.58.0.1ubuntu1
to
158
.

I've tried:

alvas@ubi:~/repp$ dpkg -s libboost-dev | grep 'Version'
Version: 1.58.0.1ubuntu1
alvas@ubi:~/repp$ dpkg -s libboost-dev | grep 'Version' | cut -d' ' -f2 | grep -oP '([0-9].*\.[0-9].*)\.'
1.58.0.


But I also understand that the
-P
option might not be available on linux platforms.

I could do this by piping into Python to do the dirty string works but that's a little too much... I'm sure there's a better way:

alvas@ubi:~/repp$ dpkg -s libboost-dev | grep 'Version' | python -c "import re,sys; print re.findall(r'([0-9].*\.[0-9].*)\.[0-9].*\.', sys.stdin.readline())[0].replace('.', '')"
158


So the question is:


  • How do I get the
    boost_major_version
    on Ubuntu 16.04?

  • If
    dpkg -s libboost-dev | grep 'Version'
    is the only way to do it, how do I strip
    Version: 1.58.0.1ubuntu1
    ->
    158
    ?


Answer

You can find the version info by querying the dpkg database with dpkg-query directly:

dpkg-query -W --showformat='${Version}\n' libboost-dev

--showformat indicates we just want the value of the Version parameter, so you will get just the version number in the output.

Now, to answer your second question, AFAIK dpkg does not differentiate between the major and minor (and micro) version numbers, you need to parse that yourself. Here is a sed way for this case:

dpkg-query -W --showformat='${Version}\n' libboost-dev | sed -E 's/^([^.]+)\.([^.]+)\..*/\1\2/'