Discussion:
Redefinition and other errors
(too old to reply)
Rayne
2009-12-09 03:20:31 UTC
Permalink
Hi all,

I'm sorry, but this is going to be a long post, because I'm trying to
be as detailed as possible. I'm running the code on Visual Studio .NET
2003. The entire project has 28 header files, and corresponding .c
files where applicable. I'm new to programming in Windows, and I'm
getting confused with all the different Windows header files that seem
to be causing these errors. I'm going to split the post into 2 since
it is long.

The 28 self-defined header files (A.h - Z.h, AA.h, BB.txt) are listed
below, showing what other header files are included. All header files
have #include guards.

A.h
----
#include <WinSock2.h>
#include <Ws2tcpip.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <curses.h>
#include <tchar.h>
#include <strsafe.h>

#include "X.h"
#include "AA.h"
#include "R.h"

B.h
----
#include "X.h"
#include "M.h"

C.h
----
#include <WinSock2.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "M.h"

D.h
----
No #includes

E.h
----
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <strsafe.h>

#include "D.h"
#include "R.h"

F.h
----
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

G.h
----
#include <time.h>
#include <curses.h>
#include <pthread.h>
#include <time.h>
#include <pcap.h>

#include "A.h"

H.h
----
#include <WinSock2.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
#include <string.h>
#include <strsafe.h>
#include <signal.h>

#include "D.h"
#include "U.h"
#include "F.h"
#include "BB.txt"
#include "M.h"
#include "R.h"

I.h
----
#include "V.h"
#include "Y.h"
#include "K.h"
#include "J.h"

J.h
----
#include <stdlib.h>
#include <vector>

#include "X.h"

K.h
----
#include <WinSock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
#include <string.h>

#include "L.h"
#include "X.h"

L.h
----
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>

M.h
----
#include <WinSock2.h>
#include <ws2tcpip.h>

#include "X.h"

N.h
----
#include <winsock2.h>
#include <windows.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "X.h"
#include "A.h"
#include "P.h"

O.h
----
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <pcap.h>
#include <pthread.h>
#include <strsafe.h>

#include "Z.h"
#include "K.h"

P.h
----
#include <WinSock2.h>
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <time.h>
#include <fcntl.h>

#include "X.h"

Q.h
----
#include <stdlib.h>
#include <pthread.h>
#include <vector>
#include <algorithm>

#include "K.h"
#include "V.h"
#include "C.h"
#include "S.h"
#include "I.h"

R.h
----
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>

S.h
----
#include <stdlib.h>
#include <stdio.h>
#include <windef.h>

T.h
----
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <pcap.h>
#include <signal.h>
#include <strsafe.h>

#include "V.h"
#include "Y.h"
#include "W.h"
#include "Q.h"
#include "N.h"
#include "G.h"
#include "A.h"

U.h
----
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

V.h
----
#include "K.h"
#include "O.h"
#include "B.h"
#include "I.h"
#include "M.h"

W.h
----
#include <stdlib.h>
#include <time.h>
#include <pthread.h>

#include "K.h"
#include "Q.h"

X.h
----
No #includes

Y.h
----
#include "K.h"
#include "O.h"
#include "B.h"
#include "S.h"
#include "I.h"
#include "M.h"

Z.h
----
#include <Windows.h>
#include <time.h>

#include "X.h"

AA.h
-----
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <Windows.h>

BB.txt
-------
No #includes
Rayne
2009-12-09 03:39:03 UTC
Permalink
I'm getting mostly redefinition errors that point to Winsock2.h and
WS2tcpip.h. They all refer to "see previous definition if
'<variable>'" in WinSock.h. I'm also getting errors (not redefinition)
that point to WSPiAPi.h, even though this file is not explicitly
#included by me.

There are over a hundred errors, and I'm showing the representative
ones:

error C2011: 'fd_set' : 'struct' type redefinition
error C2011: 'hostent' : 'struct' type redefinition
error C2011: 'ip_mreq' : 'struct' type redefinition
error C2011: 'linger' : 'struct' type redefinition
error C2011: 'timeval' : 'struct' type redefinition
error C2011: 'WSAData' : 'struct' type redefinition
error C2375: 'accept' : redefinition; different linkage
error C2375: 'bind' : redefinition; different linkage
error C2375: 'connect' : redefinition; different linkage
error C2375: 'gethostbyname' : redefinition; different linkage
error C2375: 'gethostname' : redefinition; different linkage
error C2375: 'htonl' : redefinition; different linkage
error C2375: 'inet_addr' : redefinition; different linkage
error C2375: 'inet_ntoa' : redefinition; different linkage
error C2375: 'WSAAsyncGetHostByAddr' : redefinition; different linkage
error C2375: 'WSAStartup' : redefinition; different linkage

error C2440: '=' : cannot convert from 'char *(__stdcall *)(in_addr)'
to 'char *'
error C2440: '=' : cannot convert from 'char *(__stdcall *)(in_addr)'
to 'char *' There is no context in which this conversion is possible
error C2440: '=' : cannot convert from 'hostent *(__stdcall *)(const
char *)' to 'hostent *'
error C2440: '=' : cannot convert from 'hostent *(__stdcall *)(const
char *)' to 'hostent *' There is no context in which this conversion
is possible
error C2440: '=' : cannot convert from 'servent *(__stdcall *)(const
char *,const char *)' to 'servent *'
error C2440: '=' : cannot convert from 'servent *(__stdcall *)(const
char *,const char *)' to 'servent *' There is no context in which this
conversion is possible
error C2440: '=' : cannot convert from 'u_long (__stdcall *)(u_long)'
to 'DWORD'
error C2440: '=' : cannot convert from 'u_long (__stdcall *)(u_long)'
to 'DWORD' This conversion requires a reinterpret_cast, a C-style cast
or function-style cast
error C2450: switch expression of type 'int (__stdcall *)(void *) is
illegal Integral expression required
error C3861: 'gethostbyaddr': identifier not found, even with argument-
dependent lookup
error C3861: 'getservbyname': identifier not found, even with argument-
dependent lookup
error C3861: 'htonl': identifier not found, even with argument-
dependent lookup
error C3861: 'htons': identifier not found, even with argument-
dependent lookup
error C3861: 'inet_ntoa': identifier not found, even with argument-
dependent lookup
error C3861: 'inet_addr': identifier not found, even with argument-
dependent lookup
error C3861: 'ntohs': identifier not found, even with argument-
dependent lookup
error C3861: 'WSAGetLastError': identifier not found, even with
argument-dependent lookup

warning C4005: 'AF_IPX' : macro redefinition
warning C4005: 'AF_MAX' : macro redefinition
warning C4005: 'FD_ACCEPT' : macro redefinition
warning C4005: 'FD_CONNECT : macro redefinition
warning C4005: 'IP_ADD_MEMBERSHIP' : macro redefinition
warning C4005: 'IP_DROP_MEMBERSHIP' : macro redefinition
warning C4005: 'IP_TOS' : macro redefinition
warning C4005: 'SO_DONTLINGER' : macro redefinition
warning C4005: 'SOMAXCONN' : macro redefinition

I don't know why I'm getting these errors, and I would greatly
appreciate any help I can get.

Thank you!

Regards,
Rayne
Ulrich Eckhardt
2009-12-09 12:12:03 UTC
Permalink
Post by Rayne
I'm getting mostly redefinition errors that point to Winsock2.h and
WS2tcpip.h. They all refer to "see previous definition if
'<variable>'" in WinSock.h. I'm also getting errors (not redefinition)
that point to WSPiAPi.h, even though this file is not explicitly
#included by me.
winsock2.h defines several things that winsock.h also defines. To make that
work, winsock2.h checks the winsock.h include guards, too. The problem is
that windows.h includes winsock.h, so if you need anything from winsock2.h
including it afterwards doesn't help. I consider this a bug in the MS
Windows SDK.

To work around this, include winsock2.h before windows.h. Note that standard
headers like e.g. stdio.h also sometimes include windows.h, making the
problem worse. Including winsock2.h as first header of every translation
unit (*.c) where you get errors should do the job.

BTW: You included pthread.h but POSIX threads are not available on win32.
You can install a pthread emulation library though, provided you really
need it.

Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Giovanni Dicanio
2009-12-09 12:09:08 UTC
Permalink
Post by Rayne
I'm getting mostly redefinition errors that point to Winsock2.h and
WS2tcpip.h. They all refer to "see previous definition if
'<variable>'" in WinSock.h. I'm also getting errors (not redefinition)
that point to WSPiAPi.h, even though this file is not explicitly
#included by me.
O.h
----
#include <windows.h>
[...]
#include "K.h"
K.h
----
#include <WinSock2.h>
#include <Ws2tcpip.h>
[...]
So, it seems that <windows.h> (from O.h) could be included before
<winsock2.h> (from K.h), which could cause errors.

Using precompiled headers (like David already suggested) may help you - you
may want to add these #includes to "StdAfx.h":

#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <tchar.h>
#include <strsafe.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <vector>


HTH,
Giovanni
Rayne
2009-12-10 01:26:28 UTC
Permalink
I added the Command Line Option /D_WINSOCKAPI_ and can now compile and
run the program, even though I get multiple warnings about
_WINSOCKAPI_ redefinition. It seems that WinSock2.h defines
_WINSOCKAPI_, but not Windows.h.

Thank you for all your help!

David Wilkinson
2009-12-09 11:42:33 UTC
Permalink
Post by Rayne
Hi all,
I'm sorry, but this is going to be a long post, because I'm trying to
be as detailed as possible. I'm running the code on Visual Studio .NET
2003. The entire project has 28 header files, and corresponding .c
files where applicable. I'm new to programming in Windows, and I'm
getting confused with all the different Windows header files that seem
to be causing these errors. I'm going to split the post into 2 since
it is long.
The 28 self-defined header files (A.h - Z.h, AA.h, BB.txt) are listed
below, showing what other header files are included. All header files
have #include guards.
A.h
----
#include <WinSock2.h>
#include <Ws2tcpip.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <curses.h>
#include <tchar.h>
#include <strsafe.h>
#include "X.h"
#include "AA.h"
#include "R.h"
B.h
----
#include "X.h"
#include "M.h"
C.h
----
#include <WinSock2.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "M.h"
D.h
----
No #includes
E.h
----
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <strsafe.h>
#include "D.h"
#include "R.h"
F.h
----
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
G.h
----
#include <time.h>
#include <curses.h>
#include <pthread.h>
#include <time.h>
#include <pcap.h>
#include "A.h"
H.h
----
#include <WinSock2.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
#include <string.h>
#include <strsafe.h>
#include <signal.h>
#include "D.h"
#include "U.h"
#include "F.h"
#include "BB.txt"
#include "M.h"
#include "R.h"
I.h
----
#include "V.h"
#include "Y.h"
#include "K.h"
#include "J.h"
J.h
----
#include <stdlib.h>
#include <vector>
#include "X.h"
K.h
----
#include <WinSock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
#include <string.h>
#include "L.h"
#include "X.h"
L.h
----
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
M.h
----
#include <WinSock2.h>
#include <ws2tcpip.h>
#include "X.h"
N.h
----
#include <winsock2.h>
#include <windows.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "X.h"
#include "A.h"
#include "P.h"
O.h
----
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <pcap.h>
#include <pthread.h>
#include <strsafe.h>
#include "Z.h"
#include "K.h"
P.h
----
#include <WinSock2.h>
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <time.h>
#include <fcntl.h>
#include "X.h"
Q.h
----
#include <stdlib.h>
#include <pthread.h>
#include <vector>
#include <algorithm>
#include "K.h"
#include "V.h"
#include "C.h"
#include "S.h"
#include "I.h"
R.h
----
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>
S.h
----
#include <stdlib.h>
#include <stdio.h>
#include <windef.h>
T.h
----
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <pcap.h>
#include <signal.h>
#include <strsafe.h>
#include "V.h"
#include "Y.h"
#include "W.h"
#include "Q.h"
#include "N.h"
#include "G.h"
#include "A.h"
U.h
----
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
V.h
----
#include "K.h"
#include "O.h"
#include "B.h"
#include "I.h"
#include "M.h"
W.h
----
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include "K.h"
#include "Q.h"
X.h
----
No #includes
Y.h
----
#include "K.h"
#include "O.h"
#include "B.h"
#include "S.h"
#include "I.h"
#include "M.h"
Z.h
----
#include <Windows.h>
#include <time.h>
#include "X.h"
AA.h
-----
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <Windows.h>
BB.txt
-------
No #includes
Rayne:

I would recommend

1. Including windows.h before other windows headers

2. Including windows headers before standard library headers

3. Using a precompiled header file, at least for the windows headers. The
precompiled header file should appear as the first #include in each .c file.
--
David Wilkinson
Visual C++ MVP
Giovanni Dicanio
2009-12-09 11:51:25 UTC
Permalink
Post by David Wilkinson
I would recommend
1. Including windows.h before other windows headers
David:

I tried with this:

#include <Windows.h>
#include <WinSock2.h>

and I had errors (VS2008 SP1 + Windows 7 SDK).

Instead this seems to work fine for me:

#include <WinSock2.h>
#include <Windows.h>

(Frankly speaking, my first thought was like yours, i.e. <windows.h> should
be the first Windows header in the inclusion list, but it seems to not work
correctly...)

Giovanni
Giovanni Dicanio
2009-12-09 11:44:27 UTC
Permalink
Post by Rayne
The entire project has 28 header files, and corresponding .c
files where applicable. I'm new to programming in Windows, and I'm
getting confused with all the different Windows header files that seem
to be causing these errors.
I would like to suggest you to try to use precompiled headers in this case.

You may want to add all the common standard include files required by your
project in StdAfx.h, e.g.:

// Add these in StdAfx.h :

#include <winsock2.h>
#include <ws2tcpip.h>
#include <tchar.h>
#include <windows.h>
#include <strsafe.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <vector>

...

And then in your source .cpp files you can add as first non-comment line:

#include "stdafx.h"

(and not explicitly include the standard header files e.g. <winsock2.h>,
etc. in .cpp source files.)


HTH,
Giovanni
Continue reading on narkive:
Loading...