cygwin + Windows套接字编程 [英] cygwin + Windows socket programming
问题描述
我正在尝试学习Windows中的Socket编程,并且正在使用cygwin.我发现,所需的相同文件位于/usr/include/w32api/
.
I am trying to learn Socket programming in Windows and am using cygwin for the same. I found out that the required files needed for the same were at /usr/include/w32api/
.
我从net处获取了一个示例程序,并尝试进行编译,但无法这样做....相同的代码是
I took a sample program from net and tried to compile but was unable to do so.... The code for the same is
#include <w32api/windows.h>
#include <w32api/winsock.h>
#include <stdio.h>
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow)
{
WORD sockVersion;
WSADATA wsaData;
int nret;
sockVersion = MAKEWORD(1, 1); // We'd like Winsock version 1.1
// We begin by initializing Winsock
WSAStartup(sockVersion, &wsaData);
// rest part of code
}
我在cygwin环境中使用gcc-3进行编译,并得到了非常奇怪的错误.....
I compile it using gcc-3 in cygwin environment and get very strange errors .....
undefined reference to '_WSAStartup08' and many such errors...
我已经从一个教程站点上获取了代码,因此想知道我在做什么错以及应该如何运行该程序.
I have taken the code from a tutorial site and thus would like to know what am i dng wrong and how should i run the program.
非常感谢..
编辑---------
edit ---------
我也曾尝试使用winsock2而不是winsock.h,但错误仍然存在...
I have also tried to use winsock2 instead of winsock.h but the errors persist...
推荐答案
-lws2_32
应该这样做.
但是,Cygwin在winsock之上有自己的POSIX兼容套接字实现,通常将它们混在一起不是一个好主意.如果您想坚持使用Winsock,则可能要使用gcc-3的 -mno-cygwin
选项,该选项可以使Cygwin DLL脱颖而出.(您还需要从 #include
行中删除 w32api/
.)
However, Cygwin has its own POSIX-compatible socket implementation on top of winsock, and mixing things up generally is not a good idea. If you want to stick with winsock, you probably want to use gcc-3's -mno-cygwin
option that takes the Cygwin DLL out of the equation. (You'll also need to drop w32api/
from the #include
lines.)
这篇关于cygwin + Windows套接字编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!