# LLVM fails to build with modules

**URL:** <https://forums.swift.org/t/llvm-fails-to-build-with-modules/59700>\
**Category:** C++ interoperability\
**Created:** [August 16, 2022, 6:01pm UTC](https://forums.swift.org/t/llvm-fails-to-build-with-modules/59700 "2022-08-16T18:01:10Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![George](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/george/32/37757_2.png) [@George](https://forums.swift.org/u/George)\
**Post date:** [August 16, 2022, 6:01pm UTC](https://forums.swift.org/t/llvm-fails-to-build-with-modules/59700/1 "2022-08-16T18:01:10Z")

</div>

I'm trying build a recent LLVM main as a reference for my work integrating Swift's C++ interop with MLIR. As I understand it, C++ interop requires clang modules be enabled via LLVM\_ENABLE\_MODULES (though I've noticed there is no mention of this here: [GitHub - apple/swift-llvm-bindings: Swift Bindings for LLVM Project APIs](https://github.com/apple/swift-llvm-bindings))). When I try to build `check-llvm` I now run into a large number of errors similar to the following:

[build] /usr/bin/../lib/gcc/x86\_64-linux-gnu/11/../../../../include/c++/11/bits/unique\_ptr.h:37:10: note: '/usr/bin/../lib/gcc/x86\_64-linux-gnu/11/../../../../include/c++/11/tuple' included multiple times, additional include site in header from module 'LLVM\_Utils.ADT.Any'

Has anyone else run into this? Am I missing something?

---

<div class="post-metadata">

**Author:** ![Alex\_L](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alex_l/32/6202_2.png) [@Alex\_L](https://forums.swift.org/u/Alex_L)\
**Post date:** [August 17, 2022, 3:02am UTC](https://forums.swift.org/t/llvm-fails-to-build-with-modules/59700/2 "2022-08-17T03:02:29Z")

</div>

are you using libstdc++? Libstdc++ is not modularized, so that's probably why you're hitting this.

---

<div class="post-metadata">

**Author:** ![egor.zhdan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/egor.zhdan/32/44150_2.png) [@egor.zhdan](https://forums.swift.org/u/egor.zhdan)\
**Post date:** [August 17, 2022, 10:17am UTC](https://forums.swift.org/t/llvm-fails-to-build-with-modules/59700/3 "2022-08-17T10:17:55Z")

</div>

> [@George](#):
>
> As I understand it, C++ interop requires clang modules be enabled via LLVM\_ENABLE\_MODULES

I don't think this is required. Swift always enables clang modules when importing C/C++ headers, however, when building C/C++, you don't necessarily have to use modules. For example, we don't enable modules when building LLVM or Swift, but we're still able to use LLVM & Swift headers in SwiftCompilerSources.

@Alex_L is right, the error that you're seeing is caused by libstdc++ not being modularized. The Swift compiler works around this by [injecting a modulemap into libstdc++](https://github.com/apple/swift/blob/d40301fa66ab17da358131eac55f68f5e84b04a0/lib/ClangImporter/ClangIncludePaths.cpp#L237) when importing C++ headers.

---

<div class="post-metadata">

**Author:** ![George](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/george/32/37757_2.png) [@George](https://forums.swift.org/u/George)\
**Post date:** [August 21, 2022, 5:16pm UTC](https://forums.swift.org/t/llvm-fails-to-build-with-modules/59700/4 "2022-08-21T17:16:59Z")

</div>

Hmm… so this has been failing for me on recent nightlies, even if I turn LLVM\_ENABLE\_MODULES to OFF. Would there be some reason the Swift compiler fails to inject this modulemap? Is there a way I can diagnose this?

---

<div class="post-metadata">

**Author:** ![George](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/george/32/37757_2.png) [@George](https://forums.swift.org/u/George)\
**Post date:** [August 22, 2022, 7:02pm UTC](https://forums.swift.org/t/llvm-fails-to-build-with-modules/59700/5 "2022-08-22T19:02:16Z")

</div>

One of the first errors I'm running into is:

```swift
/workspaces/llvm-workspace/llvm-project/llvm/include/llvm/ADT/StringRef.h:96:43: error: no type named 'string_view' in namespace 'std'
     /*implicit*/ constexpr StringRef(std::string_view Str)

```

Could this be an issue with the modulemap for std used by the Swift compiler?

**UPDATE** It looks like this may be a C++ 17 issue... I'm not sure where this is being misinterpreted though.

Is there a flag I can pass to `swiftc` to enable C++17 mode?

---

<div class="post-metadata">

**Author:** ![Alex\_L](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alex_l/32/6202_2.png) [@Alex\_L](https://forums.swift.org/u/Alex_L)\
**Post date:** [August 23, 2022, 12:31am UTC](https://forums.swift.org/t/llvm-fails-to-build-with-modules/59700/6 "2022-08-23T00:31:18Z")

</div>

> [@George](#):
>
> Is there a flag I can pass to `swiftc` to enable C++17 mode?

`-Xfrontend -Xcc -Xfrontend -std=gnu++17`

---

<div class="post-metadata">

**Author:** ![plotfi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/plotfi/32/14061_2.png) [@plotfi](https://forums.swift.org/u/plotfi)\
**Post date:** [August 23, 2022, 12:36am UTC](https://forums.swift.org/t/llvm-fails-to-build-with-modules/59700/7 "2022-08-23T00:36:54Z")

</div>

I also get away with -Xcc -std=c++17

---

<div class="post-metadata">

**Author:** ![George](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/george/32/37757_2.png) [@George](https://forums.swift.org/u/George)\
**Post date:** [August 23, 2022, 2:13am UTC](https://forums.swift.org/t/llvm-fails-to-build-with-modules/59700/8 "2022-08-23T02:13:58Z")

</div>

Thanks! I forgot that `-std` was different from `-stdlib=libc++`.

I'm running into some different errors, both with `gnu++17` and `c++17`, the most salient of which seems to be:

```swift
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/functional:54:10: note: in file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/functional:54:
#include <tuple>
         ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/tuple:1849:27: error: redefinition of '__unpack_std_tuple<_Trait, _Tp, const tuple<_Up...> &>'
    inline constexpr bool __unpack_std_tuple<_Trait, _Tp, const tuple<_Up...>&>

```

---

<div class="post-metadata">

**Author:** ![egor.zhdan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/egor.zhdan/32/44150_2.png) [@egor.zhdan](https://forums.swift.org/u/egor.zhdan)\
**Post date:** [August 23, 2022, 12:38pm UTC](https://forums.swift.org/t/llvm-fails-to-build-with-modules/59700/9 "2022-08-23T12:38:11Z")

</div>

This looks like a modularization issue with libstdc++. I think I know a solution, I submitted a [patch](https://github.com/apple/swift/pull/60722) to fix this error.

---

<div class="post-metadata">

**Author:** ![George](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/george/32/37757_2.png) [@George](https://forums.swift.org/u/George)\
**Post date:** [August 23, 2022, 3:52pm UTC](https://forums.swift.org/t/llvm-fails-to-build-with-modules/59700/10 "2022-08-23T15:52:19Z")

</div>

Thanks @egor.zhdan!

---

<div class="post-metadata">

**Author:** ![George](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/george/32/37757_2.png) [@George](https://forums.swift.org/u/George)\
**Post date:** [September 12, 2022, 5:49pm UTC](https://forums.swift.org/t/llvm-fails-to-build-with-modules/59700/11 "2022-09-12T17:49:36Z")

</div>

@egor.zhdan I have a new issue after trying with an updated compiler version that I think may be the same problem:

```swift
/workspaces/llvm-workspace/llvm-project/llvm/include/llvm/Support/MathExtras.h:313:22: error: no template named 'is_same_v' in namespace 'std'; did you mean 'is_same'?
if constexpr (std::is_same_v<T, uint64_t>)

```

I'd wager this won't be the last one. Any chance we could add a flag to the Swift compiler to specify a modulemap for `libstdc++` at compile time? At least as a development aid?

---

<div class="post-metadata">

**Author:** ![egor.zhdan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/egor.zhdan/32/44150_2.png) [@egor.zhdan](https://forums.swift.org/u/egor.zhdan)\
**Post date:** [September 14, 2022, 11:01am UTC](https://forums.swift.org/t/llvm-fails-to-build-with-modules/59700/12 "2022-09-14T11:01:23Z")

</div>

This doesn't look like a modularization issue: both `std::is_same` and `std::is_same_v` are defined in the same header (`type_traits`) so it's likely that the header got its module assigned correctly.

I think this might be a result of wrong C++ standard version being passed to clang. Could you please try passing `-Xcc -std=c++20` to the Swift compiler?

---

<div class="post-metadata">

**Author:** ![George](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/george/32/37757_2.png) [@George](https://forums.swift.org/u/George)\
**Post date:** [September 14, 2022, 6:40pm UTC](https://forums.swift.org/t/llvm-fails-to-build-with-modules/59700/13 "2022-09-14T18:40:47Z")

</div>

Ah right, sorry I seem to have lost this flag in between git branches.
