7

I'm experimenting with Clang "modules" feature, and I'm trying to compile following piece of code:

export module a;

#include <new>

export void *foo()
{
    return ::operator new(1, std::align_val_t(1));
}

export int main() {}

Try it live

When I tried clang++ -std=c++2a -pedantic-errors -fmodules-ts --precompile -x c++-module a.cpp -o a.pcm, I got

error: ISO C++ requires a definition in this translation unit for function 'operator new'
 because its type does not have linkage [-Werror,-Wundefined-internal-type]
a.cpp:7:14: note: used here
    return ::operator new(1, std::align_val_t(1));
         ^
1 error generated.

Removing -pedantic-errors fixes the error, but when I try to link the resulting module using clang++ -std=c++2a -fmodules-ts a.pcm -o a.exe, I get

Z:\Lander\msys2\tmp\a-cfaf65.o:a.pcm:(.text+0x10): undefined reference to
 `_ZnwyW1aESt11align_val_t'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

It's especially annoying since <iostream> (indirectly) seems to rely on the aligned operator new, so I can't use it in modules too. As well as some other standard headers.

What's going on here?

It it's a Clang bug, how can I work around it?


My Clang is the latest version provided by MSYS2:

# clang++ --version
clang version 8.0.0 (tags/RELEASE_800/final)
Target: x86_64-w64-windows-gnu
Thread model: posix

EDIT:

Filed a bug report, let's see what happens...

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207

1 Answers1

3

The standard library isn't part of your module a. So don't include the header after the export module a;. Include the header before that.

T.C.
  • 133,968
  • 17
  • 288
  • 421
  • Huh. Got it, thanks! But do you happen to know what the error means exactly? Aligned new is declared as `void* operator new(std::size_t, std::align_val_t) __attribute__((__externally_visible__));` for me, so it should have external linkage, rather than no linkage? – HolyBlackCat Apr 04 '19 at 15:08