Search code examples
buildconfigureopenocd

Windows (MSYS2) built OpenOCD fails to create sockets


I've cloned openOCD from https://github.com/gnu-mcu-eclipse/openocd and built it using MSYS2 (MINGW32) shell. Build passed without any trouble and when tried to execute, it failed and gave error "Error: error creating socket".

I enabled debugging option and found it is coming from server.c file. I'm using 64 bit Windows 10 PC for building.

Error is coming from below code segment,

    c->fd = socket(AF_INET, SOCK_STREAM, 0);
    if (c->fd == -1) {
        LOG_ERROR("error creating socket: %s", strerror(errno));
        exit(-1);
    }

I noticed that a macro which has to be enabled for WIN32 build has not gotten enabled, which if enabled calls windows specific socket functions.

int server_preinit(void)
{
/* this currently only calls WSAStartup on native win32 systems
 * before any socket operations are performed.
 * This is an issue if you call init in your config script */

#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData;

wVersionRequested = MAKEWORD(2, 2);

if (WSAStartup(wVersionRequested, &wsaData) != 0) {
    LOG_ERROR("Failed to Open Winsock");
    exit(-1);
}

/* register ctrl-c handler */
SetConsoleCtrlHandler(ControlHandler, TRUE);

signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
signal(SIGBREAK, sig_handler);
signal(SIGABRT, sig_handler);
#endif

return ERROR_OK;
}

I manually enabled this macro, and compilation failed.

Can anyone help to identify if I missed something in configuring openOCD? I'm invoking configure script by "--disable-werror" option alone.


Solution

  • You need to build OpenOCD with MinGW compiler, not MSYS one. You can try to use script from this repo. Also the link has description how to install MinGW under MSYS.