Bug in MATLAB mex file generation from generic C++ code

4 views (last 30 days)
When trying to convert general C++ code into a MATLAB mex file (via the coder GUI or via a script calling codegen), there seems to be a bug in the MATLAB linker - at least when using the MinGW64 Compiler.
For example, running the following script
cfg = coder.config('mex'); % Set target to mex file
cfg.TargetLang = 'C++'; % Let MATLAB know the source file is C++
cfg.CustomSourceCode = '#include "main.h"'; % Include header
cfg.GenerateReport = true; % Show report
%%Invoke MATLAB Coder.
codegen -config cfg -I C:\mingw-w64\mingw64\include MATLAB_Cpp
where the MATLAB_Cpp.m file is
function outVal = MATLAB_Cpp
outPut = 0;
coder.updateBuildInfo('addSourceFiles', 'helloworld.cpp');
outVal = coder.ceval('main');
end
the helloworld.cpp file is
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!";
return 0;
}
and the header is just a simple function prototype for main (which seems to be necessary)
int main();
will generate an error.
Checking the build log in the Code Generation Report shows:
g++ -c -fexceptions -fno-omit-frame-pointer -DMATLAB_MEX_FILE -DMATLAB_MEX_FILE -O -DNDEBUG -I "." "C:\MATLAB/helloworld.cpp"
... (7 lines omitted here, all calling g++) ...
gcc -Wl,--version-script,MATLAB_Cpp_mex_mex.map helloworld.o MATLAB_Cpp_data.o MATLAB_Cpp_initialize.o MATLAB_Cpp_terminate.o MATLAB_Cpp.o _coder_MATLAB_Cpp_info.o _coder_MATLAB_Cpp_api.o _coder_MATLAB_Cpp_mex.o -m64 -Wl,--no-undefined -shared -L"C:\Program Files\MATLAB\R2016a\extern\lib\win64\mingw64" -llibmx -llibmex -llibmat -lm -llibmwlapack -llibmwblas -o MATLAB_Cpp_mex.mexw64 -llibemlrt -llibcovrt -llibut -llibmwmathutil
helloworld.o:helloworld.cpp:(.text+0x10): undefined reference to `std::ios_base::Init::~Init()'
helloworld.o:helloworld.cpp:(.text+0x37): undefined reference to `std::basic_ostream >& std::operator<< >(std::basic_ostream >&, char const*)'
helloworld.o:helloworld.cpp:(.text+0x56): undefined reference to `std::ios_base::Init::Init()'
helloworld.o:helloworld.cpp:(.rdata$.refptr._ZSt4cout[.refptr._ZSt4cout]+0x0): undefined reference to `std::cout'
collect2.exe: error: ld returned 1 exit status
gmake: *** [MATLAB_Cpp_mex.mexw64] Error 1
Coder correctly calls g++ for the compilation, as seen in the first line above.
The problem here seems to be that gcc is used as the linker, when it should be g++ so the C++ libraries can be included (the error goes away if iostream is not present).
It seems, even when the source file is C++ (.cpp), MATLAB Coder still chooses the gcc linker.
This can be fixed by editing the 'mex_C_win64.xml' file in C:\Users\{UserName}\AppData\Roaming\MathWorks\MATLAB\R2016a, and adding "-lstdc++" to the LDFLAGS variable (this will include the C++ libraries).
While this works, a nicer solution would be for MATLAB to use the correct linker (g++), as set in the 'mex_C++_win64.xml' file, but this seems to be ignored.
I assume this to be a MATLAB bug...
Any ideas?

Answers (1)

Anakin Zheng
Anakin Zheng on 27 Feb 2018
This is a bug in MATLAB Coder and will be fixed in the 18a release.
To workaround this issue in current release you can modify the following file: MATLABROOT\toolbox\coder\coder\mex\c\mex_mingw.mk
Change LINK_FLAGS += >ADDITIONAL_LDFLAGS< to LINK_FLAGS += -lstdc++ >ADDITIONAL_LDFLAGS<

Community Treasure Hunt

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

Start Hunting!