Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Mex File C Sockets

159 views
Skip to first unread message

F E

unread,
Jun 22, 2016, 3:39:08 PM6/22/16
to
Hello,

I'm looking to open a socket using a matlab mex file, but on compilation I get a number of errors stating that there are undefined references. For example:

C:\Users\eraz\AppData\Local\Temp\mex_61256728966913_2780\socket.obj:socket.cpp:(.text+0x52):
undefined reference to `__imp_htons'

The header files are properly included, so I'm at a loss as to what could be causing this. If more info is required, I can try to provide it.

Thanks a bunch,

Fab

F E

unread,
Jun 22, 2016, 3:46:09 PM6/22/16
to
So as I post this I realized I left out that I'm running on a 64-bit Windows machine.

James Tursa

unread,
Jun 22, 2016, 7:51:09 PM6/22/16
to
"F E" wrote in message <nkepgn$j42$1...@newscl01ah.mathworks.com>...
The header files only let the compiler know function signatures etc. What you have is a linker error, not a compiler error. You need to list all of the necessary library files that your code relies on in the mex command.

James Tursa

F E

unread,
Jun 23, 2016, 9:17:08 AM6/23/16
to
"James Tursa" wrote in message <nkf898$6oe$1...@newscl01ah.mathworks.com>...
Hi James,

Thanks for your reply. I tried linking WS2_32.lib without success, though admittedly the socket code might need other libraries that I'm not aware of. I have two question, in this case:

1) Can you verify that the following command is correct mex syntax? Perhaps it isn't linking properly:
mex -I"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A" -I./PacketInterfaceCode SingleAxisPacketSend.cpp ./PacketInterfaceCode/Socket.cpp WS2_32.lib

2) The program is using the MinGW64 C++ compiler -- is there a way to change compilers such that it has automatic access to the required libraries? I've heard mention of a visual studio compiler which supposedly might do the trick.

Thanks again!

F E

unread,
Jun 23, 2016, 2:36:10 PM6/23/16
to
"F E" wrote in message <nkgngf$53c$1...@newscl01ah.mathworks.com>...
I figured it out, so for the sake of completion (and to help anybody who's unlucky enough to run into this problem later on) I'll enumerate what I had done:
1) The MinGW64 C++ compiler that I was using didn't know where to look for the C++ libraries (though from what I've dug up, it is possible to modify the mex.bat file in Windows to path it properly -- in any case, read on). Follow the guide provided here:
https://www.mathworks.com/matlabcentral/answers/96611-how-do-i-install-microsoft-visual-c-2010-and-microsoft-windows-sdk-7-1
which details how to install the Microsoft Windows SDK7.1 compiler despite a know bug regarding order or installation. You may have to uninstall current version(s) of VC++ and associated software (redistributables, prereqs, etc.).

2) In my case, even after the installation mex didn't recognize the SDK compiler. I had to manually set it up using:
mex -setup:'C:\Program Files\MATLAB\R2016a\bin\win64\mexopts\winsdk-7.1_cpp.xml' C++

3) Attempting to compile now resulted in unresolved external symbols, rather than undefined functions. This is good (well, better). I then had to explicitly include WS2_32.lib at the end of my mex command. Since I was using the SDK compiler I gather it knew where to look for the library once included.

My command at the end therefore looked like this, where the .lib file was NOT in the local directory:
mex -I"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include" SingleAxisPacketSend.cpp Socket.cpp WS2_32.lib
0 new messages