llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Error about declaration in module following declaration in the global module

Open rnikander opened this issue 3 years ago • 7 comments

I found this while trying to use the kqueue API on macOS, but here is a minimal example that shows the same thing, I think, without needing the include.

There seems to a weird situation in C++ where you have to write struct kevent instead of just kevent, in order to declare a variable, because the C library has a struct and function with the same name.

minimal.ccm

module;
// #include <sys/event.h>
struct kevent { };      // <-- something like this is in that header on macOS
void kevent(int x);     // <--
export module my_mod;
struct kevent evt;

And in my shell:

/Dev/llvm-project/build/bin/clang++ -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk -std=c++2b --precompile minimal.ccm -o minimal.pcm
minimal.ccm:6:8: error: declaration of 'kevent' in module my_mod follows declaration in the global module
    6 | struct kevent evt;
      |        ^
minimal.ccm:3:8: note: previous declaration is here
    3 | struct kevent { };
      |        ^
1 error generated.

This seems like a workaround...

module;
#include <sys/event.h>
using Renamed = struct kevent;
export module my_mod;
Renamed my_event; 

rnikander avatar Nov 11 '23 18:11 rnikander

@llvm/issue-subscribers-clang-modules

Author: None (rnikander)

I found this while trying to use the `kqueue` API on macOS, but here is a minimal example that shows the same thing, I think, without needing the include.

There seems to a weird situation in C++ where you have to write struct kevent instead of just kevent, in order to declare a variable, because the C library has a struct and function with the same name.

minimal.ccm

module;
// #include &lt;sys/event.h&gt;
struct kevent { };      // &lt;-- something like this is in that header on macOS
void kevent(int x);     // &lt;--
export module my_mod;
struct kevent evt;

And in my shell:

/Dev/llvm-project/build/bin/clang++ -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk -std=c++2b --precompile minimal.ccm -o minimal.pcm
minimal.ccm:6:8: error: declaration of 'kevent' in module my_mod follows declaration in the global module
    6 | struct kevent evt;
      |        ^
minimal.ccm:3:8: note: previous declaration is here
    3 | struct kevent { };
      |        ^
1 error generated.

This seems like a workaround...

module;
#include &lt;sys/event.h&gt;
using Renamed = struct kevent;
export module my_mod;
Renamed my_event; 

llvmbot avatar Nov 12 '23 02:11 llvmbot

It's connected with the fact that everything is written to a global module because of ABI and is unlikely to be solved in the nearest future, am I right or not?

mmd18cury avatar Aug 07 '24 19:08 mmd18cury

It's connected with the fact that everything is written to a global module because of ABI and is unlikely to be solved in the nearest future, am I right or not?

Yeah it looks true.

ChuanqiXu9 avatar Aug 08 '24 02:08 ChuanqiXu9

I am also slightly surprised since it looks like the compiler think struct kevent evt; is a declaration... I need to check it.

ChuanqiXu9 avatar Aug 08 '24 02:08 ChuanqiXu9

We are running into the same issue. We have other examples, but I think the issue is even better demonstrated by just a simplification of the example above.

$ cat foo.cppm
module;
struct kevent { };
export module my_mod;
struct kevent evt;
$ clang -std=c++20 --precompile foo.cppm -o foo.pcm
foo.cppm:4:8: error: declaration of 'kevent' in module my_mod follows declaration in the global
      module
    4 | struct kevent evt;
      |        ^
foo.cppm:2:8: note: previous declaration is here
    2 | struct kevent { };
      |        ^
1 error generated.
$ clang --version
clang version 22.0.0git (https://github.com/llvm/llvm-project c2bf1ca4001508961aedc5c59e3d6f3b429c795f)
Target: x86_64-unknown-linux-gnu

This does not have the function named kevent, in case that was causing the confusion.

This example does compile with Visual Studio 2022 compiler (Version 19.44.35214) with no errors.

sks75220 avatar Aug 15 '25 19:08 sks75220

Adding a typedef or using doesn't work for gcc.

error: global module fragment contents must be from preprocessor inclusion [-Werror=global-module]

Might come from ISO/IEC 14882:2020

It is ill-formed (an error) to redeclare an entity (such as a struct) in a named module unit if it was declared in the global module fragment.

A work around for both both work is to create a new header file add the header files for the structs and aliases and then include this before the start of the module.

daunabomba avatar Aug 18 '25 01:08 daunabomba

I encountered this issue as well when trying to use libc++ on Windows, and it's even more severe, even the libc++ headers cannot appear in the global module fragment.

click to expand PS C:\Users\xyx\Documents\cmake-test> cmake -B build -S . -G Ninja -DCMAKE_C_COMPILER="C:\Users\xyx\Documents\clang-mingw\bin\clang.exe" -DCMAKE_CXX_COMPILER="C:\Users\xyx\Documents\clang-mingw\bin\clang++.exe" -- Configuring done (0.1s) -- Generating done (0.1s) -- Build files have been written to: C:/Users/xyx/Documents/cmake-test/build PS C:\Users\xyx\Documents\cmake-test> cmake --build build [2/385] Building CXX object CMakeFiles/main_app.dir/winrt/winrt.numerics.ixx.obj FAILED: [code=1] CMakeFiles/main_app.dir/winrt/winrt.numerics.ixx.obj CMakeFiles/main_app.dir/winrt.numerics.pcm C:\Users\xyx\Documents\clang-mingw\bin\clang++.exe -IC:\Users\xyx\Documents\cmake-test\winrt -std=c++23 -MD -MT CMakeFiles/main_app.dir/winrt/winrt.numerics.ixx.obj -MF CMakeFiles\main_app.dir\winrt\winrt.numerics.ixx.obj.d @CMakeFiles\main_app.dir\winrt\winrt.numerics.ixx.obj.modmap -o CMakeFiles/main_app.dir/winrt/winrt.numerics.ixx.obj -c C:/Users/xyx/Documents/cmake-test/winrt/winrt.numerics.ixx C:/Users/xyx/Documents/clang-mingw/include/c++/v1/__new/align_val_t.h:28:12: error: declaration of 'align_val_t' in module winrt.numerics follows declaration in the global module 28 | enum class align_val_t : size_t {}; | ^ note: previous declaration is here In file included from C:/Users/xyx/Documents/cmake-test/winrt/winrt.numerics.ixx:34: In file included from C:/Users/xyx/Documents/clang-mingw/include/windowsnumerics.impl.h:28: In file included from C:/Users/xyx/Documents/clang-mingw/include/c++/v1/algorithm:1865: In file included from C:/Users/xyx/Documents/clang-mingw/include/c++/v1/__algorithm/inplace_merge.h:28: In file included from C:/Users/xyx/Documents/clang-mingw/include/c++/v1/__memory/unique_temporary_buffer.h:17: C:/Users/xyx/Documents/clang-mingw/include/c++/v1/__memory/allocator.h:34:7: error: declaration of 'allocator' in module winrt.numerics follows declaration in the global module 34 | class allocator; | ^ C:/Users/xyx/Documents/clang-mingw/include/c++/v1/__fwd/memory.h:21:7: note: previous declaration is here 21 | class allocator; | ^ In file included from C:/Users/xyx/Documents/cmake-test/winrt/winrt.numerics.ixx:34: In file included from C:/Users/xyx/Documents/clang-mingw/include/windowsnumerics.impl.h:28: In file included from C:/Users/xyx/Documents/clang-mingw/include/c++/v1/algorithm:1865: In file included from C:/Users/xyx/Documents/clang-mingw/include/c++/v1/__algorithm/inplace_merge.h:28: In file included from C:/Users/xyx/Documents/clang-mingw/include/c++/v1/__memory/unique_temporary_buffer.h:20: C:/Users/xyx/Documents/clang-mingw/include/c++/v1/__new/global_new_delete.h:30:54: error: declaration of 'operator new' in module winrt.numerics follows declaration in the global module 30 | [[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC; | ^ note: previous declaration is here C:/Users/xyx/Documents/clang-mingw/include/c++/v1/__new/global_new_delete.h:33:35: error: declaration of 'operator delete' in module winrt.numerics follows declaration in the global module 33 | _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT; | ^ note: previous declaration is here C:/Users/xyx/Documents/clang-mingw/include/c++/v1/__new/global_new_delete.h:39:54: error: declaration of 'operator new[]' in module winrt.numerics follows declaration in the global module 39 | [[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC; | ^ note: previous declaration is here C:/Users/xyx/Documents/clang-mingw/include/c++/v1/__new/global_new_delete.h:42:35: error: declaration of 'operator delete[]' in module winrt.numerics follows declaration in the global module 42 | _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT; | ^ note: previous declaration is here C:/Users/xyx/Documents/clang-mingw/include/c++/v1/__new/global_new_delete.h:49:54: error: declaration of 'operator new' in module winrt.numerics follows declaration in the global module 49 | [[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC; | ^ note: previous declaration is here C:/Users/xyx/Documents/clang-mingw/include/c++/v1/__new/global_new_delete.h:52:35: error: declaration of 'operator delete' in module winrt.numerics follows declaration in the global module 52 | _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT; | ^ note: previous declaration is here C:/Users/xyx/Documents/clang-mingw/include/c++/v1/__new/global_new_delete.h:59:1: error: declaration of 'operator new[]' in module winrt.numerics follows declaration in the global module 59 | operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC; | ^ note: previous declaration is here C:/Users/xyx/Documents/clang-mingw/include/c++/v1/__new/global_new_delete.h:62:35: error: declaration of 'operator delete[]' in module winrt.numerics follows declaration in the global module 62 | _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT; | ^ note: previous declaration is here In file included from C:/Users/xyx/Documents/cmake-test/winrt/winrt.numerics.ixx:34: In file included from C:/Users/xyx/Documents/clang-mingw/include/windowsnumerics.impl.h:29: In file included from C:/Users/xyx/Documents/clang-mingw/include/c++/v1/cmath:331: In file included from C:/Users/xyx/Documents/clang-mingw/include/c++/v1/math.h:304: C:/Users/xyx/Documents/clang-mingw/include/math.h:156:10: error: declaration of '_exception' in the global module follows declaration in module winrt.numerics 156 | struct _exception { | ^ C:/Users/xyx/Documents/clang-mingw/include/math.h:15:8: note: previous declaration is here 15 | struct _exception; | ^ 1 warning and 11 errors generated. ninja: build stopped: subcommand failed.

YexuanXiao avatar Apr 26 '26 02:04 YexuanXiao