由于<winsock2.h>包含,socket.h无法编译

时间:2017-12-20 23:17:55

标签: c++ sockets netbeans

我正在使用带有g ++编译器(cygwin)的Netbeans 8.1 IDE 构建一个客户端Socket类,其实现定义良好 在socket.cpp中,并在socket.h中声明,如下所示:

//socket.h

#ifndef SOCKET_H
#define SOCKET_H
  
#include <windows.h>
#include <winsock2.h>     /*Code builds successfully if this line is commented. errors otherwise*/

class Socket {
    
public: 
    Socket(){std::cout << "Testing: Inside socket constructor ..." << endl;}
    Socket(const Socket&);
    virtual ~Socket();
    Socket& operator=(Socket&);
    
    void Close();

protected: 

    Socket(SOCKET s);   /*Windows socket handle, defined in <winsock2.h>*/
     
    SOCKET s_;

    int* refCounter_;

private:
    static bool Start();
    static void End();
    static int nofSockets_;

};

#endif /* SOCKET_H */

现在,我试试这样:

//main.cpp

#include "socket.h"

int main() {
    Socket socket;
    return 0;
}

删除winsock2.h时,上面的代码已成功构建,否则会出错。 老实说,我是netbeans IDE的新手,我无法理解你在做什么 错误。我也不知道IDE对我的要求。我需要你的帮助 请调试一下。谢谢。这是错误:

cd 'C:\wamp\www\klandestine'
C:\cygwin64\bin\make.exe -f Makefile CONF=Debug clean
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .clean-conf
make[1]: Entering directory '/cygdrive/c/wamp/www/klandestine'
rm -f -r build/Debug
rm -f dist/Debug/Cygwin-Windows/klandestine.exe
make[1]: Leaving directory '/cygdrive/c/wamp/www/klandestine'

CLEAN SUCCESSFUL (total time: 656ms)
cd 'C:\wamp\www\klandestine'
C:\cygwin64\bin\make.exe -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/cygdrive/c/wamp/www/klandestine'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin-Windows/klandestine.exe
make[2]: Entering directory '/cygdrive/c/wamp/www/klandestine'
mkdir -p build/Debug/Cygwin-Windows
rm -f "build/Debug/Cygwin-Windows/main.o.d"
g++    -c -g -MMD -MP -MF "build/Debug/Cygwin-Windows/main.o.d" -o build/Debug/Cygwin-Windows/main.o main.cpp
In file included from /usr/include/w32api/winsock2.h:56:0,
                 from socket.h:12,
                 from main.cpp:14:
/usr/include/w32api/psdk_inc/_fd_types.h:100:2: warning: #warning "fd_set and associated macros have been defined in sys/types.      This can cause runtime problems with W32 sockets" [-Wcpp]
 #warning "fd_set and associated macros have been defined in sys/types.  \
  ^
In file included from socket.h:12:0,
                 from main.cpp:14:
/usr/include/w32api/winsock2.h:995:123: error: conflicting declaration of C function 'int select(int, _types_fd_set*, _types_fd_set*, _types_fd_set*, PTIMEVAL)'
   WINSOCK_API_LINKAGE int WSAAPI select(int nfds,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,const PTIMEVAL timeout);
                                                                                                                           ^
In file included from /usr/include/sys/types.h:68:0,
                 from /usr/include/pthread.h:14,
                 from /usr/lib/gcc/x86_64-pc-cygwin/5.3.0/include/c++/x86_64-pc-cygwin/bits/gthr-default.h:35,
                 from /usr/lib/gcc/x86_64-pc-cygwin/5.3.0/include/c++/x86_64-pc-cygwin/bits/gthr.h:148,
                 from /usr/lib/gcc/x86_64-pc-cygwin/5.3.0/include/c++/ext/atomicity.h:35,
                 from /usr/lib/gcc/x86_64-pc-cygwin/5.3.0/include/c++/bits/ios_base.h:39,
                 from /usr/lib/gcc/x86_64-pc-cygwin/5.3.0/include/c++/ios:42,
                 from /usr/lib/gcc/x86_64-pc-cygwin/5.3.0/include/c++/ostream:38,
                 from /usr/lib/gcc/x86_64-pc-cygwin/5.3.0/include/c++/iostream:39,
                 from main.cpp:9:
/usr/include/sys/select.h:73:5: note: previous declaration 'int select(int, _types_fd_set*, _types_fd_set*, _types_fd_set*, timeval*)'
 int select __P ((int __n, fd_set *__readfds, fd_set *__writefds,
     ^
make[2]: *** [nbproject/Makefile-Debug.mk:69: build/Debug/Cygwin-Windows/main.o] Error 1
make[2]: Leaving directory '/cygdrive/c/wamp/www/klandestine'
make[1]: *** [nbproject/Makefile-Debug.mk:60: .build-conf] Error 2
make[1]: Leaving directory '/cygdrive/c/wamp/www/klandestine'
make: *** [nbproject/Makefile-impl.mk:40: .build-impl] Error 2

BUILD FAILED (exit value 2, total time: 3s)

0 个答案:

没有答案