Skip to content

Conversation

@mikomikotaishi
Copy link
Contributor

This pull request adds support for C++20 modules through CMake. It is enabled by the HTTPLIB_BUILD_MODULES option (which requires HTTPLIB_COMPILE to be enabled, though it probably doesn't have to - I only forced this requirement because it seems to make the most sense to force the library to compile if modules are to be compiled).

@Spixmaster
Copy link

Spixmaster commented Dec 8, 2025

Great work. I also thought about this.

The approach is very similiar to what was done to https://github.com/nlohmann/json with nlohmann/json#4799. Then, I noticed ...

Is this temporary approach with a file with using ... for the meantime while modules and no modules are supported?

This approach is not using modules natively but rather as an interface to the original way.

Does this method work without disadvantages?

@mikomikotaishi
Copy link
Contributor Author

mikomikotaishi commented Dec 8, 2025

This is the best way to my knowledge to support modules on top of a header-only or header/source library, allowing continued support for older versions while providing newer features as an option.

I'm not aware of any disadvantages to it besides a being additional translation unit to compile, but if I am wrong please correct me. The only glaring difference in API is that detail symbols are hidden as they are not exported, but in my opinion that's probably better not to expose detail symbols and flood IDE suggestions with implementation details.

@Spixmaster
Copy link

Spixmaster commented Dec 8, 2025

What about compiled libraries? Is it possible to have the traditional method and modules installed in parallel?

I am thinking of repositories that ship compiled .so or .a.

This is relevant here, https://aur.archlinux.org/packages/cpp-httplib-compiled .

@mikomikotaishi
Copy link
Contributor Author

mikomikotaishi commented Dec 8, 2025

Yes I believe it's possible to use shared/static libraries with modules, all of my modular projects compiled to shared libraries that an executable consumes

@yhirose
Copy link
Owner

yhirose commented Dec 8, 2025

@mikomikotaishi, thanks for the fine pull request! It's fantastic, but my concern is that someone needs to update modules/httplib.cppm whenever new external symbols are added or existing symbols are removed/renamed, and it could happen quite often. I am not planning to maintain this file. So if you cannot keep maintaining the file, I am probably not merge it since the file can be easily out-of-date. Is there a way to generate modules/httplib.cppm from httplib.h automatically?

@sum01 @jimmy-park @Tachi107 do you have any thought about this pull request?

@mikomikotaishi
Copy link
Contributor Author

mikomikotaishi commented Dec 8, 2025

I could create a Python script, or some other kinds of automated means of updating, which you could run every time it is updated. Until then I would be OK with maintaining this file, as it is a simple process. Such a script would probably comb through the file and add any symbols not part of a detail or internal namespace, or prefixed with an underscore, etc.

However, I am curious why it is not feasible to update the file manually. In case it isn't clear how, one can update the file by adding a using httplib::NEW_SYMBOL_HERE; into the export namespace httlib { } block, for each new symbol that is added. Do let me know if any more information is needed.

@mikomikotaishi
Copy link
Contributor Author

I have also seen some repositories use bots to push some commits too. Potentially one such bot could be set up to automatically populate the module with new changes each time there is a mismatch. I don't know anything about how to set this up, but I have seen this before and it could potentially be a solution (but I think the simplest one is just to run a Python script each time any update to the library happens).

@mikomikotaishi
Copy link
Contributor Author

Anyway, I think this could be one such way of automatically updating the module.

@yhirose
Copy link
Owner

yhirose commented Dec 9, 2025

@mikomikotaishi thanks for the additional explanation. I am ok with the following your suggestion.

I have also seen some repositories use bots to push some commits too. Potentially one such bot could be set up to automatically populate the module with new changes each time there is a mismatch. I don't know anything about how to set this up, but I have seen this before and it could potentially be a solution.

We could automatically generate modules/httplib.cppm in a GitHub Actions script when httplib.h is pushed. (I can't accept the way to run a script to update the file manually, since I don't maintain any build systems in this repository. Please see #955 (comment).)

@mikomikotaishi
Copy link
Contributor Author

OK, that makes sense to me. (I don't know anything about how to run GitHub Actions or write scripts for it however, so I'm afraid the most I can do is create a script for this.)

@mikomikotaishi
Copy link
Contributor Author

I'm not sure why there were failing workflows as I didn't change anything in the core library

@mikomikotaishi
Copy link
Contributor Author

Never mind, it seems the failing CI is happening upstream too.

@Tachi107
Copy link
Contributor

Tachi107 commented Dec 9, 2025 via email

@mikomikotaishi
Copy link
Contributor Author

mikomikotaishi commented Dec 9, 2025

@Tachi107 CMake needs to know what the output directory is ahead of time to compile the module. How do you propose to solve this?

@mikomikotaishi
Copy link
Contributor Author

@yhirose I think there is one possible solution to allow both the directory to be user-specified while still supporting CMake module building, which is probably just to have the Python script generate the CMake file too. I don't know if this is too convoluted or awkward of a design though, so please do tell me your thoughts.

@Tachi107
Copy link
Contributor

Tachi107 commented Dec 11, 2025 via email

Copy link
Contributor

@Tachi107 Tachi107 left a comment

Choose a reason for hiding this comment

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

Some feedback now that I have been able to look at the whole code :)

@sum01
Copy link
Contributor

sum01 commented Dec 14, 2025

Is there a reason to try and add module support when the library itself is C++11?

Not to mention, Cmake v3.28 adds built-in module support, which isn't being used either.

@Spixmaster
Copy link

Spixmaster commented Dec 14, 2025

This library being C++11 does not require programmes depending on it being C++11.

I support the modules.

@mikomikotaishi
Copy link
Contributor Author

Indeed, some people who use C++20 and onward may wish to consume the library with the module rather than the header. This is precisely what modules are designed to improve for compilations, as re-including the same large header in multiple locations massively increases build times whereas modules are built only once and then imported/linked.

@sum01
Copy link
Contributor

sum01 commented Dec 14, 2025

I understand you can use older standards on newer ones, but I mean trying to add support where it isn't "supported" seems unnecessary.

As for having to re-compile, why not use pre-compiled headers (Cmake v3.16 feature), or the compiled version of this library?

@mikomikotaishi
Copy link
Contributor Author

mikomikotaishi commented Dec 14, 2025

Precompiled headers are not a standard language feature, they are just a compiler trick. Modules provide a very specific API and very specific features for library encapsulation. For example, we may want to avoid including httplib's macros or avoid adding detail/implementation symbols into scope, which overwhelms autocomplete on IDEs with irrelevant symbols.

Even splitting the library into a header/source division still leaves a very large header that gets recompiled each time.

As for the question of "support", this specific wrapper style (a module which includes the header and then re-exports the symbols) is how most libraries built with C++11/14/17 provide module support (such as Boost, Poco, etc.) This is the simplest way to do it to my knowledge, and it's not a hastily tacked-on hack. You can see in other libraries this is how originally header-libraries are wrapped for modules.

@mikomikotaishi
Copy link
Contributor Author

Hi, sorry for the silence on this issue. I've been busy with an exam, but I'll return to this and send a solution today or tomorrow.

@mikomikotaishi
Copy link
Contributor Author

@yhirose Hi, I am sorry but I am completely lost as to how to actually generate a module correctly from the header. I was considering using libclang but I don't want to introduce a new dependency for a build script.

@mikomikotaishi
Copy link
Contributor Author

I personally would rather manually maintain the module file for the time being, but if someone more skilled at parsing C++ wants to take a crack at this please feel free.

@yhirose
Copy link
Owner

yhirose commented Dec 29, 2025

@mikomikotaishi Thanks for your willingness. However, I am now thinking of closing this pull request for the maintenance reason I commented on before. I would like to confirm that you will commit to updating modules/httplib.cppm whenever new external symbols are added or existing symbols are removed/renamed by contributors. I am not going to wait for this update to happen. This update must be done by you before I release a new version. Otherwise, httplib.h will be shipped with an incompatible, out-of-date httplib.cppm. If you accept this responsibility from now on, I will reconsider it.

@yhirose yhirose requested review from yhirose and removed request for Tachi107 and yhirose January 12, 2026 04:56
@yhirose
Copy link
Owner

yhirose commented Jan 14, 2026

@mikomikotaishi could you test it on three platforms (macos, ubuntu, Windows)?

@mikomikotaishi
Copy link
Contributor Author

@mikomikotaishi could you test it on three platforms (macos, ubuntu, Windows)?

Things are building correctly for me and I'm able to use the module on MSVC, GCC and Clang, so I think things are good

@yhirose
Copy link
Owner

yhirose commented Jan 14, 2026

Could you show me how you compile it with clang++, g++, and vc++? I'll try on my machines when I have time.

@mikomikotaishi
Copy link
Contributor Author

mikomikotaishi commented Jan 14, 2026

I'm relying on CMake to build it, because compiling libraries by manually calling the compiler rather than including it by header or using a build system is difficult and something I have little experience with. If you are interested in what I did:

cmake -B build -S . -G Ninja -DHTTPLIB_COMPILE=ON -DHTTPLIB_BUILD_MODULES=ON

This runs the split.py script and creates the files httplib.h, httplib.cc, and httplib.cppm. (Could we replace .cc with .cpp so that it aligns with .cppm? I'll leave this decision up to you, I personally prefer .cpp for the aforementioned reason but it's nothing dealbreaking, I don't particularly mind either way.) It configures to use Ninja as the build system, and indicates to the build system we wish to use modules. Of course, the compiler must support modules; see arewemodulesyet/tools for versions.

cmake --build build

This performs the actual build. All of the compiler configuration is handled by the generated build system files.

Copy link
Contributor

@Tachi107 Tachi107 left a comment

Choose a reason for hiding this comment

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

Some more comments :)

@Tachi107
Copy link
Contributor

(Could we replace .cc with .cpp so that it aligns with .cppm? I'll leave this decision up to you, I personally prefer .cpp for the aforementioned reason but it's nothing dealbreaking, I don't particularly mind either way.)

This would create issues for people who have hardcoded .cc in their build scripts, without any really advantage apart from a cosmetic difference :/

@Tachi107
Copy link
Contributor

Tachi107 commented Jan 15, 2026 via email

@Tachi107
Copy link
Contributor

Tachi107 commented Jan 15, 2026 via email

@mikomikotaishi
Copy link
Contributor Author

OK, I'll instead put it behind an include call

@mikomikotaishi
Copy link
Contributor Author

@yhirose Anything else in need of addressing?

@Tachi107
Copy link
Contributor

OK, I'll instead put it behind an include call

I think I haven't been understood :/

Please look at the documentation of cmake_policy and cmake_minimum_required to get what I meant :)

@Spixmaster
Copy link

Spixmaster commented Jan 21, 2026

Cannot you (@Tachi107) just give a diff instead of letting him guess what you want? This is not constructive.

@mikomikotaishi, is very responsive and puts a lot of effort into it for more then five weeks.

@Tachi107
Copy link
Contributor

Tachi107 commented Jan 21, 2026 via email

@mikomikotaishi
Copy link
Contributor Author

Hi, I'm sorry but I don't have very much knowledge of CMake and its features or how to correctly apply the desired solution. I would have thought an include would be sufficient to prevent version incompatibilities, but if not a diff would be highly appreciated, I am willing to take suggestions of anyone more familiar with CMake than I am.

@sum01
Copy link
Contributor

sum01 commented Jan 28, 2026

I'd guess he was talking about adding cmake_minimum_required(3.28) into the modules file and setting CMP0155 to the new policy?

@Tachi107
Copy link
Contributor

Tachi107 commented Jan 28, 2026 via email

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.

6 participants