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.
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?
CodePudding user response:
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
.
CodePudding user response:
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.