Search code examples
c++google-colaboratory

How to upgrade libstdc++.so on Colab runtime?


I would like to run on Colab a C++ library which requires a version of libstdc++.so which is newer than the one provided by the default g++ 7.x installed on Colab. Such requirement is due to C++17 features not supported by g++ 7.x.

In order to do so, I install a recent g++ compiler (and corresponding libstdc++.so) from a PPA. However, when I try to import libraries compiled with the updated g++ compiler it seems that the python runtime is not aware of the update to libstdc++.so, unless one restarts the runtime via "Runtime -> Restart runtime".

Since restarting the runtime is an ugly workaround, and might be disruptive for my end users, is there a better way to make the current runtime aware that libstdc++.so has changed?

I have prepared a standalone notebook which shows my problem on a simple case (using pybind11).

Thanks


Solution

  • To install and use recent libstdc++ do:

    1. sudo add-apt-repository ppa:ubuntu-toolchain-r/test
    2. sudo apt install libstdc++-9-dev.
    3. Installed include files are inside /usr/include/c++/9/ and /usr/include/x86_64-linux-gnu/c++/9/.
    4. Installed binary library files (.a/.so) are in /usr/lib/gcc/x86_64-linux-gnu/9/.
    5. You can list installed files of package through dpkg -L libstdc++-9-dev.

    Now you can run your program like:

    LD_LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/9/ your_program_name
    

    So that it gets correct path and correct .so file.

    Issue with your Notebook was solved for me by adding to setup.py following options, after that your Notebook doesn't print any error:

    extra_compile_args=["-std=c++11", '-static', '-static-libgcc', '-static-libstdc++'],
    extra_link_args=['-static-libgcc', '-static-libstdc++'],
    

    Also you can collect all necessary .so files into folder of your Python's module and package all into wheel .whl file. There is no need to APT-install all .so dependencies on every machine. I think for Python is more clean solution to ship all necessary .so files inside .whl.