简体   繁体   中英

gcc unable to find “windows.h, winsock2.h and ws2tcpip.h” header files even after installing mingw package on Linux

I am trying to create ac/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

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM