cmatlabdllmingw-w64winmain

MinGW-W64 undefined reference to `WinMain' compiling DLL


I'm trying to create a DLL from a set of MatLab scripts and a matching wrapper DLL (I need it to adapt functions calls/parameters to be used in LabView).

Creating the DLL from the MatLab script goes pretty straightforward there are many examples, I got all the files that such compilation is supposed to produce (.dll, .h, .lib, .def etc).

I followed a template for making the wrapper and the resuling source seems correct but when I try to compile it with this command:

mbuild -v A0007121_3LIV_W.c A0007121_3LIV.lib LINKFLAGS="$LINKFLAGS /DLL /DEF:A0007121_3LIV_W.def" LDEXT=".dll" CMDLINE250="mt -outputresource:$EXE';'2 -manifest $MANIFEST"

I got this:

Verbose mode is on.
... Looking for compiler 'MinGW64 Compiler (C)' ...
    ... Looking for environment variable 'MW_MINGW64_LOC' ...Yes ('C:\mingw64').
    ... Looking for file 'C:\mingw64\bin\gcc.exe' ...Yes.
    ... Looking for folder 'C:\mingw64' ...Yes.
    [..]
Building with 'MinGW64 Compiler (C)'.
C:\mingw64\bin\gcc -c -DMATLAB_DEFAULT_RELEASE=R2017b  -DUSE_MEX_CMD   -m64  -I"C:\Program Files\MATLAB\R2019b/extern/include" -I"C:\Program Files\MATLAB\R2019b/simulink/include" -I"C:\Program Files\MATLAB\R2019b/extern\lib\win64\mingw64" -I"C:\Program Files\MATLAB\R2019b\extern\include\win64" -fexceptions -fno-omit-frame-pointer -O2 -fwrapv -DNDEBUG "C:\Users\m.santucci\Documents\MATLAB\SIRIO\A0007121_3LIV\A0007121_3LIV_W.c" -o C:\Users\MAB0B~1.SAN\AppData\Local\Temp\5\mex_4521000822066357_3928\A0007121_3LIV_W.obj
C:\mingw64\bin\gcc -m64 -Wl,--no-undefined,--out-implib,"A0007121_3LIV_W.lib"  -s  C:\Users\MAB0B~1.SAN\AppData\Local\Temp\5\mex_4521000822066357_3928\A0007121_3LIV_W.obj   A0007121_3LIV.lib  -L"C:\Program Files\MATLAB\R2019b\extern\lib\win64\mingw64" -lmclmcrrt -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -luuid -lodbc32 -lodbccp32 -o A0007121_3LIV_W.dll
Error using mbuild (line 166)
Unable to complete successfully.
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/6.3.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e):
undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status

I'm using MatLab 2019b, MinGW-W64 6.3.0

--- SOURCES ---

Script source.

function [SNR_dB, PhaseNoise_mean] = Misura_SNR_con_SBX(FullName, Figure_path)
   ...
end

Wrapper header.

#ifndef A0007121_3LIV_W
#define A0007121_3LIV_W

void loadA0007121_3LIV(void);
void unloadA0007121_3LIV(void);

int wmlfMisura_SNR_con_SBX(char* FullName, char* FigurePath, double* SNR_dB, double* PhaseNoise_mean);

#endif

Wrapper code.

#include "A0007121_3LIV_W.h"
#include "A0007121_3LIV.h"

#include "matrix.h"

void loadA0007121_3LIV(void)
{
    A0007121_3LIVInitialize();
}

void unloadA0007121_3LIV(void)
{
    A0007121_3LIVTerminate();
}

int wmlfMisura_SNR_con_SBX(char* FullName, char* FigurePath, double* SNR_dB, double* PhaseNoise_mean)
{
    int result = 1;

    mxArray* mFullName = mxCreateString(FullName);
    mxArray* mFigure_path = mxCreateString(FigurePath);

    int nargout = 2;

    mxArray* mSNR_dB = NULL;
    mxArray* mPhaseNoise_mean = NULL;

    //int nargout, mxArray** SNR_dB, mxArray** PhaseNoise_mean, mxArray* FullName, mxArray* Figure_path
    result = mlfMisura_SNR_con_SBX(
        nargout, 
        &mSNR_dB, 
        &mPhaseNoise_mean, 
        mFullName, 
        mFigure_path);

    //TODO: outputs ?

    memcpy(SNR_dB, mxGetPr(mSNR_dB), sizeof(double));
    memcpy(PhaseNoise_mean, mxGetPr(mPhaseNoise_mean), sizeof(double));

    mxDestroyArray(mFullName);
    mxDestroyArray(mFigure_path);
    mxDestroyArray(mSNR_dB);
    mxDestroyArray(mPhaseNoise_mean);

    return result;
}

Wrapper .def file.

LIBRARY A0007121_3LIV_W
EXPORTS
loadA0007121_3LIV
unloadA0007121_3LIV
wmlfMisura_SNR_con_SBX

I can share more details if needed.

--- EDIT ---

I tried to add an empty main to the wrapper C code:

int main(int argc, char* argv[])
{
}

Now WinMain reference error has been fixed (!?) but I got this:

'mt' is not recognized as an internal or external command,
operable program or batch file.

Solution

  • I fixed the problem simply removing:

    CMDLINE250="mt -outputresource:$EXE';'2 -manifest $MANIFEST"
    

    I don't know why this option have been added to the command line (it's a reccomendation provided by MatLab support) but it deals with a manifest that's not available following the standard workflow for creating the library. Removing it just let the process and gracefully and the DLL file is finally created.