据我所知,翻译单元在编译阶段彼此之间没有任何关系。你能解释一下为什么我会出现这种奇怪的行为:在一个 A.cpp 中包含 windows.h 之后(实际上不使用套接字 api 并且不包含 winsock2.h)我在编译另一个 B.cpp 时出错winsock2.h winsock.h 冲突。这个 B.cpp 根本没有改变。提前致谢。
编辑:这实际上是一个编译而不是链接错误。我已将 msvc 编译器置于详细模式,此处标头包含树:
2>perfomancemeter.cpp
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\numeric
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\iterator
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\xutility
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\climits
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\yvals.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\crtdefs.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\sal.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\crtassem.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\vadefs.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\use_ansi.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\limits.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\crtdefs.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\utility
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\iosfwd
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\cstdio
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\stdio.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\crtdefs.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\swprintf.inl
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\cstring
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\string.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\crtdefs.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\cwchar
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\wchar.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\crtdefs.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\wtime.inl
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\xstddef
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\cstddef
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\stddef.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\crtdefs.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\crtdbg.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\crtdefs.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\algorithm
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\memory
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\xmemory
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\cstdlib
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\stdlib.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\crtdefs.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\exception
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\eh.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\crtdefs.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\malloc.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\crtdefs.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\new
2>Note: including file: d:\repos\cbs\billing\common\perfomancemeter.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\vector
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\stdexcept
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\xstring
2>Note: including file: d:\repos\cbs\billing\common\util.h
2>Note: including file: d:\repos\cbs\billing\common\framework\include\types/types.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\string
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\istream
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\ostream
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\ios
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\xlocnum
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\streambuf
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\xiosbase
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\xlocale
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\typeinfo
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\xdebug
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\xlocinfo
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\xlocinfo.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\ctype.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\crtdefs.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\locale.h
2>Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\crtdefs.h
1>c:\program files (x86)\microsoft visual studio 8\vc\platformsdk\include\winsock2.h(112) : error C2011: 'fd_set' : 'struct' type redefinition
1> c:\program files (x86)\microsoft visual studio 8\vc\platformsdk\include\winsock.h(54) : see declaration of 'fd_set'
我之前遇到过这个问题,并且知道在包含 Windows 标头之前 #define WIN32_LEAN_AND_MEAN 可以修复它。但是为什么会在这种情况下发生呢?