Skip to content

Fix mingw-w64 building #2198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Conversation

apocelipes
Copy link

Since v0.23.1 cpp-httplib can not be complied with mingw-w64. The error message looks like this:

> D:\a\_temp\msys64\mingw64\bin\x86_64-w64-mingw32-g++ -c -m64 -std=c++11 -IC:\Users\runneradmin\AppData\Local\.xmake\packages\c\cpp-httplib\v0.25.0\cd2fddbf26ef4285ab9708e0209f52e0\include -o D:\a\_temp\msys64\tmp\.xmake\250808\_97BBC115DC5B437086AEA0BA215614B0.o D:\a\_temp\msys64\tmp\.xmake\250808\_7D0C25F8195246278D1C039D74425C23.cpp
> checking for c++ includes(httplib.h)
> checking for c++ snippet(test)
checkinfo: ...amdir\core\sandbox\modules\import\core\tool\compiler.lua:84: @programdir\modules\core\tools\gcc.lua:1035: In file included from D:\a\_temp\msys64\tmp\.xmake\250808\_7D0C25F8195246278D1C039D74425C23.cpp:2:
C:\Users\runneradmin\AppData\Local\.xmake\packages\c\cpp-httplib\v0.25.0\cd2fddbf26ef4285ab9708e0209f52e0\include/httplib.h: In member function 'bool httplib::detail::mmap::open(const char*)':
C:\Users\runneradmin\AppData\Local\.xmake\packages\c\cpp-httplib\v0.25.0\cd2fddbf26ef4285ab9708e0209f52e0\include/httplib.h:3081:14: error: '::CreateFile2' has not been declared; did you mean 'CreateFileW'?
 3081 |   hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ,
      |              ^~~~~~~~~~~
      |              CreateFileW
image

You can find the whole CI log here: link

The function ::CreateFile2 needs _WIN32_WINNT >= 0x0602, however mingw-w64 in our CI cannot detect the correct value of it and set the value to 0x0600 and 0x0502, that makes the code cannot use CreateFile2.

Because cpp-httplib requires Windows 10 or newer, let's just set the _WIN32_WINNT to _WIN32_WINNT_WIN10 if we are using mingw-w64. Now mingw-w64 should compile the code.

Copy link
Contributor

@thbeu thbeu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I consider this a kludge. Any chance to define/redefine _WIN32_WINNT to the appropriate value outside? Otherwise what about a macro HAVE_CREATEFILE2?

@apocelipes
Copy link
Author

apocelipes commented Aug 8, 2025

I consider this a kludge. Any chance to define/redefine _WIN32_WINNT to the appropriate value outside? Otherwise what about a macro HAVE_CREATEFILE2?

What can we do if HAVE_CREATEFILE2 was false? I think fall back to CreateFileW is acceptable.

Code could be like:

#if defined(HAVE_CREATEFILE2)
  hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ,
                         OPEN_EXISTING, NULL);
#else
  hFile_ = ::CreateFileW(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL,
                         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
#endif

Edited: CreateFileW has different arguments, fixed.

@apocelipes
Copy link
Author

The code has been updated. I add the macro HAVE_CREATEFILE2.

@apocelipes apocelipes force-pushed the fix-mingw64 branch 2 times, most recently from 47075dd to 86adf1f Compare August 8, 2025 10:00
yhirose added a commit that referenced this pull request Aug 9, 2025
@yhirose
Copy link
Owner

yhirose commented Aug 9, 2025

@apocelipes thanks for the pull request. However, I don't prefer seeing __MINGW32__ and introducing another macro for this purpose. I made a different pull request to fix this issue at #2203. Could you please try it to see if it works on your machine, since I don't have the mingw-w64 environment? Thanks!

@yhirose
Copy link
Owner

yhirose commented Aug 9, 2025

Or, I am thinking to use only CreateFileW in httplib.h. :)

@apocelipes
Copy link
Author

apocelipes commented Aug 9, 2025

@yhirose Thanks for your review.

I think use CreateFile2 is fine because it is a modern and safer API.

The code can be simplified and no need to introduce a new macro. You could take a look at the new code:

#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
  hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ,
                         OPEN_EXISTING, NULL);
#else
  hFile_ = ::CreateFileW(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL,
                         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
#endif

This is more simple than use std::once_flag and no runtime performance cost.

@thbeu
Copy link
Contributor

thbeu commented Aug 9, 2025

We already have the _WIN32_WINNT version check in

cpp-httplib/httplib.h

Lines 29 to 34 in 3f44c80

#ifdef _WIN32
#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0A00
#error \
"cpp-httplib doesn't support Windows 8 or lower. Please use Windows 10 or later."
#endif
#endif

CreateFileW will not be used then.

@apocelipes
Copy link
Author

So, the _WIN32_WINNT is greater than _WIN32_WINNT_WIN8 but CreateFile2 does not exist. It is toolchain's bug.

I can add a cmake check to set the macro HAVE_CREATEFILE2 but maybe it's not worth for it.

@apocelipes
Copy link
Author

A cmake compile check has been added. PTAL.

@apocelipes
Copy link
Author

I upgraded the toolchain and it fixed. I think this PR is no longer needed. Thank you for your advice.

@apocelipes apocelipes closed this Aug 9, 2025
@apocelipes apocelipes deleted the fix-mingw64 branch August 9, 2025 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants