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
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.gcc -v test.c
gives such output on my systemSo 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.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).