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 :)

@yhirose
Copy link
Owner

yhirose commented Dec 30, 2025

If the following simple approach works, I’m fine with it. I don’t mind exposing the httplib::detail::* symbols, since #including httplib.h exposes the detail functions anyway. Could you please make the change? After that, I’ll take a look at it for review.

module;

// include all included headers seen in <httplib.h>,
// to prevent them from being re-included in the export block
#include <...>

export module httplib;

export {
    #include "../httplib.h"
}

Also, please don’t forget to remove any files that are no longer needed, revert the changes in README.md, and add any necessary comments only to the CMake files. (I intentionally avoid mentioning any build systems in the README, as they are optional and not officially supported by cpp-httplib itself, but are kindly maintained by excellent contributors. Thanks for your understanding.)

@mikomikotaishi
Copy link
Contributor Author

mikomikotaishi commented Dec 30, 2025

I did make this change, but we have to re-open the PR for it to appear here. I will update the README as requested.

@mikomikotaishi
Copy link
Contributor Author

I've removed the changes to the README, but I did notice that the README mentions how to use the script to split the program into .h/.cc file, where would it be appropriate to document that the library contains support for modules features?

@mikomikotaishi
Copy link
Contributor Author

mikomikotaishi commented Dec 30, 2025

What if we put all detail namespaces inside an #ifndef HTTPLIB_MODULE_HIDE_DETAILS in the header, and defined that macro at the top of the global module fragment, so that the header exposes details but not the module? Essentially what I'm proposing:

httplib.h:

namespace httplib {
    // ...

    #ifndef HTTPLIB_MODULE_HIDE_DETAILS
    namespace detail {
        // details here
    }
    #endif

    // ...
}

httplib.cppm:

module;

#define HTTPLIB_MODULE_HIDE_DETAILS

// all httplib includes
#include <...>

export module httplib;

export {
    // Because HTTPLIB_MODULE_HIDE_DETAILS is defined, httplib::detail::* symbols aren't exported
    #include "../httplib.h"
}

@yhirose do let me know what you think, I think this wouldn't any more difficult to maintain moving forward, we would just have to ensure that #ifndef HTTPLIB_MODULE_HIDE_DETAILS appears before (or inside) every namespace detail declaration.

@mikomikotaishi
Copy link
Contributor Author

Thanks, will review tomorrow

@yhirose
Copy link
Owner

yhirose commented Dec 30, 2025

@mikomikotaishi Please don't change httplib.h or README.md at all. Instead, just focus on creating the simpler httplib.cppm, making the smallest possible changes and adding only the minimum necessary usage to the CMake-related files. Thank you.

@mikomikotaishi
Copy link
Contributor Author

Then the PR is basically in a finished state.

@Tachi107
Copy link
Contributor

where would it be appropriate to document that the library contains support for modules features?

I think the build script itself is fine. Meson has meson_options.txt, which is self-documenting; CMake users can just grep for the available options(), or use some command I don't remember (or the GUI) to have CMake list them. As another example, the project supports system-wide installation, but since this is a build-system-specific feature its "documentation" is, well, the build system itself.

* but it will have to do until we figure out how to use a script to do it.
*/
export {
#include "../httplib.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

As mentioned earlier, would it make sense to change this to the split .h, with less implementation details exposed, or does it require the full header?

In case the smaller header can be used instead, it might make sense to make this file a template httplib.cppm.in and configure it from CMake by including the header produced my execute_process() (which should really be changed to add_custom_command(OUTPUT), but that's a separate issue) via @VAR@ substitutions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would like to do this at some point with the smaller header, seeing as the the library has to be compiled anyway if the module has to be compiled.

Copy link
Owner

@yhirose yhirose Jan 10, 2026

Choose a reason for hiding this comment

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

I don't prefer including such a lengthy list of #include ..., because it's not easily maintainable... Is the following enough in this file?

export module httplib;

export {
    #include "../httplib.h"
}

Copy link
Contributor Author

@mikomikotaishi mikomikotaishi Jan 11, 2026

Choose a reason for hiding this comment

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

The headers above were included first because it activates the header guard preventing it from being pulled in when the actual httplib.h header is included in the export block. If we didn't do that, the module would export stuff like std::* symbols or other headers pulled in.

This can just be copied in when we generate modules with Python instead

Copy link
Owner

Choose a reason for hiding this comment

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

This is too complex and appears difficult to maintain. I'm hesitant to merge this code unless it can be simplified and the code duplication is resolved. Thanks for your understanding.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Weren't you originally content with a Python script to generate the module on demand?

Copy link
Owner

Choose a reason for hiding this comment

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

That’s fine. However, I wasn’t satisfied with the update_modules.py script at all. The script should generate the entire httplib.cppm from scratch, rather than updating the existing file using git diff. This would make the script much simpler.

Also, I would prefer not to commit httplib.cppm to this repository. It should be treated the same as httplib.h and httplib.cc, which are generated by split.py. In other words, httplib.cppm is just a build artifact.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@yhirose It's now possible just doing cmake -B build -S . -G Ninja -DHTTPLIB_COMPILE=ON -DHTTPLIB_BUILD_MODULES=ON. I hope this is sufficiently simple.

@Spixmaster
Copy link

Spixmaster commented Jan 11, 2026

Might I ask a question? Is the currennt approach good for programme packages? Think of https://aur.archlinux.org/packages?O=0&K=cpp-httplib .

Can users who have installed a package use modules and not with one package installed? This is very important because if one software depending on cpp-httplib uses modules and the other not one will need to have two different packages whose files would likely collide.

Alternatively formulated, can no modules and modules exist in parallel?

@mikomikotaishi
Copy link
Contributor Author

Alternatively formulated, can no modules and modules exist in parallel?

I'm not sure if I entirely understand what you mean. Are you asking if it's possible to #include <httplib/httplib.h> and import httplib; in the same project? I believe the answer is yes.

@mikomikotaishi
Copy link
Contributor Author

@yhirose Hi, could you please review it again and tell me your thoughts on the current solution? I think this is very simple and should be to your liking.

@yhirose yhirose requested review from Tachi107 and yhirose and removed request for Tachi107 and yhirose January 12, 2026 04:54
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