namespace cpp

C++ lernen, kennen, anwenden

Benutzer-Werkzeuge

Webseiten-Werkzeuge


howto:modules_gcc11

Dies ist eine alte Version des Dokuments!


How to use C++20 modules in GNU g++-11

using gcc11-20210124-32.exe (Windows build on equation.com)

module definition

#include headers in the global module fragment

module;
#include <iostream>
// ...

before modulemodule_name; or import them after that:

export module greetings;
import <iostream>;  
 
export auto say_hello()
{
    std::cout << "Hello, modules!\n";   
}

module interface unit

Every module consists of exactly one module interface unit export modulemodule_name; after which it declares which names should be exported, and eventually other module implementation units (source files) starting with modulemodule_name; (without export).

import module

by its module name if you want to use the contents of a module

import greetings;
 
int main()
{
    say_hello();    
}

Prepare std header unit(s)

before compiling modularized source code containing e.g. import <iostream>;

g++ -fmodule-header -std=c++20 -x c++ c:\gcc\include\c++\11.0.0\iostream

(adapt the path to include files on your system …)

Compile program

with option -fmodules-ts in c++20 mode:

g++ greetings.cpp main.cpp -fmodules-ts -std=c++20

Done! Start program:

> a.exe
Hello, modules!
howto/modules_gcc11.1612203312.txt.gz · Zuletzt geändert: 2021-02-01 19:15 von rrichter