Skip to content

HttpLib unable to compile using MSVC, when integrated with C++ 20 Modules #2087

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

Open
Rabbit0w0 opened this issue Mar 5, 2025 · 17 comments
Open

Comments

@Rabbit0w0
Copy link

Rabbit0w0 commented Mar 5, 2025

StackOverflow: https://stackoverflow.com/questions/79478667/msvc-unable-to-compile-program-with-c20-module-feature

MS Dev Community: https://developercommunity.visualstudio.com/t/MSVC-confused-with-operators-when-using/10861598

I'm assuming that if this is a compiler bug or sth else related to MSVC's nature, but Clang did compile my work. Though it is likely that HttpLib has nothing to do with this, can you roll out a temporary patch for MSVC?

@falbrechtskirchinger
Copy link
Contributor

Please provide a minimal example to reproduce on Compiler Explorer.
You can use the template "C++ CMake" (click Templates in the top left) and #include <https://raw.githubusercontent.com/yhirose/cpp-httplib/master/httplib.h> to include the library. Thanks!

@Rabbit0w0
Copy link
Author

Rabbit0w0 commented Mar 5, 2025

GodBolt is having trouble compiling with MSVC/CMake: compiler-explorer/compiler-explorer#6742

MRE here https://godbolt.org/z/Pborbb5nd, tho not cmake is not configuring

@falbrechtskirchinger
Copy link
Contributor

GodBolt is having trouble compiling with MSVC/CMake: compiler-explorer/compiler-explorer#6742

That's unfortunate. Sorry about that!

MRE here godbolt.org/z/Pborbb5nd, tho not cmake is not configuring

I don't have access to MSVC otherwise, but after taking another look at the error in your linked posts, I'm not confident there's anything we can do about it anyway.

@falbrechtskirchinger
Copy link
Contributor

I looked at the relevant code in microsoft/STL:

https://github.com/microsoft/STL/blob/0d8f517ae3828284fed594741b847db940167a59/stl/inc/thread#L166-L180

Specifically, decltype(_Now + _Rel_time) .

If I had access to MSVC, I'd see what happens with that function in the global module fragment (and some code that uses it).

@Rabbit0w0
Copy link
Author

Rabbit0w0 commented Mar 6, 2025

If you are willing to tell your email or send me an email at [email protected], I'm more than willing to set up a free cloud Windows instance (with proper license linked with HyperV in certain regions) for you

@falbrechtskirchinger
Copy link
Contributor

If you are willing to tell your email or send me an email at [email protected], I'm more than willing to set up a free cloud Windows instance (with proper license linked with HyperV in certain regions) for you

Awesome, that would certainly help! E-mail me at [email protected]. Not that latency is a huge concern, but since you mentioned regions, my preferred data center locations are Frankfurt, DE, or Amsterdam, NL. Thanks!

@Rabbit0w0
Copy link
Author

Sure, this process will take up to 3 business days (I have to go through a reimbursement process :) ), and I will notify you directly through email once it is done. Thanks in advance for investigating this problem.

@Rabbit0w0
Copy link
Author

Just checking if you received my mail. I received no response and no one else connected to the VM instance. If you haven't seen my email, please check the junk box :)

@falbrechtskirchinger
Copy link
Contributor

falbrechtskirchinger commented Mar 8, 2025

Thanks for checking in; you did end up in the spam folder. I'll test my theory tomorrow. If that doesn't reveal anything, I'll see if I can think of something else next week. Maybe I can figure out how to run C-Reduce/C-Vise on Windows.

@falbrechtskirchinger
Copy link
Contributor

Small update: My test code built without issues, so there's some odd interaction between modules, the STL, and httplib going on. I'm now building C-Vise, which should take some time (maybe 24h?) on a single CPU system. I'll keep you posted.

@Rabbit0w0
Copy link
Author

Update: Found that MSVC cannot compile as well if OpenSSL is enabled, as it cannot find a function defined by macro. (I tried to comment out all sleep_for calls to see if it compiles)

H:\vcpkg\installed\x64-windows-static\include\openssl\x509.h(390): error C2129: static function 'OPENSSL_sk_freefunc ossl_check_X509_INFO_freefunc_type(sk_X509_INFO_freefunc)' declared but not defined
H:\vcpkg\installed\x64-windows-static\include\openssl\x509.h(390): note: see declaration of 'ossl_check_X509_INFO_freefunc_type'

It seems that both issues could be related to a bug in MSVC, where the compiler fails to resolve definitions under certain circumstances. (For thread, MSVC failed to find the operator; and for OpenSSL, MSVC then failed to resolved the function)

@falbrechtskirchinger
Copy link
Contributor

That makes things more complicated, of course. We can work around MSVC issues in httplib.h, but OpenSSL is another story.

Meanwhile, C-Vise is built, and when I find some spare time, I'll write a test case for the reduction.

@falbrechtskirchinger
Copy link
Contributor

Update: I couldn't get C-Vise to run correctly on Windows, so I'm running C-Vise locally and delegating the compilation to the VM. I asked Grok to write a client/server for me to do that, which worked well. It's been running for about 20 minutes, and we're down from 8250 lines to 7228 lines of code while still triggering the compiler error. Let's see where this goes…

@falbrechtskirchinger
Copy link
Contributor

falbrechtskirchinger commented Apr 4, 2025

Sorry for my long absence. Three weeks ago, I made some planned hardware upgrades and expected to be offline for a few days at most. Things didn't go well.

Ultimately, C-Vise failed to reduce the code sufficiently in a reasonable time frame. I aborted the first attempt after 6 hours (had to power off for the upgrades) and noticed that the clang_delta reduction steps failed to run. For my second attempt, I fixed the clang_delta issue, but this resulted in a less than 1% reduction in code size with over 10000 compilations (over 9 hours), after which I had to abort again.

My manual attempts to reproduce the issue also weren't fruitful. Since there's an additional problem not related to <thread> and sleep_for() in OpenSSL, I cannot think of a viable workaround for what is clearly a compiler bug. Microsoft will have to fix it. One thing I didn't explore is the Clang frontend for MSVC. Does the Clang frontend do more than AST generation?

@Rabbit0w0 Thanks for the VM! Sorry, it didn't help. 🤷‍♂

@Rabbit0w0
Copy link
Author

Thanks for your generous help and I'm sorry to hear that things weren't going well. My temporary fix is to refactor all module-based classes in my project back to traditional headers. Hope Microsoft will improve their support for modern C++ features soon.

And for anyone who is experiencing the same issue:
Shifting to another compiler works. But if your project heavily relies on MSVC, then consider using headers instead of modules. And please vote up for my thread on MS Developer Community.

@yhirose
Copy link
Owner

yhirose commented Apr 5, 2025

@Rabbit0w0, @falbrechtskirchinger thank you for all the investigations for this issue! I'll leave this issue as 'information', so that others can benefit from it.

@daanforever
Copy link

daanforever commented Apr 9, 2025

Internal compiler error:

module;
#include "httplib.h"
export module test;
export namespace test {
  class Server {
    httplib::Server server_;
  public:
    void start();
  };

  void Server::start() {
    server_.listen("0.0.0.0", 8080);
  };
}

src\httplibtest.cppm(15): fatal error C1001: Internal compiler error.
(compiler file 'msc1.cpp', line 1533)
...
INTERNAL COMPILER ERROR in 'D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\bin\Hostx64\x64\cl.exe'

Solution:

module;
#include "httplib.h"
export module test;
export namespace test {
  class Server {
  public:
    void start();
  };

  void Server::start() {
    httplib::Server server_;
    server_.listen("0.0.0.0", 8080);
  };
}

Hope this helps someone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants