c++ – C++20 modules error when including the iostream header in Clang

Consider the following files:

math.cppm

export module math;

export namespace math {
    int sum(int num1, int num2);

    int multiply(int num1, int num2);
}

/*
Compiled with: 
clang++ -c --std=c++20 -stdlib=libc++ -Xclang -emit-module-interface --precompile -o math.pcm math.cppm
*/

math.cpp

module;
#include <iostream>  // HERE it's the problem

module math;


// Implementation of the definitions on the module unit interface
// for the sum and multiply math operations

namespace math {
    int sum(int num1, int num2) {
        return num1 + num2;
    }

    int multiply(int num1, int num2) {
        return num1 * num2;
    }
}

/*
Compiled with: 
clang++ -c --std=c++20 -stdlib=libc++ math.cpp -o math.o -fmodule-file=./math.pcm
*/

main.cpp

import <iostream>;
import math;

int main() {

    std::cout << "RESULT '+': " << math::sum(2, 8) << std::endl;
    std::cout << "RESULT '-': " << math::substract(8, 2) << std::endl;

    return 0;
}

/*
Compiled with:
clang++ --std=c++20 -stdlib=libc++ -fmodules -fimplicit-modules -fbuiltin-module-map -fimplicit-module-maps -o main.exe main.cpp math.pcm math.o -fprebuilt-module-path=.
*/

And I am getting this error:

In file included from math.cpp:3:
In file included from C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includeiostream:11:
In file included from C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includeistream:11:
In file included from C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includeostream:11:
In file included from C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includeios:11:
In file included from C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includexlocnum:16:
In file included from C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includestreambuf:11:
In file included from C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includexiosbase:12:
In file included from C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includesystem_error:14:
In file included from C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includestdexcept:11:
In file included from C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includeexception:33:
In file included from C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includevcruntime_exception.h:10:
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includeeh.h:56:14: error: reference to 'type_info' is ambiguous
        _In_ type_info const&     _Type,
             ^
note: candidate found by name lookup is 'type_info'
note: candidate found by name lookup is 'type_info'
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includeeh.h:56:14: error: reference to 'type_info' is ambiguous
note: candidate found by name lookup is 'type_info'
note: candidate found by name lookup is 'type_info'
In file included from zero/src/testing/testing.cpp:3:
In file included from C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includeiostream:11:
In file included from C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includeistream:11:
In file included from C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includeostream:11:
In file included from C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includeios:11:
In file included from C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includexlocnum:16:
In file included from C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includestreambuf:11:
In file included from C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includexiosbase:13:
In file included from C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includexlocale:13:
In file included from C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includememory:14:
In file included from C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includetypeinfo:23:
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includevcruntime_typeinfo.h:121:13: error: reference to 'type_info' is ambiguous
    using ::type_info;
            ^
note: candidate found by name lookup is 'type_info'
note: candidate found by name lookup is 'type_info'
In file included from zero/src/testing/testing.cpp:3:
In file included from C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includeiostream:11:
In file included from C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includeistream:11:
In file included from C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.30.30705includeostream:11:

If I don’t split the module into interface and declaration, I can put #include <iostream> in the .cppm file and compiles without problem.

Does anyone know what could be the issue? Must I add something to the command line when the implementation file includes a header?

Leave a Comment