howto:module_std_gcc15
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
howto:module_std_gcc15 [2025-03-08 16:28] – gelöscht - Externe Bearbeitung (Unbekanntes Datum) 127.0.0.1 | howto:module_std_gcc15 [2025-03-08 16:34] (aktuell) – rrichter | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | ====== How to use C++ module std in GNU g++-15 ====== | ||
+ | |||
+ | Modules with g++15 HEAD, CMake and Ninja under Windows 10. Warning: This is experimental. | ||
+ | |||
+ | A small demo project. Available on [[https:// | ||
+ | |||
+ | How to build and execute: | ||
+ | |||
+ | < | ||
+ | cd build | ||
+ | cmake -G Ninja | ||
+ | ninja | ||
+ | demo | ||
+ | </ | ||
+ | Prerequisites: | ||
+ | |||
+ | * GCC 15 HEAD https:// | ||
+ | * CMake 3.31.3 | ||
+ | * Ninja 1.12.1 | ||
+ | |||
+ | Limitations (at the time of writing, 2025-02-21): | ||
+ | |||
+ | * CMake supports C++ modules for Ninja generator only | ||
+ | * CMake tells Ninja nothing about module std. Hack: extra library '' | ||
+ | |||
+ | Some lines in source file '' | ||
+ | |||
+ | <code cpp> | ||
+ | // using std:: | ||
+ | // using std:: | ||
+ | // using std:: | ||
+ | // using std:: | ||
+ | // using std:: | ||
+ | </ | ||
+ | Sadly, newer weekly build compiler '' | ||
+ | |||
+ | ==== CMake ==== | ||
+ | |||
+ | < | ||
+ | cmake_minimum_required(VERSION 3.31) | ||
+ | |||
+ | project( | ||
+ | compile_with_module_std | ||
+ | VERSION 0.0.1 | ||
+ | LANGUAGES CXX) | ||
+ | |||
+ | # temporary hack for GCC g++15.0 HEAD https:// | ||
+ | # to remove when g++ & CMake know about module std | ||
+ | add_library(module_std) | ||
+ | |||
+ | target_sources(module_std | ||
+ | PUBLIC | ||
+ | FILE_SET CXX_MODULES FILES | ||
+ | gnu/std.cc # modified from MinGW\include\c++\15.0.0\bits\std.cc | ||
+ | ) | ||
+ | target_compile_features(module_std PUBLIC cxx_std_26) | ||
+ | # also to remove (see below): | ||
+ | # target_link_libraries(...XYZ... PRIVATE module_std) | ||
+ | |||
+ | # another hack for advanced features like std:: | ||
+ | # target_link_libraries(...XYZ... PRIVATE stdc++exp) | ||
+ | |||
+ | add_library(my_module) | ||
+ | |||
+ | target_sources(my_module | ||
+ | PUBLIC | ||
+ | FILE_SET CXX_MODULES FILES | ||
+ | src/ | ||
+ | ) | ||
+ | target_compile_features(my_module PUBLIC cxx_std_26) | ||
+ | target_link_libraries(my_module PRIVATE module_std) | ||
+ | |||
+ | add_executable(demo src/ | ||
+ | target_compile_features(demo PUBLIC cxx_std_26) | ||
+ | target_link_libraries(demo PRIVATE module_std) | ||
+ | target_link_libraries(demo PRIVATE my_module) | ||
+ | target_link_libraries(demo PRIVATE stdc++exp) | ||
+ | |||
+ | </ | ||
+ | ==== C++ code ==== | ||
+ | |||
+ | Modular '' | ||
+ | |||
+ | <code cpp> | ||
+ | export module my_first_module; | ||
+ | import std; | ||
+ | |||
+ | export namespace something | ||
+ | { | ||
+ | |||
+ | std::string hello(std:: | ||
+ | { | ||
+ | return " | ||
+ | } | ||
+ | |||
+ | } // end namespace something | ||
+ | </ | ||
+ | used in '' | ||
+ | |||
+ | <code cpp> | ||
+ | import std; | ||
+ | import my_first_module; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | std:: | ||
+ | } | ||
+ | </ | ||