-1

Any time I try to use more than 1 networking library in the same (CMake) project, there are many Winsock redefinition errors. These libraries are asio, SteamAPI, libssh, and SDL_net to name a few.

redefinition errors

I turned to Google for answers, and there are many posts regarding this issue. Many people have proposed how to fix this such as defining WIN32_LEAN_AND_MEAN before including Windows.h, or not including Windows.h twice... I have removed all usages of Windows.h. I have also tried many variations of the answers in the hope of getting my program to compile. This makes no difference.

Among all of the available answers, there is not one single answer that solves this issue. Most of the answers do point to this being a WindowsAPI-only issue due to it trying to include the old Winsock.h.

Is there any way to use the networking libraries mentioned above simultaneously without these errors?

crazicrafter1
  • 309
  • 5
  • 18

2 Answers2

0

winsock.h does not co-exist with winsock2.h. winsock2.h is designed to replace winsock.h, not extend it. As such, winsock2.h declares everything that winsock.h also declares.

If winsock2.h is included first then it disables winsock.h and all is well, but if winsock.h is included first then you get these kinds of redeclaration errors.

So, the solution is to make sure that winsock2.h is included before winsock.h.

For instance, by including winsock2.h before windows.h (which includes winsock.h) and all other socket libraries. You can also define WIN32_LEAN_AND_MEAN so windows.h doesn't include winsock.h.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • I am not sure if my project has an issue in it, because even when I try to add WIN32_LEAN_AND_MEAN as a compile definition in the CMake, its the same issue (asio now complains that winsock.h was already included). It's hard for me to accept this answer when I keep running into the same issue. If my solution is the only one that works, I can only assume it might be for others, for now at least. Here is the CMakeLists.txt, including stuff I've commented when it failed https://github.com/PeriodicSeizures/Valhalla/blob/steam-server/CMakeLists.txt. I don't know what else to do at this point. – crazicrafter1 Nov 01 '22 at 13:50
  • 1
    @crazicrafter1 you do not need to alter SDK headers to solve this problem. – Remy Lebeau Nov 01 '22 at 14:55
  • @crazicrafter1 Try compiling without CMake. Once that works, translate that back to CMake. – IInspectable Nov 02 '22 at 06:31
-3

I believe I have discovered a solution to this problem. It involves modifying Windows.h located in C:\Program Files (x86)\Windows Kits\10\Include\<version>\um.

This works because all of the gunk that Windows.h includes is located within the #ifndef section for WIN32_LEAN_AND_MEAN, including Winsock.h.

Using a text editor, remove the #ifndef WIN32_LEAN_AND_MEAN section. It should be about 21 lines in length. Once removed, add `#include <Winsock2.h> to where the section was. Your computer might ask for permissions to save the file.

This worked for me, and I cannot assure it will work for you, but it very much should. Hope this helps.

crazicrafter1
  • 309
  • 5
  • 18
  • 2
    Or, just define `WIN32_LEAN_AND_MEAN` in your code and then add `#include `... – Arush Agarampur Nov 01 '22 at 03:13
  • 1
    @crazicrafter1 - Arush Agarampur is correct. And hacking windows.h is definitely a Bad Idea. Please consider DELETING this answer, and upvoting and "accepting" Remy Lebeau's answer instead. – paulsm4 Nov 01 '22 at 04:00
  • 2
    Modifying *any* Windows SDK assets is not a solution to any problem. [Using the Windows Headers](https://learn.microsoft.com/en-us/windows/win32/winprog/using-the-windows-headers) explains how to use the header files. – IInspectable Nov 01 '22 at 06:34