MinGW-w64 gcc compilier improperly linked with matlab libraries

I am using the MinGW-64 compiler to attempt to compile some C code in Matlab using mex.
The following code works fine:
mex('-c', '-g', '-I../include', filenames)
however, the below code gives the following error
mex('-g', '-I../include', -output, 'MexFunction.c', filenames, 'ws2_32.lib')
Error using mex
gcc: error: ws2_32.lib: No such file or directory
ws2_32.lib is a matlab library on the matlab path and I would get the same error no matter what library I chose. If I add the lib path with -Lpath ie.
mex('-g', '-I../include', -output, 'MexFunction.c', filenames,...
'-LC:\Program Files\MATLAB\R2016a\sys\lcc64\lcc64\lib64', 'ws2_32.lib')
I get the same error. I believe a similar unanswered question has been posted here .
I am using Matlab 2016a, MinGW-w64 version 4.9.2, and Windows 10.

Answers (1)

You need to specify the library name with 'l' option:
Also, it used gcc under the hood, so it may not be able to detect the file even if it is on the MATLAB path.
If it does not help, then use -v mode (verbose) to find the verbose output and paste it here.

4 Comments

Thanks for the verbose tip! I think I found the problem but I can't understand why it is occuring or how to fix it. I run the command:
mex('-g', '-I../include', -output, 'MexFunction.c', filenames,...
'-LC:\Program Files\MATLAB\R2016a\sys\lcc64\lcc64\lib64', 'ws2_32.lib')
And I get the following verbose output:
LINKLIBS : ws2_32.lib "-LC:\Program Files\MATLAB\R2016a\sys\lcc64\lcc64\lib64" -L"C:\Program Files\MATLAB\R2016a\extern\lib\win64\mingw64"
The path to the extra library I want to link is "-LC:..." not -L"C:..."
Hi Benjamin,
Did this ever get resolved? I too have the same problem only with R2017b. One thing I can recommend is to try:
"-LC:\Program Files\MATLAB\R2016a\sys\lcc64\lcc64\lib64"
with the double quotes otherwise the space in Program Files screws up MATLAB.
But, I'm not sure if that's the problem either
Actually I was able to successfully compile. I did
mex -v '-LC:\Program Files\MATLAB\R2017b\sys\lcc64\lcc64\lib64' -lws2_32 open_socket_connection.cpp
and it worked! Use single quotes and make sure you include -lws2_32 so it knows which .lib file to use.
>> mex Your_C_code.c "C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\um\x64\WS2_32.Lib"

Sign in to comment.

Asked:

on 25 Apr 2016

Commented:

on 14 Feb 2023

Community Treasure Hunt

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

Start Hunting!