6

I use Visual Studio 2012 and trying to get static library UDT get to work. Unfortunately, i cannot compile project that links UDT lib to itself, i get 159 strange errors about type or macro redefinitions in windows SDK headers.

c:\program files\windows kits\8.0\include\shared\ws2def.h(96): warning C4005: 'AF_IPX' : macro redefinition 2> c:\program files\windows kits\8.0\include\um\winsock.h(452) : see previous definition of 'AF_IPX' 2>c:\program files\windows kits\8.0\include\shared\ws2def.h(136): warning C4005: 'AF_MAX' : macro redefinition 2> c:\program files\windows kits\8.0\include\um\winsock.h(471) : see previous definition of 'AF_MAX' 2>c:\program files\windows kits\8.0\include\shared\ws2def.h(173): warning C4005: 'SO_DONTLINGER' : macro redefinition 2> c:\program files\windows kits\8.0\include\um\winsock.h(394) : see previous definition of 'SO_DONTLINGER' 2>c:\program files\windows kits\8.0\include\shared\ws2def.h(217): error C2011: 'sockaddr' : 'struct' type redefinition 2> c:\program files\windows kits\8.0\include\um\winsock.h(477) : see declaration of 'sockaddr'

Previously removed some code which was defining dllexport/dllimport for each UDT function:

#define UDT_API // no dllimport/export

I checked that library always use #ifdef to check if file is already included. And this error:

windows kits\8.0\include\um\ws2tcpip.h(703): error C3861: 'WSASetLastError': identifier not found

May reason be const keyword in that expression (somewhere inside udt):

UDT_API int bind(UDTSOCKET u, const struct sockaddr* name, int namelen);

What iam missing?

Croll
  • 3,631
  • 6
  • 30
  • 63
  • What can we do without a minimum code that reproduce the problem? – Jepessen Jul 15 '15 at 15:34
  • @Jepessen there is not code, i just cannot build project if it does `#include "udt.h"`. You can follow the link, download archive, untar, open in visual studio and change `udt` build type to `static library`.Then remove `dllimport/dllexport` expressions (there two, can be easily found) and should be just `#define UDT_API`. Then link udt to other empty project and do `include "udt.h"` in it.... That is what i did – Croll Jul 15 '15 at 15:37
  • 3
    I remember something about defining `WIN32_LEAN_AND_MEAN` before including the `windows.h` as it already includes `winsock` and there seems to be problems otherwise. Is this such a scenario? I don't know at the moment hence this is a comment and no answer... – PuerNoctis Jul 15 '15 at 15:39
  • @PuerNoctis you've pointed me to a right direction, now i can compile both my projects, but i cannot write UDT code because i cannot include winsock stuff to my code. If i type `#include ` or `#include ` or `#include `, i get same erros – Croll Jul 15 '15 at 15:49
  • Windows is notorious for being picky about the order of include files. Bad design in my opinion. – dbush Jul 15 '15 at 15:55

2 Answers2

12

If you include winsock.h and winsock2.h make sure that you include winsock2.h first. If the includes are not so obvious you can check cpp files in question if you compile them with /P and walk through the generated preprocessor output file.

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
  • Have just read http://stackoverflow.com/questions/22517036/socket-errors-cant-get-functions-in-winsock2-h and no, that does not help..If i try to include any winsock header, i get 159 errors (like in linked question) – Croll Jul 15 '15 at 15:52
  • Did you check with `/P`? – Werner Henze Jul 15 '15 at 15:54
  • UDT or my project? Where and *what* should i watch after build with /P? – Croll Jul 15 '15 at 15:57
  • Use `/P` for one (any you like) of the files that can't compile. – Werner Henze Jul 15 '15 at 16:03
  • I just get this stupid another error. http://pastebin.com/6V5RdjQV I do not understand what does it mean. – Croll Jul 15 '15 at 16:14
  • That's not so bad. This means that the linker can't find the Output file. This is expected, as `/P` only write the preprocessed file but does not compile. Now look into the file that was written by the Compiler (file.i). – Werner Henze Jul 15 '15 at 16:26
  • 1
    Sharing my method that solved my issue: 1. turn on #include hierarchy(https://stackoverflow.com/questions/1137966/displaying-the-include-hierarchy-for-a-c-file-in-visual-studio#:~:text=It's%20under%20Project%20Settings%20%2D%3E%20Configuration,%3E%20Advanced%20%2D%3E%20Show%20Includes.). 2. reorder the include to resolve the issue: My issue is due to winsoc2.h included after windows.h(https://www.sysnet.pe.kr/2/0/12438) – run run chicken Oct 07 '21 at 01:53
2

Just to describe what problem was... Seems like i shouldnt include udt.h before any winsock headers. That's the fix:

 #ifndef WIN32
   #include <unistd.h>
   #include <cstdlib>
   #include <cstring>
   #include <netdb.h>
   #else
   #define WIN32_LEAN_AND_MEAN
    #include <WinSock2.h>
    #include <WS2tcpip.h>
    #include <Windows.h>
   #endif

   #include <udt.h>
   #include <singleton.h>
   #include <excassert.h>
   #include <SharedUtility.h>

   #ifndef WIN32
   void* recvdata(void*);
   #else
   DWORD WINAPI recvdata(LPVOID);
   #endif
Croll
  • 3,631
  • 6
  • 30
  • 63