Skip to content
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

C++20 modules support #1162

Open
germandiagogomez opened this issue Jan 13, 2025 · 4 comments
Open

C++20 modules support #1162

germandiagogomez opened this issue Jan 13, 2025 · 4 comments

Comments

@germandiagogomez
Copy link

Is your feature request related to a problem?

I would like to use cpr as a module.

Possible Solution

Being able to import cpr from my own code.

Alternatives

Compile with #include

Additional Context

I would like to start to use modules in the context of a bigger project.

@COM8
Copy link
Member

COM8 commented Jan 16, 2025

@germandiagogomez thanks for your feature request!
Yes, this would be awesome and I would love to merge a PR adding this functionality.

@COM8 COM8 changed the title C++20 modules support? C++20 modules support Jan 16, 2025
@germandiagogomez
Copy link
Author

Hello @COM8. Maybe over some weekend I could try a couple of experiments at some point. But I do not know yet when that weekend will come. Too busy with work actually til at least May unless I find a slot somewhere.

@COM8
Copy link
Member

COM8 commented Feb 8, 2025

No hurry I know we all got other stuff to do and we are all doing this in our free time :)

@wdhongtw
Copy link

wdhongtw commented Feb 18, 2025

Here is one possible way to support C++20 module usage.

diff --git a/cpr/CMakeLists.txt b/cpr/CMakeLists.txt
index db48358..ad8922d 100644
--- a/cpr/CMakeLists.txt
+++ b/cpr/CMakeLists.txt
@@ -29,6 +29,10 @@ add_library(cpr
         curlmultiholder.cpp
         multiperform.cpp)
 
+target_compile_features(cpr PUBLIC cxx_std_20)
+
+target_sources(cpr PUBLIC FILE_SET CXX_MODULES FILES module.cpp)
+
 add_library(cpr::cpr ALIAS cpr)
 
 target_link_libraries(cpr PUBLIC ${CURL_LIB}) # todo should be private, but first dependencies in ssl_options need to be removed
@@ -50,6 +54,7 @@ include(GNUInstallDirs)
 
 install(TARGETS cpr
         EXPORT cprTargets
+        FILE_SET CXX_MODULES DESTINATION ${CMAKE_INSTALL_LIBDIR}
         RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
         ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
         LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
diff --git a/cpr/module.cpp b/cpr/module.cpp
new file mode 100644
index 0000000..74d86d8
--- /dev/null
+++ b/cpr/module.cpp
@@ -0,0 +1,16 @@
+module;
+
+#include "cpr/cpr.h"
+
+export module cpr;
+
+export namespace cpr {
+using cpr::Authentication;
+using cpr::AuthMode;
+
+using cpr::Parameters;
+using cpr::Response;
+using cpr::Url;
+
+using cpr::Get;
+} // namespace cpr

Now the example in README.md can be switched between import cpr; and #include <cpr/cpr.h>.
Just export every public stuffs through using again.

(Edited)

We might need to first decide whether users can using cpr through module
is a ABI compatible way. (linking against .so that's built in module-agnostic configuration)
Or require users to always build cpr on their own when using in module mode.
That will have significant impact on which option we provide in CMake,
and how we structure module file and/or adjust header files.

It's a problem since that cpr already been release in some distributions like Fedora and Arch.

See also:

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

3 participants