Hello Swift/C++ Interoperability Workgroup,
The Chromium for iOS team is exploring enabling Swift/C++ interoperability within our codebase. We're very excited about the potential of this feature.
A key aspect of the Chromium build environment is that we use a custom version of libc++ with certain features restricted. We understand that the Swift compiler is tightly integrated with the platform's system C++ standard library, as noted in the documentation:
Swift compiler uses the platform’s default C++ standard library when interoperating with C++.
To work around this, we attempted to direct swiftc to our custom libc++ headers using -Xcc flags.
Here is the command we used:
$ swiftc -cxx-interoperability-mode=default \
-Xcc=-nostdinc++ \
-Xcc=-isystem <path-to-chromium>/third_party/libc++/src/include \
-Xcc=-isystem <path-to-chromium>/third_party/libc++abi/src/include \
<and other flags>
and what we get from the build:
<path-to-chromium>/src/out/Debug-iphoneos/../../third_party/libc++/src/include/stdexcept:188:34: error: 'std::overflow_error::overflow_error' from module 'Pointer' is not present in definition of 'std::overflow_error' in module 'Darwin.C.complex'
186 | class _LIBCPP_EXPORTED_FROM_ABI overflow_error : public runtime_error {
187 | public:
188 | _LIBCPP_HIDE_FROM_ABI explicit overflow_error(const string& __s) : runtime_error(__s) {}
| |- error: 'std::overflow_error::overflow_error' from module 'Pointer' is not present in definition of 'std::overflow_error' in module 'Darwin.C.complex'
| `- note: declaration of 'overflow_error' does not match
189 | _LIBCPP_HIDE_FROM_ABI explicit overflow_error(const char* __s) : runtime_error(__s) {}
| `- note: declaration of 'overflow_error' does not match
190 |
191 | # ifndef _LIBCPP_ABI_VCRUNTIME 192 | _LIBCPP_HIDE_FROM_ABI overflow_error(const overflow_error&) _NOEXCEPT = default;
| `- note: declaration of 'overflow_error' does not match
193 | _LIBCPP_HIDE_FROM_ABI overflow_error& operator=(const overflow_error&) _NOEXCEPT = default;
194 | ~overflow_error() _NOEXCEPT override;
Our interpretation of this error is that the Swift compiler is importing C++ type definitions from a system module, which then conflicts with the definitions provided by our custom libc++ headers. This creates a redefinition or mismatch error.
Given this context, we have a few questions:
Does our interpretation sound correct?
Is there a way to prevent the Swift compiler from importing the C++ standard library from system modules?
Any idea or workaround to achieve Swift/C++ interop in such an environment?
Any guidance or insight would be greatly appreciated. Thank you for your time and for all your work on this project.