Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use aligned `operator new` in a module with Clang

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...

like image 404
HolyBlackCat Avatar asked Apr 03 '19 18:04

HolyBlackCat


1 Answers

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.

like image 160
T.C. Avatar answered Oct 21 '22 10:10

T.C.