Wednesday, March 27, 2019

C++ Modules

Modules will be included in C++ 20!


I just tried a small sample on clang-9 and it worked out of the box:


1. Create file test.cppm:


export module M;

export int sum(int op1, int op2)
{
        return op1 + op2;
}


2. Compile the module with:

./clang++ -fmodules-ts --precompile test.cppm --std=c++2a -o M.pcm

./clang++ -fmodules-ts -c M.pcm -o M.o


3. Create file main.cpp:

import M;

#include <iostream>
using namespace std;

int main()
{
        cout << sum(2, 3);

        return 0;
}



4. Compile with:

./clang++ -fmodules-ts -fprebuilt-module-path=. M.o main.cpp -o sample --std=c++2a

5. Run

./sample
5


What a great time to be a C++ programmer. :-)

No comments:

Post a Comment