MinGW clibgen build fails to find -llibMatlabDataArray

I am using Matlab2021a. I am trying to wrap an existing C++ library for use in Matlab. I did the wrapping in Linux and I am now trying to do the same thing in Windows. For my comiler, I am using MinGW 6.3.0 as installed by the using the download provided by Mathworks:
I was able to run the clibgen.generateLibraryDefinition command, but when I try to do a build, I get linker errors. To narrow down the origin of the problem, I followed a useful simple example case. The instructions are provided at the following link:
I made the libfive.h and libfive.cpp files as per those instructions. I then built a shared library using MinGW's g++.
$ g++ -c -fPIC libfive.cpp -o libfive.o
$ g++ libffive.o -shared -o libfive_win.so
From inside Matlab, I generated a library definition.
>> clibgen.generateLibraryDefinition('libfive.h', 'Libraries', 'libfive_win.so')
Using MinGW64 Compiler (C++) compiler.
Generated definition file definelibfive.mlx and data file 'libfiveData.xml' contain definitions for 1 constructs supported by MATLAB.
Build using build(definelibfive).
The inclusion of the _win suffix is to distinguish that the file is a windows shared library because I did the same test for the Linux case. When I try to do a build, though, I get the following error:
>> build(definelibfive)
Building interface file 'libfiveInterface.dll'.
Error using clibgen.internal.buildHelper (line 62)
Build failed with error:
'C:/ProgramData/MATLAB/SupportPackages/R2021a_1/3P.instrset/mingw_w64.instrset/bin/../lib/gcc/x86_64-w64-mingw32/6.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
cannot find -l-LC:\MATLAB\R2021a\extern\lib\win64\mingw64
C:/ProgramData/MATLAB/SupportPackages/R2021a_1/3P.instrset/mingw_w64.instrset/bin/../lib/gcc/x86_64-w64-mingw32/6.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
cannot find -llibMatlabDataArray
collect2.exe: error: ld returned 1 exit status
'.
Error in clibgen.LibraryDefinition/build (line 1297)
clibgen.internal.buildHelper(obj, obj.LibraryInterface, '', directBuild);
The error is the same for both this simple example case and the more complex case. In C:\MATLAB\R2021a\extern\lib\win64\mingw64, the libMatlabDataArray.lib is present.

 Accepted Answer

The cause of this problem was that Matlab failed to fill in the library name that needs to be provided to the linker (which is libfive_win.so). Libraries built from MinGW must have a .lib file extension. This is because the clibgen.internal.build function uses
idx = strfind(nameExt, '.lib');
res = nameExt(1:idx-1);
name = ['-l', res];
In my example, the MinGW extension was set up as .so causing strfind to return an empty array and name became nothing but the -l.
The linker then parsed '-l -LC:\MATLAB\R2021a\extern\lib\win64\mingw64' as one token leading to this error.
If you simply rename the library file, it will lead to link errors because internally the generated library will look for a library with the old name so it will cause
Error using which
Unable to load interface library: '[...]\libfive\libfiveInterface.dll'. Reason: The specified module
could not be found.
Ensure the C++ dependent libraries for the interface library are added to run-time path.
Related documentation
The solution was to rebuild the library so that the output had the name libfive.lib and then follow the normal pattern.
doc clib.libfive.five % Show documentation
clib.libfive.five() % Run the function

More Answers (0)

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!