我在 Windows 机器上的 Eclipse 中用 C 语言创建了一个经典的客户端服务器程序。代码本身不会产生任何错误。我得到的唯一错误是关于库的。我已经包含了以下库:
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
但是当我尝试构建项目时出现错误:
error "ws2tcpip.h is not compatible with winsock.h. Include winsock2.h instead".
唯一的问题是我没有包含winsock.h,而只包含winsock2.h,就像他们指定的那样。
如何解决这个问题?
更新
我现在按以下顺序定义了库:
#define _WINSOCKAPI_ /* Prevent inclusion of winsock.h in windows.h */
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
但现在我收到更多错误:
undefined reference to `socket@12'
undefined reference to `gethostbyname@4'
undefined reference to `connect@12'
undefined reference to `htons@4'
undefined reference to `WSACleanup@0'
undefined reference to `closesocket@4'
undefined reference to `WSACleanup@0'
undefined reference to `WSAStartup@8'
更新2
我认为这个警告可能与我仍然不断收到的错误有关:
ignoring #pragma comment [-Wunknown-pragmas]
最佳答案
winsock2.h
顶部有一行:
#define _WINSOCKAPI_ /* Prevent inclusion of winsock.h in windows.h */
您需要在 windows.h
之前包含 winsock2.h
。
关于客户端服务器程序 C Windows - 库冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23365066/