libstdc++.so.6: version `GLIBCXX_3.4.19' not found
Running libsdca_solve in Matlab on Linux produces the following error message:
Invalid MEX-file '/path/to/libsdca_solve.mexa64':
/usr/lib/matlab-8.5/bin/glnxa64/../../sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.19' not found
(required by /path/to/libsdca_solve.mexa64)
Can reproduce using e.g. the following Matlab / Compiler combinations:
- R2015a (8.5.0.197613) 64-bit (glnxa64) / c++ (Debian 4.9.2-10) 4.9.2
- R2015b (8.6.0.267246) 64-bit (glnxa64) / c++ (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9)
Problem
The problem may appear when C++ code is compiled using a more recent version of gcc (4.8, 4.9 in this case) than the one supported by Matlab (4.7 in this case).
To check which gcc version is currently supported, see http://www.mathworks.com/support/compilers/current_release
The problem is relatively well-known (241634, 255846) and seems to also apply to other popular libraries, such as Caffe (Issue #827), MatConvNet (Issue #14), and SPAMS (22564357).
Details
Matlab ships with its own version of libstdc++ (/path/to/matlab/sys/os/glnxa64/libstdc++.so.6), which is loaded when Matlab starts and may be different from the system-wide libstdc++. In particular, the Matlab's libstdc++ may not contain more recent symbols (`GLIBCXX_3.4.19') required by the C++ code that was compiled with the system's libstdc++.
To check which symbols specifically the code depends on, see http://stackoverflow.com/a/423313.
Solutions that do NOT work
Solutions that are easy to think of as well as the ones suggested here did not work for me.
- Compile with
mexinstead ofgcc- Using the mex tool or the mex command from Matlab does not solve this problem on its own since it just uses the configured compiler (
mex -setup) under the hood. - It may work if mex is configured to use a different version of gcc, in which case that same version of gcc can be used directly.
- Using the mex tool or the mex command from Matlab does not solve this problem on its own since it just uses the configured compiler (
- Set
LD_LIBRARY_PATHbefore starting Matlab.- This does not work because Matlab overwrites
LD_LIBRARY_PATHon startup. - Run
matlab -nto check the settings that are used.
- This does not work because Matlab overwrites
- Edit the
.matlab7rc.shscript and setLDPATH_PREFIX.- It is indeed possible to change the
LD_LIBRARY_PATHthat is used by Matlab this way (the correct value is reported by both thematlab -nand thegetenv('LD_LIBRARY_PATH')from Matlab). - However, Matlab still loads its own version of libstdc++ (
matlab -nconfirms this). Not sure what is going on there.
- It is indeed possible to change the
- Run
setenv('LD_LIBRARY_PATH', '');- This does not work because the wrong version of libstdc++ seems to be already loaded (see above).
- Use
rpath(-Wl,-rpath)- Again, this does not work for the same reason as above (libstdc++ is already loaded).
- Link libstdc++ statically (
-static-libstdc++)- This does not work because mex files are shared libraries while libstdc++.a does not have PIC code by default, therefore, compilation fails. See http://stackoverflow.com/a/19997068, http://stackoverflow.com/a/4036497, and http://stackoverflow.com/a/23258102 for further details.
Solutions that DO work
- Use an older version of
gcc- Installing and switching to the supported version of
gccshould fix the problem.
- Installing and switching to the supported version of
- Use
LD_PRELOADwhen starting Matlab.- This seems to be the most widely used solution that requires minimum effort.
- Run
locate libstdc++.so.6to find the library on your system. - Use
LD_PRELOAD=/located-path/libstdc++.so.6 matlabto run Matlab.
- Remove or substitute the libstdc++ at
/path/to/matlab/sys/os/glnxa64- This should work since the Matlab's libstdc++ file is physically replaced with another version (e.g. via a symlink), and so the old version is gone.
- Change the code so that it does not use the more recent symbols (`GLIBCXX_3.4.19' in this case)
- This particular issue was fixed by switching from the new
std::chrono::high_resolution_clockto the old Cgettimeofdayfunction.
- This particular issue was fixed by switching from the new
Further comments
- Is it safe to use a newer version of libstdc++.so.6 (e.g. via
LD_PRELOAD)?- The library is designed to be backward compatible, therefore it should work in most of the cases (http://stackoverflow.com/a/14082540, http://stackoverflow.com/a/2325933).
I have the same problem in Matlab R2016a and I get this error message
=========SL_ZED_WITH_MATLAB=========
Invalid MEX-file '/home/ahmad/work/zed-matlab/matlab/mexZED.mexa64':
/usr/local/MATLAB/R2016a/bin/glnxa64/../../sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.19' not found (required by
/usr/local/zed/lib/libsl_tracking.so)
Error in ZED_Camera (line 11)
mexZED('create', 720, 60);
I tried LD_PRELOAD and my error changed to this one
=========SL_ZED_WITH_MATLAB=========
Invalid MEX-file '/home/ahmad/work/zed-matlab/matlab/mexZED.mexa64': dlopen: cannot load any more object with static TLS
*** MATLAB has encountered a Linux operating system bug; this bug will eventually cause MATLAB to crash. Do not repeat the
command which produced this error, as doing so might cause an immediate crash. Save your work and restart MATLAB now.
I think LD_PRELOAD or substituting Matlab's library is not a solution for Matlab R2016a.
That is a different problem, see http://www.mathworks.com/matlabcentral/answers/125117-openmp-mex-files-static-tls-problem and http://stackoverflow.com/questions/19268293/matlab-error-cannot-open-with-static-tls
The issue is that Matlab runs out of slots for shared libraries loaded at runtime. Try using static linkage instead (requires PIC code), or add the necessary libraries to LD_PRELOAD, or try other workarounds suggested in the links above.
Hi, I have Matlab 2017b. I have tried both LD_PRELOAD=/located-path/libstdc++.so.6 matlab, as well as replacing the libstdc++.so.6 under matlab install path with the linux standard. But when I do this, instead of getting the GLIBCXX_3.4.19 not found error, Matlab crashed when I'm building my s-function immediately. Have you encountered something similar? By the way, similar crash happens when you delete the libstdc++.so.6 under matlab install path, so maybe Matlab doesnt like overriding/replacing libstdc++.so.6 ?