msys2 / MINGW-packages

Package scripts for MinGW-w64 targets to build under MSYS2.
https://packages.msys2.org
BSD 3-Clause "New" or "Revised" License
2.29k stars 1.22k forks source link

CLANG64 clang++ fatal error: could not build module 'std' (C++ 20 Modules) #16665

Open ynty opened 1 year ago

ynty commented 1 year ago

Description / Steps to reproduce the issue

  1. Install clang++ with: pacman -S mingw-w64-clang-x86_64-toolchain.
  2. Here is a sample of C++ 20 Modules:
    //hello.cpp
    import <iostream>;
    using std::cout, std::endl;
    int main() {
        cout << "Hello C++ 20." << endl;
        return 0;
    }
  3. Compile it with command clang++ hello.cpp -o hello -std=c++20 -fmodules, just like how compile it on Mac with clang 16 (installed from homebrew).

通过 pacman -S mingw-w64-clang-x86_64-toolchain 安装 clang++

以下是一个 C++ 20 模块功能的简单示例:

//hello.cpp
import <iostream>;
using std::cout, std::endl;
int main() {
        cout << "Hello C++ 20." << endl;
        return 0;
}

使用 clang++ hello.cpp -o hello -std=c++20 -fmodules 编译。(在 Mac 上使用 clang 16 可以编译成功)

Expected behavior

...@...... CLANG64 ~/Cpp20
$ clang++ hello.cpp -o hello -std=c++20 -fmodules

...@...... CLANG64 ~/Cpp20
$ ./hello
Hello C++ 20.

Actual behavior

clang++ failed to compile hello.cpp, with the message "fatal error: could not build module 'std'".

While building module 'std' imported from hello.cpp:1:
In file included from <module-includes>:17:
In file included from <msys64>/clang64/include/c++/v1/math.h:316:
In file included from <msys64>/clang64/include/c++/v1/stdlib.h:94:
In file included from <msys64>/clang64/include/stdlib.h:770:
<msys64>/clang64/include/malloc.h:125:25: error: redefinition of '_MarkAllocaS'
  static __inline void *_MarkAllocaS(void *_Ptr,unsigned int _Marker) {
                        ^
<msys64>/clang64/include/malloc.h:125:25: note: previous definition is here
  static __inline void *_MarkAllocaS(void *_Ptr,unsigned int _Marker) {
                        ^
While building module 'std' imported from hello.cpp:1:
In file included from <module-includes>:17:
In file included from <msys64>/clang64/include/c++/v1/math.h:316:
In file included from <msys64>/clang64/include/c++/v1/stdlib.h:94:
In file included from <msys64>/clang64/include/stdlib.h:770:
<msys64>/clang64/include/malloc.h:144:32: error: redefinition of '_freea'
  static __inline void __cdecl _freea(void *_Memory) {
                               ^
<msys64>/clang64/include/malloc.h:144:32: note: previous definition is here
  static __inline void __cdecl _freea(void *_Memory) {
                               ^
While building module 'std' imported from hello.cpp:1:
In file included from <module-includes>:17:
In file included from <msys64>/clang64/include/c++/v1/math.h:316:
<msys64>/clang64/include/c++/v1/stdlib.h:113:61: error: redefinition of 'abs'
_LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY long abs(long __x) _NOEXCEPT {
                                                            ^
<msys64>/clang64/include/c++/v1/stdlib.h:113:61: note: previous definition is here
_LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY long abs(long __x) _NOEXCEPT {
                                                            ^
While building module 'std' imported from hello.cpp:1:
In file included from <module-includes>:17:
In file included from <msys64>/clang64/include/c++/v1/math.h:316:
<msys64>/clang64/include/c++/v1/stdlib.h:116:66: error: redefinition of 'abs'
_LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY long long abs(long long __x) _NOEXCEPT {
                                                                 ^
<msys64>/clang64/include/c++/v1/stdlib.h:116:66: note: previous definition is here
_LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY long long abs(long long __x) _NOEXCEPT {
                                                                 ^
While building module 'std' imported from hello.cpp:1:
In file included from <module-includes>:17:
In file included from <msys64>/clang64/include/c++/v1/math.h:316:
<msys64>/clang64/include/c++/v1/stdlib.h:122:62: error: redefinition of 'abs'
_LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY float abs(float __lcpp_x) _NOEXCEPT {
                                                             ^
<msys64>/clang64/include/c++/v1/stdlib.h:122:62: note: previous definition is here
_LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY float abs(float __lcpp_x) _NOEXCEPT {
                                                             ^
While building module 'std' imported from hello.cpp:1:
In file included from <module-includes>:17:
In file included from <msys64>/clang64/include/c++/v1/math.h:316:
<msys64>/clang64/include/c++/v1/stdlib.h:126:63: error: redefinition of 'abs'
_LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY double abs(double __lcpp_x) _NOEXCEPT {
                                                              ^
<msys64>/clang64/include/c++/v1/stdlib.h:126:63: note: previous definition is here
_LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY double abs(double __lcpp_x) _NOEXCEPT {
                                                              ^
While building module 'std' imported from hello.cpp:1:
In file included from <module-includes>:17:
In file included from <msys64>/clang64/include/c++/v1/math.h:316:
<msys64>/clang64/include/c++/v1/stdlib.h:131:1: error: redefinition of 'abs'
abs(long double __lcpp_x) _NOEXCEPT {
^
<msys64>/clang64/include/c++/v1/stdlib.h:131:1: note: previous definition is here
abs(long double __lcpp_x) _NOEXCEPT {
^
While building module 'std' imported from hello.cpp:1:
In file included from <module-includes>:17:
In file included from <msys64>/clang64/include/c++/v1/math.h:316:
<msys64>/clang64/include/c++/v1/stdlib.h:150:41: error: redefinition of 'div'
inline _LIBCPP_INLINE_VISIBILITY ldiv_t div(long __x, long __y) _NOEXCEPT {
                                        ^
<msys64>/clang64/include/c++/v1/stdlib.h:150:41: note: previous definition is here
inline _LIBCPP_INLINE_VISIBILITY ldiv_t div(long __x, long __y) _NOEXCEPT {
                                        ^
While building module 'std' imported from hello.cpp:1:
In file included from <module-includes>:17:
In file included from <msys64>/clang64/include/c++/v1/math.h:316:
<msys64>/clang64/include/c++/v1/stdlib.h:154:42: error: redefinition of 'div'
inline _LIBCPP_INLINE_VISIBILITY lldiv_t div(long long __x,
                                         ^
<msys64>/clang64/include/c++/v1/stdlib.h:154:42: note: previous definition is here
inline _LIBCPP_INLINE_VISIBILITY lldiv_t div(long long __x,
                                         ^
While building module 'std' imported from hello.cpp:1:
In file included from <module-includes>:34:
In file included from <msys64>/clang64/include/c++/v1/complex.h:27:
In file included from <msys64>/clang64/include/c++/v1/ccomplex:21:
In file included from <msys64>/clang64/include/c++/v1/complex:243:
In file included from <msys64>/clang64/include/c++/v1/sstream:191:
In file included from <msys64>/clang64/include/c++/v1/istream:165:
In file included from <msys64>/clang64/include/c++/v1/ostream:168:
In file included from <msys64>/clang64/include/c++/v1/__memory/shared_ptr.h:42:
In file included from <msys64>/clang64/include/c++/v1/atomic:526:
In file included from <msys64>/clang64/include/c++/v1/__thread/timed_backoff_policy.h:18:
<msys64>/clang64/include/c++/v1/__threading_support:57:11: error: missing '#include <sys/timeb.h>'; 'timespec' must be declared before it is used
typedef ::timespec __libcpp_timespec_t;
          ^
<msys64>/clang64/include/sys/timeb.h:101:8: note: declaration here is not visible
struct timespec {
       ^
hello.cpp:1:8: fatal error: could not build module 'std'
import <iostream>;
~~~~~~~^
11 errors generated.

<msys64> is where I installed MSYS2.


编译失败,提示“致命错误:无法构建模组‘std’”,详细信息如上。其中 <msys64> 为 MSYS2 的安装路径。(D:/msys64)

Verification

Windows Version

MINGW64_NT-10.0-22621

MINGW environments affected

As I'm using a x86_64 PC, I don't know whether this issue will affect CLANG32.

Are you willing to submit a PR?

No.

oscarfv commented 1 year ago

The problem here probably is that the platform is not modularized yet:

https://clang.llvm.org/docs/Modules.html#modularizing-a-platform

Your test case also fails to compile on Debian Testing with clang 16.0.1 and the gcc 12.2.0 C++ Standard Library:

$ clang++ modules.cpp -o modules -std=c++20 -fmodules
modules.cpp:1:8: error: header file <iostream> (aka
      '/usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/iostream') cannot be imported because it is not known
      to be a header unit
import <iostream>;
       ^
modules.cpp:2:7: error: use of undeclared identifier 'std'
using std::cout, std::endl;
      ^
modules.cpp:2:18: error: use of undeclared identifier 'std'
using std::cout, std::endl;
                 ^
modules.cpp:4:9: error: use of undeclared identifier 'cout'
        cout << "Hello C++ 20." << endl;
        ^
modules.cpp:4:36: error: use of undeclared identifier 'endl'
        cout << "Hello C++ 20." << endl;
                                   ^
5 errors generated

However, it works when using libc++:

$ clang++ -stdlib=libc++ modules.cpp -o modules -std=c++20 -fmodules
$ ./modules
Hello C++ 20