How to solve mex function error caused by libstdc++ library version mismatch ?

27 views (last 30 days)
Hello,
When compiling a mex function, and running it right after, I get an error saying :
Invalid C++ MEX-file 'myfun':
/usr/local/MATLAB/R2023b/interprocess/bin/glnxa64/mex/../../../../sys/os/glnxa64/libstdc++.so.6:
version `GLIBCXX_3.4.29' not found (required by ...
I saw on this matlab post that the issue is that the libstdc++ version used during compilation is not the one that matlab has on its path during execution. And indeed, the version in "/lib/x86_64-linux-gnu" (i use gcc that links to this lib stdc++ ) and "usr/local/MATLAB/R2023b/sys/os/glnxa64" are not the same.
As explained on the post, launching matlab using LD_PRELOAD to force the use of the right libstdc++ works:
LD_PRELOAD=/lib/x86_64-linux-gnu/libstdc++.so.6 matlab
But it does not seem very clean. Is there a way to indicate to matlab that it should alwlays at startup load the the version of libstd in /lib/x86_64-linux-gnu ? On the matlab post somebody says that I could simply delete the matlab libstd version, but I don't know enough about matlab to feel confortable deleting some libraires.
Matt
PS : how I compile the mex function
mex (header_path,lib_path,'-lsome_lib', ...
'-lgomp',"CXXFLAGS=$CXXFLAGS '-fopenmp'",...
'COPTIMFLAGS=-O2',...
"CXXFLAGS=$CXXFLAGS '-Wall'",...
"/path/myfun.cpp")

Accepted Answer

Poorna
Poorna on 29 May 2024
Hi Matt,
I see you are facing errors while trying to compile using the "mex" function. I see you also have found a solution to the problem which is to set the environment variable LD_PRELOAD to the right libstdc++. More information on this can be found in the below documentation: https://www.mathworks.com/help/compiler/mcr-path-settings-for-run-time-deployment.html
The above documentation also provides you a way to set the env variable permanently by appending the command to the ~./bashprofile. This will set the env variable to all applications which might not be desirable.
You could on the other hand try leveraging the startup.m file to set the env variable. This will automate the process so that you need not use the entire "LD_PRELOAD=/lib/x86_64-linux-gnu/libstdc++.so.6 matlab" command. "setenv" function can be used to set the env variable.
Refer to the following documentation to know about "startup.m" file and "setenv" function:
You can find an example usage of the "startup" file to set env variable in the below MATLAB Answer:
Hope this Helps!

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!