Visual C++ Tips

How to find where header files are included

  • 问题场景
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\shared\ws2def.h(103): warning C4005: 'AF_IPX': macro redefinition
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\winsock.h(457): note: see previous definition of 'AF_IPX'
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\shared\ws2def.h(147): warning C4005: 'AF_MAX': macro redefinition
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\winsock.h(476): note: see previous definition of 'AF_MAX'
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\shared\ws2def.h(185): warning C4005: 'SO_DONTLINGER': macro redefinition
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\winsock.h(399): note: see previous definition of 'SO_DONTLINGER'
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\shared\ws2def.h(235): error C2011: 'sockaddr': 'struct' type redefinition
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\winsock.h(1007): note: see declaration of 'sockaddr'
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\shared\ws2def.h(437): error C2059: syntax error: 'constant'
......
  • 解决方法

设置编译器

为了能找到winsock.h在何处被引入的,我们需要编译器输出对于文件引入的信息,而这个功能在Visual C++是有提供的,具体位置在Project->Property->C++->Advanced->Show Includes

编译输出

1>stdafx.cpp
1>Note: including file: c:\workspace\projects\replay\stdafx.h
1>Note: including file:  C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\atlmfc\include\atlbase.h
1>Note: including file:   C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\atlmfc\include\atldef.h
1>Note: including file:    C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\atlmfc\include\atlrc.h
1>Note: including file:    C:\Program Files (x86)\Windows Kits\10\Include\10.0.15063.0\ucrt\errno.h
1>Note: including file:     C:\Program Files (x86)\Windows Kits\10\Include\10.0.15063.0\ucrt\corecrt.h
1>Note: including file:      C:\Program Files (x86)\Microsoft Visual 
......