0

When compiling c++-20 modules with clang. I get strange compilation errors when importing a file that imports another file that imports a third file like so:

// a.cppm
#include <string>

export module a;

export std::string getStuffA() {
  return "a";
}
// b.cppm
#include <string>

import a;

export module b;

export std::string getStuffB() {
  return "b" + getStuffA();
}
// c.cpp
#include <string>

import b;

int main() {
  std::cout << getStuffB() << std::endl;
  return 0;
}

The compiler outputs:

In file included from ./src/c.cpp:1:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/iostream:39:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/ostream:38:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/ios:44:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/basic_ios.h:37:
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/locale_facets.h:2569:5: error: inline declaration of 'isspace' follows non-inline definition

And it lists some more functions that seems to have both inline and non-inline definiton, like isprint, iscntrl, isupper.

Is there any way to get around these errors.

Note: Removing std::string and #include <string> removes the compilation errors, so I think that the errors comes from <string>

Lasersköld
  • 2,028
  • 14
  • 20

1 Answers1

0

As already said, modules are supported in gcc 11, and you are trying to use clang with gcc 10 standard C++ library.

With using the proper C++ standard library, preprocessor include directives such as #include <string> will be automatically mapped to import <string>;. With using the standard C++ library for gcc 10, that does not support modules, you will get errors such as you have got.

If you use clang, that supports modules, and it seems you are using such modern clang, otherwise you would get another errors on the import directive, you should use the clang C++ standard library: -stdlib=libc++.

273K
  • 29,503
  • 10
  • 41
  • 64