0

#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>

I m trying to compile my C code for windows from linux since I wrote the code using these header files in linux. Since, compiling in linux had no errors, but I wanted to compile for windows. I used winsock.h,winsock2.h,windows.h but still while compiling in windows, I m getting these errors:

PS C:\Users\Dell\Downloads\ChatChamber> cd "c:\Users\Dell\Downloads\ChatChamber" ; if ($?) { gcc client.c -o client -lpthread 
} ; if ($?) { .\client }
In file included from client.c:14:
C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/i686-w64-mingw32/include/winsock2.h:15:2: warning: #warning Please include winsock2.h before windows.h [-Wcpp]
 #warning Please include winsock2.h before windows.h
  ^~~
C:\Users\Dell\AppData\Local\Temp\ccnULxsd.o:client.c:(.text+0xd5): undefined reference to _imp__recv@16'
C:\Users\Dell\AppData\Local\Temp\ccnULxsd.o:client.c:(.text+0x1bd): undefined reference to _impsend@16'
C:\Users\Dell\AppData\Local\Temp\ccnULxsd.o:client.c:(.text+0x2be): undefined reference to `_impsocket@12'
C:\Users\Dell\AppData\Local\Temp\ccnULxsd.o:client.c:(.text+0x33e): undefined reference to _imp__inet_addr@4'
C:\Users\Dell\AppData\Local\Temp\ccnULxsd.o:client.c:(.text+0x352): undefined reference to _imphtons@4'
C:\Users\Dell\AppData\Local\Temp\ccnULxsd.o:client.c:(.text+0x377): undefined reference to `_impconnect@12'
C:\Users\Dell\AppData\Local\Temp\ccnULxsd.o:client.c:(.text+0x3ba): undefined reference to _imp__getsockname@12'
C:\Users\Dell\AppData\Local\Temp\ccnULxsd.o:client.c:(.text+0x3dc): undefined reference to _impgetpeername@12'
C:\Users\Dell\AppData\Local\Temp\ccnULxsd.o:client.c:(.text+0x3f0): undefined reference to `_impntohs@4'
C:\Users\Dell\AppData\Local\Temp\ccnULxsd.o:client.c:(.text+0x403): undefined reference to _imp__inet_ntoa@4'
C:\Users\Dell\AppData\Local\Temp\ccnULxsd.o:client.c:(.text+0x42b): undefined reference to _impntohs@4'
C:\Users\Dell\AppData\Local\Temp\ccnULxsd.o:client.c:(.text+0x43e): undefined reference to `_impinet_ntoa@4'
C:\Users\Dell\AppData\Local\Temp\ccnULxsd.o:client.c:(.text+0x47c): undefined reference to `_imp__send@16'
collect2.exe: error: ld returned 1 exit status
YesThatIsMyName
  • 1,458
  • 3
  • 22
  • 28
  • @phuclv C++ is the C with ++(increment) of one feature that are 'classes'. This problem might arise in C++ as well so I tagged C++ as well. I hope you give me proper solution if you have one – GauravGiri Aug 26 '21 at 08:44
  • 1
    You need to link the socket library. The documentation tells you what to link. – the busybee Aug 26 '21 at 08:49
  • @thebusybee I have used `winsock2.h` , `winsock.h`, `windows.h` and change some of the portion of code accordingly to these header files and this was the last error I couldn't understand – GauravGiri Aug 26 '21 at 08:51
  • 2
    You need to understand the difference between header files and the libraries, of which these declare the public API. You include header files so that the compiler knows about the functions, **and** you need to link the library so that your calls are resolved. These are different steps! – the busybee Aug 26 '21 at 08:56
  • @GauravGiri no, C++ is not C with classes or a superset of C. They have very different features, for example C++ doesn't have `_Complex` or VLA like C and C doesn't have the huge STL. On SO you only tag the language you use. Only tag both C and C++ if the question relates to both languages like interoperability [Why are questions tagged C answered in C++?](https://meta.stackoverflow.com/q/407637/995714) – phuclv Aug 26 '21 at 09:01
  • @phuclv ohhh...thanks about the knowledge. – GauravGiri Aug 26 '21 at 09:05
  • @thebusybee thank you so much for the idea, it worked when i linked the ws2_32 library. the thing command i used while compiling was : `gcc client.c -o client -lpthread -lws2_32` and also I used WSStartup() to resolve the socket creation failure. – GauravGiri Aug 26 '21 at 10:17

0 Answers0