skip to Main Content

I am trying to create a c/c++ program using winsock2.h, windows.h and ws2tcpip.h in Linux. But I get the following error messages:

cannot open source file “winsock2.h”C/C++(1696)

Likewise for windows.h

cannot open source file “windows.h”C/C++(1696)

I have already installed Mingw on my system using this command

mingw-w64-common mingw-w64-i686-dev mingw-w64-tools mingw-w64-x86-64-dev

My Program

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h> 
#include <stdio.h> 
#pragma comment(lib, "Ws2_32.lib") 

 int main() { return 0; }

VSC ver – 1.45.1

OS – Debian

2

Answers


  1. The problems seem to be something related to “not finding header files”. If with gcc to include the custom headers path (or directory), you do something as below.

    NOTE: My intentions here are not using the mingw offered headers with gcc; this is just for demonstration purpose.

    dpkg -L mingw-w64-common gives such output on my system.

    /usr/share/mingw-w64/include/windows.h
    /usr/share/mingw-w64/include/winsock2.h
    /usr/share/mingw-w64/include/ws2tcpip.h
    

    gcc -v test.c gives such output on my system

    #include "..." search starts here:
    #include <...> search starts here:
     /usr/lib/gcc/x86_64-linux-gnu/9/include
     /usr/local/include
     /usr/lib/gcc/x86_64-linux-gnu/9/include-fixed
     /usr/include/x86_64-linux-gnu
     /usr/include
    

    So it is evident that the headers or not in the default search path. In this particular case, you need to compile with gcc -I/usr/share/mingw-w64/include. You give a similar option for your toolchain’s compiler. But then you may need to deal with the linking errors based on what libraries your code is using.

    Login or Signup to reply.
  2. The message format cannot open source file "winsock2.h"C/C++(1696) doesn’t look like to me like something GCC would say.
    Is it possible that while you installed MinGW-w64 you are not using its GCC compiler?
    For MinGW-w64 to work you need to actualy us it’s toolchain (compiler, linker).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search