namespace cpp {}

C++ lernen, kennen, anwenden

Benutzer-Werkzeuge

Webseiten-Werkzeuge


howto:modules_gcc11
no way to compare when less than two revisions

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.


howto:modules_gcc11 [2021-02-01 19:44] (aktuell) – angelegt - Externe Bearbeitung 127.0.0.1
Zeile 1: Zeile 1:
 +====== How to use C++20 modules in GNU g++-11 ======
 +
 +using gcc11-20210124-32.exe (Windows build from [[http://www.equation.com/servlet/equation.cmd?fa=fortran|equation.com]]) (Warning: This is experimental!)
 +
 +===== module definition =====
 +
 +''%%#include%%'' headers in the global module fragment
 +
 +<code cpp>
 +module;
 +#include <iostream>
 +// ...
 +</code>
 +before ''module'' //''module_name''//'';'' or ''import'' them after that:
 +
 +<code cpp greetings.cpp>
 +export module greetings;
 +import <iostream>;  
 +
 +export auto say_hello()
 +{
 +    std::cout << "Hello, modules!\n";   
 +}
 +</code>
 +===== module interface unit =====
 +
 +Every module consists of exactly one module interface unit ''export module'' //''module_name''//'';'' after which it declares which names should be ''export''ed, and eventually other module implementation units (source files) starting with ''module'' //''module_name''//'';'' (without ''export'').
 +
 +===== import module =====
 +
 +by its module name if you want to use the contents of a module
 +
 +<code cpp main.cpp>
 +import greetings;
 +
 +int main()
 +{
 +    say_hello();    
 +}
 +</code>
 +===== Prepare (std) header unit(s) =====
 +
 +before compiling modularized source code containing e.g. ''import <iostream>;''
 +
 +<code>
 +g++ -fmodules-ts -std=c++20 -c -x c++-system-header iostream
 +</code>
 +Other options are ''c++-header'' and ''c++-user-header'' (uses ''#include'' path).
 +===== Compile program =====
 +
 +with option ''-fmodules-ts'' in ''c++20'' mode:
 +
 +<code>
 +g++ greetings.cpp main.cpp -fmodules-ts -std=c++20
 +</code>
 +Done! Start program:
 +
 +<code>
 +> a.exe
 +Hello, modules!
 +</code>
 +
 +===== Further reading =====
 +  * Nathan Sidwell: C++ Modules. A Brief Tour. Overload 28(159) 19-23, October 2020. https://accu.org/journals/overload/28/159/sidwell
 + 
  
howto/modules_gcc11.txt · Zuletzt geändert: 2021-02-01 19:44 von 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki