我有点困惑,我正在尝试制作一个没有外部依赖的程序(因此它可以很容易地移植到UNIX),它具有套接字连接功能。 但我不确定我应该使用什么?
在Windows XP,7,8上是否有一个可以链接的标准库,可以输出一个可以单独运行的exe(不依赖于DLL来运行)? 例如,在UNIX上,我可以链接到socket并使用sys/socket.h并且有95%的UNIX没有做任何事情。
I am a little confused, I am trying to make a program with no external dependencies (so it could be easily ported to UNIX for example) that has socket connection abilities. But I am unsure of what I should be using?
Is there a standard library on Windows XP, 7, 8 that can be linked against that can output a single exe that can run alone (not depend on a DLL to function)? For example on UNIX I can link against socket and use sys/socket.h and have like 95% of UNIX's without doing anything.
最满意答案
相同的代码对两者都不起作用。 但是你可以使用#ifdef WIN32使预处理器只在Windows下编译时考虑Windows的东西。
在标题中:
#ifdef WIN32 // Windows exclusive dependencies go here #include <windows.h> #include <winsock.h> #pragma comment(lib, "ws2_32.lib") #define close(s) closesocket(s) #else // Linux exclusive dependencies go here #include <sys/socket.h> #endif在main()你需要做这样的事情:
#ifdef WIN32 WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); #endif我相信这就是全部,API的其余部分与使用套接字的UNIX方式广泛兼容。
Same code just won't do for both. But you can use #ifdef WIN32 to make the preprocessor consider Windows stuff only when compiling under Windows.
In the headers:
#ifdef WIN32 // Windows exclusive dependencies go here #include <windows.h> #include <winsock.h> #pragma comment(lib, "ws2_32.lib") #define close(s) closesocket(s) #else // Linux exclusive dependencies go here #include <sys/socket.h> #endifAnd inside main() you will need to do something like this:
#ifdef WIN32 WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); #endifI believe this is all, the rest of the API is widely compatible with the UNIX way of using sockets.
更多推荐
发布评论