// sample-module.cppm: a C++20 module interface unit. // Free sample from 99helpers.com/file-types/cpp // // sample-module-main.cpp imports it. Modules need the module compiled // before the files that import it; build systems such as CMake (3.28 and // later) work out that order for you. By hand: // GCC: g++ -std=c++20 -fmodules-ts -x c++ sample-module.cppm -x none sample-module-main.cpp -o greet // Clang: clang++ -std=c++20 --precompile sample-module.cppm -o greetings.pcm // clang++ -std=c++20 -fmodule-file=greetings=greetings.pcm sample-module-main.cpp greetings.pcm -o greet // MSVC: rename it to sample-module.ixx, Visual Studio's extension for // module interfaces, and build both files in one project. // The global module fragment: where classic #include lines belong. module; #include #include // The module declaration names the module that importers ask for. export module greetings; // Only exported names are visible to importers. export namespace greetings { std::string hello(std::string_view name); inline constexpr int version = 1; } // namespace greetings // Not exported: a helper that importers can't see or call. static std::string decorate(std::string_view text) { return "*** " + std::string{text} + " ***"; } std::string greetings::hello(std::string_view name) { return decorate("Hello, " + std::string{name} + "!"); }