How to manually select the libstdc++ library to use to resolve a "version 'GLIBCXX_#.#.##' not found" error?

654 views (last 30 days)
I have generated a MEX file that links against a C++ compiled library. This MEX file was working correctly when running Ubuntu 20.04 and when I upgraded to Ubuntu 22.04, the same MEX file still worked. After making a change to the C wrapper code, I successfully rebuilt the MEX file with MATLAB R2022b. When trying to invoke the rebuilt MEX file, it failed due to the wrong GLIBCXX version with the following error:
Invalid MEX-file '.../decodeBitMatrix_mex.mexa64': /usr/local/MATLAB/R2022b/bin/glnxa64/../../sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.29' not found (required by .../decodeBitMatrix_mex.mexa64)
I see that the libstdc++ on my Ubuntu 22.04 system has two newer versions than what is shipped with MATLAB, as shown below:
$ strings /usr/local/MATLAB/R2022b/bin/glnxa64/../../sys/os/glnxa64/libstdc++.so.6 | grep GLIBCXX_3.4 | tail -4 GLIBCXX_3.4.25 GLIBCXX_3.4.26 GLIBCXX_3.4.27 GLIBCXX_3.4.28 $ strings /lib/x86_64-linux-gnu/libstdc++.so.6 | grep GLIBCXX_3.4. | tail -4 GLIBCXX_3.4.27 GLIBCXX_3.4.28 GLIBCXX_3.4.29 GLIBCXX_3.4.30
How can I setup MATLAB or my system to use the same version of libstdc++?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 30 Sep 2024
Edited: MathWorks Support Team on 3 Oct 2024
The errors are caused by a mismatch between the libstdc++ library shipped with Ubuntu 22.04 and MATLAB R2022b. Refer to the following link for the supported and compatible compilers for current and previous versions:
As a workaround, you can direct MATLAB to use the system's libstdc++ library rather than the MATLAB shipped version. This can be achieved by setting the following environment variable prior to starting MATLAB:
LD_PRELOAD=/lib/x86_64-linux-gnu/libstdc++.so.6 matlab
You can then force load the system libstdc++ with the expected GLIBCXX version. Note that the above command only sets the environment variable for MATLAB. To set it for other processes of the current shell, use the following commands instead:
export LD_PRELOAD=/lib/x86_64-linux-gnu/libstdc++.so.6 matlab
If the above solution does not help and you get an error similar to the following
.MathWorks/ServiceHost/-mw_shared_installs/v2024.9.0.2/bin/glnxa64/MathWorksServiceHost: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.30' not found (required by ........)
This error basically means that the 'libstdc++' library version shipped with the MathWorks ServiceHost has higher version than the system 'libstdc++' library. Please use one of the following mentioned steps to resolve the issue:
1. Upgrade the system 'libstdc++' library version to a newer version and set 'LD_PRELOAD' to point to this library as mentioned above.
2. Change the 'LD_PRELOAD' path to point to the MathWorks ServiceHost library as follows:
LD_PRELOAD=~/.MathWorks/ServiceHost/-mw_shared_installs/VERSION/bin/glnxa64/../../sys/os/glnxa64/libstdc++.so.6 matlab
And, to set it for other processes of the current shell, use the following commands:
export LD_PRELOAD=~/.MathWorks/ServiceHost/-mw_shared_installs/VERSION/bin/glnxa64/../../sys/os/glnxa64/libstdc++.so.6 matlab
Where 'VERSION' is the latest version of MathWorks ServiceHost matching your current MATLAB Version. For e.g, if you are using MATLAB R2024a, your ServiceHost version would like 'v2024.9.0.2'. 

More Answers (1)

Cris Luengo
Cris Luengo on 7 Nov 2023
Edited: Cris Luengo on 7 Nov 2023
The official solution to this (notwithstanding a MathWorks staff member suggesting LD_PRELOAD), is to install a version of GCC that is supported by your version of MATLAB. You can find them listed here for the latest MATLAB release, there's a button on the top-right to select an older version:
You have a newer version of GCC, hence a MEX-file compiled with it requires a newer version of the GCC libraries than the one that is loaded with MATLAB. Using the right GCC version ensures your MEX-file requires the same GCC libraries that come with MATLAB.
LD_PRELOAD is a workaround that forces MATLAB to run with the newer library, ignoring the one that it is distributed with. A simpler and more effective way of accomplishing the same thing is to simply delete the GCC libraries that come with MATLAB. They are in <matlabroot>/sys/os/glnxa64, and the files to delete are libstdc++.*, and possibly also the files libg2c.* and libgcc_s*.

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!