From 9774bdcf86dc5898311e31c2e040b373d491f8f6 Mon Sep 17 00:00:00 2001 From: Fabian Sauter Date: Wed, 10 Jul 2024 09:43:35 +0200 Subject: [PATCH 1/2] Converted the example to c++ modules --- CMakeLists.txt | 8 +++----- main.cpp | 16 ---------------- src/CMakeLists.txt | 6 ++++++ src/main.cpp | 5 +++++ src/mymod/CMakeLists.txt | 7 +++++++ src/mymod/MyMod.cpp | 18 ++++++++++++++++++ 6 files changed, 39 insertions(+), 21 deletions(-) delete mode 100644 main.cpp create mode 100644 src/CMakeLists.txt create mode 100644 src/main.cpp create mode 100644 src/mymod/CMakeLists.txt create mode 100644 src/mymod/MyMod.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ab170fe..ebac12b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ -cmake_minimum_required(VERSION 3.20) +cmake_minimum_required(VERSION 3.28) project(cpr_example) -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 23) # Set to C++ 11 if you are using cpr <= 1.9.x # More: https://github.com/libcpr/cpr#supported-releases @@ -20,7 +20,7 @@ if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo") endif() -add_executable(cpr_example main.cpp) +add_subdirectory(src) if(WIN32) # Install dlls in the same directory as the executable on Windows set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) @@ -31,5 +31,3 @@ include(FetchContent) FetchContent_Declare(cpr GIT_REPOSITORY https://github.com/libcpr/cpr.git GIT_TAG 1.10.5) # The commit hash for 1.10.x. Replace with the latest from: https://github.com/libcpr/cpr/releases FetchContent_MakeAvailable(cpr) - -target_link_libraries(cpr_example PRIVATE cpr::cpr) diff --git a/main.cpp b/main.cpp deleted file mode 100644 index 29ab847..0000000 --- a/main.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include -#include -#include - -int main(int argc, char** argv) { - cpr::Response r = cpr::Get(cpr::Url{"https://api.github.com/repos/whoshuu/cpr/contributors"}, - cpr::Authentication{"user", "pass", cpr::AuthMode::BASIC}, - cpr::Parameters{{"anon", "true"}, {"key", "value"}}); - std::cout << "Status code: " << r.status_code << '\n'; - std::cout << "Header:\n"; - for (const std::pair, std::basic_string>& kv : r.header) { - std::cout << '\t' << kv.first << ':' << kv.second << '\n'; - } - std::cout << "Text: " << r.text << '\n'; - return 0; -} \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..de3fde1 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.28) + +add_subdirectory(mymod) + +add_executable(cpr_example main.cpp) +target_link_libraries(cpr_example PRIVATE mymod) \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..2239969 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,5 @@ +import mymod; + +int main(int argc, char** argv) { + mymod::GetFromGitHub(); +} \ No newline at end of file diff --git a/src/mymod/CMakeLists.txt b/src/mymod/CMakeLists.txt new file mode 100644 index 0000000..259c6e5 --- /dev/null +++ b/src/mymod/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.28) + +add_library(mymod) + +target_sources(mymod PUBLIC FILE_SET CXX_MODULES FILES MyMod.cpp) + +target_link_libraries(mymod PRIVATE cpr::cpr) \ No newline at end of file diff --git a/src/mymod/MyMod.cpp b/src/mymod/MyMod.cpp new file mode 100644 index 0000000..ddb2e89 --- /dev/null +++ b/src/mymod/MyMod.cpp @@ -0,0 +1,18 @@ +module; + +#include +#include +#include + +export module mymod; + +namespace mymod { +export void GetFromGitHub() { + const cpr::Response response = cpr::Get(cpr::Url{"https://api.github.com/repos/whoshuu/cpr/contributors"}, + cpr::Authentication{"user", "pass", cpr::AuthMode::BASIC}, + cpr::Parameters{{"anon", "true"}, {"key", "value"}}); + std::cout << "Status code: " << response.status_code << '\n'; + std::cout << "Header:\n"; + std::cout << "Text: " << response.text << '\n'; +} +} // namespace mymod \ No newline at end of file From b316a2dffbb86f5b756e82f38f2eab4e98a30cbe Mon Sep 17 00:00:00 2001 From: Fabian Sauter Date: Wed, 10 Jul 2024 09:46:45 +0200 Subject: [PATCH 2/2] Added modules docs to the readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 8c10303..bceff7e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # CMake FetchContent Example A minimal example for using cpr via FetchContent in CMake. +Also shows how to use C++ 20 modules. This requires gcc >= 14.1.0. + ## Building ```bash