saprykin / plibsys

Highly portable C system library: threads and synchronization primitives, sockets (TCP, UDP, SCTP), IPv4 and IPv6, IPC, hash functions (MD5, SHA-1, SHA-2, SHA-3, GOST), binary trees (RB, AVL) and more. Native code performance.
MIT License
684 stars 75 forks source link

Project builds with visual studio but not ninja and MinGW : error: 'ERROR_INVALID_HANDLE' undeclared (first use in this function) #103

Closed BillyTheSquid21 closed 9 months ago

BillyTheSquid21 commented 9 months ago

After cloning the repo, I call the following commands in windows from the repo directory:

  1. mkdir build && cd build
  2. cmake .. -DCMAKE_CXX_COMPILER=gcc -G "Ninja"
  3. cmake --build . -j10

The first two steps work fine, however the build step produces this error:

[1/137] Building C object src/CMakeFiles/plibsys.dir/perror.c.obj
FAILED: src/CMakeFiles/plibsys.dir/perror.c.obj
C:\MinGW\bin\gcc.exe -DPLIBSYS_COMPILATION -DPLIBSYS_HAS_LLDIV -DPLIBSYS_HAS_SOCKADDR_STORAGE -DPLIBSYS_HAS_SOCKLEN_T -DPLIBSYS_SOCKADDR_IN6_HAS_FLOWINFO -DPLIBSYS_SOCKADDR_IN6_HAS_SCOPEID -Dplibsys_EXPORTS -ID:/Repositories/plibsys/src -ID:/Repositories/plibsys/build/src -g -MD -MT src/CMakeFiles/plibsys.dir/perror.c.obj -MF src\CMakeFiles\plibsys.dir\perror.c.obj.d -o src/CMakeFiles/plibsys.dir/perror.c.obj -c D:/Repositories/plibsys/src/perror.c
In file included from D:/Repositories/plibsys/build/src/plibsysconfig.h:29:0,
                 from D:/Repositories/plibsys/src/ptypes.h:67,
                 from D:/Repositories/plibsys/src/perror.h:62,
                 from D:/Repositories/plibsys/src/perror.c:26:
D:/Repositories/plibsys/src/perror.c: In function 'p_error_get_io_from_system':
D:/Repositories/plibsys/src/perror.c:66:7: error: 'ERROR_INVALID_HANDLE' undeclared (first use in this function)
  case WSA_INVALID_HANDLE:
       ^
D:/Repositories/plibsys/src/perror.c:66:7: note: each undeclared identifier is reported only once for each function it appears in
D:/Repositories/plibsys/src/perror.c:70:7: error: 'ERROR_INVALID_PARAMETER' undeclared (first use in this function)
  case WSA_INVALID_PARAMETER:
       ^
[10/137] Generating API documentation with Doxygen
ninja: build stopped: subcommand failed.

I haven't had a problem building in visual studio, so I'm unsure as to what is wrong here.

saprykin commented 9 months ago

Can you please provide full output from Step 2? You can paste it through GitHub Gist. As building on Windows with GCC/Clang, in general, is a bit tricky, the following info would be helpful:

BillyTheSquid21 commented 9 months ago

Here is the output to step 2

saprykin commented 9 months ago

Okay, based on the info I assume that you are using MinGW (32-bit), which was installed probably using mingw-get tool. Is this correct? At least with this version I was able to reproduce the problem. However, this version of MinGW is 10 years old, and, moreover, MinGW project is not in development anymore. Basically, the project is dead and outdated. The reason the compilation fails is due to the bugs in the header files supplied by MinGW. You can read a detailed problem explanation here. In short, the compiler does not properly include definitions of Windows error codes. You can potentially hack the headers, but there is no guarantee that you would not break something else.

I cannot fix this broken version of the compiler, but I can recommend you to switch to MinGW-w64 project which provides old and modern versions of GCC for Windows, both 32-bit and 64-bit builds. For example, if you still need GCC 6.3.0 (32-bit build), you can download a precompiled package here. This version works without any issues for me using the steps you have provided.

Another way to obtain a modern GCC (basically, MinGW-w64 builds) version on Windows is to use MSYS2 distribution which comes with a nice package manager. You can read about it also at the local Wiki.

BillyTheSquid21 commented 9 months ago

Thank you! Switching to MinGW-w64 fixed the issue, I hadn't realised how old that MinGW version was