Skip to content

Clang -Wmodule-import-in-extern-c compiler error when using C++ modules #10490

@zacharykeyessonos

Description

@zacharykeyessonos

Summary

There's a compiler error when C++ projects that use the 'modules' C++ language feature consume Mbed TLS. The specific error is module-import-in-extern-c, which occurs when #includes occur within extern C blocks.

This seems to primarily be an issue with clang. See https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fmodules

System information

TF-PSA-Crypto version (number or commit id): 1.0.0
Operating system and version: Ubuntu 22.04
Configuration (if not default, please attach crypto_config.h):
Compiler and options (if you used a pre-built binary, please indicate how you obtained it): clang -fmodules
Additional environment information:

Expected behavior

Building a C++ project that consumes Mbed TLS and using the -fmodules flag should succeed.

Actual behavior

Building a C++ project that consumes Mbed TLS and using the -fmodules flag fails:

$ clang-17 -std=c++20 -fmodules -Itf-psa-crypto/include -Itf-psa-crypto/drivers/builtin/include -o sample sample.cpp
In file included from sample.cpp:8:
In file included from tf-psa-crypto/drivers/builtin/include/mbedtls/private/chachapoly.h:44:
tf-psa-crypto/drivers/builtin/include/mbedtls/private/chacha20.h:26:1: error: import of C++ module '_Builtin_stdint' appears within extern "C" language linkage specification [-Wmodule-import-in-extern-c]
   26 | #include <stdint.h>
      | ^
tf-psa-crypto/drivers/builtin/include/mbedtls/private/chachapoly.h:35:1: note: extern "C" language linkage specification begins here
   35 | extern "C" {
      | ^
1 error generated.

Steps to reproduce

Build a C++ program that uses impacted Mbed TLS headers with clang and the -fmodules flag.

One way to do this (from mbedtls-3.6.5):

  1. Apply this diff
--- a/programs/test/CMakeLists.txt
+++ b/programs/test/CMakeLists.txt
@@ -30,6 +30,7 @@ if(TEST_CPP)
     )
     add_executable(cpp_dummy_build "${cpp_dummy_build_cpp}")
     target_include_directories(cpp_dummy_build PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../include)
+    target_compile_options(cpp_dummy_build PRIVATE -fmodules)
     target_link_libraries(cpp_dummy_build ${mbedcrypto_target} ${CMAKE_THREAD_LIBS_INIT})
 endif()
  1. scripts/config.py full
  2. CC=clang CXX=clang cmake -D ENABLE_TESTING=On -D TEST_CPP=1 .
  3. make
  4. Observe the build error:
Consolidate compiler generated dependencies of target cpp_dummy_build
[ 38%] Building CXX object programs/test/CMakeFiles/cpp_dummy_build.dir/cpp_dummy_build.cpp.o
In file included from mbedtls/programs/test/cpp_dummy_build.cpp:13:
In file included from mbedtls/programs/test/../../include/mbedtls/aes.h:33:
In file included from mbedtls/programs/test/../../include/mbedtls/platform_util.h:18:
mbedtls/programs/test/../../include/mbedtls/platform_time.h:33:1: error: import of C++ module '_Builtin_stdint' appears within extern "C" language linkage specification [-Wmodule-import-in-extern-c]
   33 | #include <stdint.h>
      | ^
mbedtls/programs/test/../../include/mbedtls/platform_time.h:16:1: note: extern "C" language linkage specification begins here
   16 | extern "C" {
      | ^
mbedtls/programs/test/../../include/mbedtls/platform_time.h:34:1: error: import of C++ module '_Builtin_inttypes' appears within extern "C" language linkage specification [-Wmodule-import-in-extern-c]
   34 | #include <inttypes.h>
      | ^
mbedtls/programs/test/../../include/mbedtls/platform_time.h:16:1: note: extern "C" language linkage specification begins here
   16 | extern "C" {
      | ^
In file included from mbedtls/programs/test/cpp_dummy_build.cpp:31:
In file included from mbedtls/programs/test/../../include/mbedtls/debug.h:15:
In file included from mbedtls/programs/test/../../include/mbedtls/ssl.h:20:
In file included from mbedtls/programs/test/../../include/mbedtls/ssl_ciphersuites.h:16:
In file included from mbedtls/programs/test/../../include/mbedtls/pk.h:32:
In file included from mbedtls/programs/test/../../include/psa/crypto.h:40:
mbedtls/programs/test/../../include/psa/crypto_types.h:42:1: error: import of C++ module '_Builtin_stdint' appears within extern "C" language linkage specification [-Wmodule-import-in-extern-c]
   42 | #include <stdint.h>
      | ^
mbedtls/programs/test/../../include/psa/crypto.h:35:1: note: extern "C" language linkage specification begins here
   35 | extern "C" {
      | ^
In file included from mbedtls/programs/test/cpp_dummy_build.cpp:31:
In file included from mbedtls/programs/test/../../include/mbedtls/debug.h:15:
In file included from mbedtls/programs/test/../../include/mbedtls/ssl.h:20:
In file included from mbedtls/programs/test/../../include/mbedtls/ssl_ciphersuites.h:16:
In file included from mbedtls/programs/test/../../include/mbedtls/pk.h:32:
In file included from mbedtls/programs/test/../../include/psa/crypto.h:71:
In file included from mbedtls/programs/test/../../include/psa/crypto_struct.h:68:
In file included from mbedtls/programs/test/../../include/psa/crypto_driver_contexts_primitives.h:24:
mbedtls/programs/test/../../include/psa/crypto_driver_common.h:26:1: error: import of C++ module '_Builtin_stdint' appears within extern "C" language linkage specification [-Wmodule-import-in-extern-c]
   26 | #include <stdint.h>
      | ^
mbedtls/programs/test/../../include/psa/crypto_struct.h:54:1: note: extern "C" language linkage specification begins here
   54 | extern "C" {
      | ^
In file included from mbedtls/programs/test/cpp_dummy_build.cpp:31:
In file included from mbedtls/programs/test/../../include/mbedtls/debug.h:15:
In file included from mbedtls/programs/test/../../include/mbedtls/ssl.h:20:
In file included from mbedtls/programs/test/../../include/mbedtls/ssl_ciphersuites.h:16:
In file included from mbedtls/programs/test/../../include/mbedtls/pk.h:32:
In file included from mbedtls/programs/test/../../include/psa/crypto.h:71:
In file included from mbedtls/programs/test/../../include/psa/crypto_struct.h:68:
In file included from mbedtls/programs/test/../../include/psa/crypto_driver_contexts_primitives.h:27:
In file included from mbedtls/programs/test/../../include/psa/crypto_builtin_primitives.h:31:
mbedtls/programs/test/../../include/mbedtls/md5.h:21:1: error: import of C++ module '_Builtin_stdint' appears within extern "C" language linkage specification [-Wmodule-import-in-extern-c]
   21 | #include <stdint.h>
      | ^
mbedtls/programs/test/../../include/psa/crypto_struct.h:54:1: note: extern "C" language linkage specification begins here
   54 | extern "C" {
      | ^
In file included from mbedtls/programs/test/cpp_dummy_build.cpp:31:
In file included from mbedtls/programs/test/../../include/mbedtls/debug.h:15:
In file included from mbedtls/programs/test/../../include/mbedtls/ssl.h:20:
In file included from mbedtls/programs/test/../../include/mbedtls/ssl_ciphersuites.h:16:
In file included from mbedtls/programs/test/../../include/mbedtls/pk.h:32:
In file included from mbedtls/programs/test/../../include/psa/crypto.h:71:
In file included from mbedtls/programs/test/../../include/psa/crypto_struct.h:68:
In file included from mbedtls/programs/test/../../include/psa/crypto_driver_contexts_primitives.h:27:
In file included from mbedtls/programs/test/../../include/psa/crypto_builtin_primitives.h:32:
mbedtls/programs/test/../../include/mbedtls/ripemd160.h:17:1: error: import of C++ module '_Builtin_stdint' appears within extern "C" language linkage specification [-Wmodule-import-in-extern-c]
   17 | #include <stdint.h>
      | ^
mbedtls/programs/test/../../include/psa/crypto_struct.h:54:1: note: extern "C" language linkage specification begins here
   54 | extern "C" {
      | ^
In file included from mbedtls/programs/test/cpp_dummy_build.cpp:31:
In file included from mbedtls/programs/test/../../include/mbedtls/debug.h:15:
In file included from mbedtls/programs/test/../../include/mbedtls/ssl.h:20:
In file included from mbedtls/programs/test/../../include/mbedtls/ssl_ciphersuites.h:16:
In file included from mbedtls/programs/test/../../include/mbedtls/pk.h:32:
In file included from mbedtls/programs/test/../../include/psa/crypto.h:71:
In file included from mbedtls/programs/test/../../include/psa/crypto_struct.h:68:
In file included from mbedtls/programs/test/../../include/psa/crypto_driver_contexts_primitives.h:27:
In file included from mbedtls/programs/test/../../include/psa/crypto_builtin_primitives.h:33:
mbedtls/programs/test/../../include/mbedtls/sha1.h:24:1: error: import of C++ module '_Builtin_stdint' appears within extern "C" language linkage specification [-Wmodule-import-in-extern-c]
   24 | #include <stdint.h>
      | ^
mbedtls/programs/test/../../include/psa/crypto_struct.h:54:1: note: extern "C" language linkage specification begins here
   54 | extern "C" {
      | ^
In file included from mbedtls/programs/test/cpp_dummy_build.cpp:31:
In file included from mbedtls/programs/test/../../include/mbedtls/debug.h:15:
In file included from mbedtls/programs/test/../../include/mbedtls/ssl.h:20:
In file included from mbedtls/programs/test/../../include/mbedtls/ssl_ciphersuites.h:16:
In file included from mbedtls/programs/test/../../include/mbedtls/pk.h:32:
In file included from mbedtls/programs/test/../../include/psa/crypto.h:71:
In file included from mbedtls/programs/test/../../include/psa/crypto_struct.h:68:
In file included from mbedtls/programs/test/../../include/psa/crypto_driver_contexts_primitives.h:27:
In file included from mbedtls/programs/test/../../include/psa/crypto_builtin_primitives.h:34:
mbedtls/programs/test/../../include/mbedtls/sha256.h:20:1: error: import of C++ module '_Builtin_stdint' appears within extern "C" language linkage specification [-Wmodule-import-in-extern-c]
   20 | #include <stdint.h>
      | ^
mbedtls/programs/test/../../include/psa/crypto_struct.h:54:1: note: extern "C" language linkage specification begins here
   54 | extern "C" {
      | ^
In file included from mbedtls/programs/test/cpp_dummy_build.cpp:31:
In file included from mbedtls/programs/test/../../include/mbedtls/debug.h:15:
In file included from mbedtls/programs/test/../../include/mbedtls/ssl.h:20:
In file included from mbedtls/programs/test/../../include/mbedtls/ssl_ciphersuites.h:16:
In file included from mbedtls/programs/test/../../include/mbedtls/pk.h:32:
In file included from mbedtls/programs/test/../../include/psa/crypto.h:71:
In file included from mbedtls/programs/test/../../include/psa/crypto_struct.h:68:
In file included from mbedtls/programs/test/../../include/psa/crypto_driver_contexts_primitives.h:27:
In file included from mbedtls/programs/test/../../include/psa/crypto_builtin_primitives.h:35:
mbedtls/programs/test/../../include/mbedtls/sha512.h:19:1: error: import of C++ module '_Builtin_stdint' appears within extern "C" language linkage specification [-Wmodule-import-in-extern-c]
   19 | #include <stdint.h>
      | ^
mbedtls/programs/test/../../include/psa/crypto_struct.h:54:1: note: extern "C" language linkage specification begins here
   54 | extern "C" {
      | ^
In file included from mbedtls/programs/test/cpp_dummy_build.cpp:31:
In file included from mbedtls/programs/test/../../include/mbedtls/debug.h:15:
In file included from mbedtls/programs/test/../../include/mbedtls/ssl.h:20:
In file included from mbedtls/programs/test/../../include/mbedtls/ssl_ciphersuites.h:16:
In file included from mbedtls/programs/test/../../include/mbedtls/pk.h:32:
In file included from mbedtls/programs/test/../../include/psa/crypto.h:71:
In file included from mbedtls/programs/test/../../include/psa/crypto_struct.h:68:
In file included from mbedtls/programs/test/../../include/psa/crypto_driver_contexts_primitives.h:27:
In file included from mbedtls/programs/test/../../include/psa/crypto_builtin_primitives.h:36:
mbedtls/programs/test/../../include/mbedtls/sha3.h:22:1: error: import of C++ module '_Builtin_stdint' appears within extern "C" language linkage specification [-Wmodule-import-in-extern-c]
   22 | #include <stdint.h>
      | ^
mbedtls/programs/test/../../include/psa/crypto_struct.h:54:1: note: extern "C" language linkage specification begins here
   54 | extern "C" {
      | ^
In file included from mbedtls/programs/test/cpp_dummy_build.cpp:31:
In file included from mbedtls/programs/test/../../include/mbedtls/debug.h:15:
In file included from mbedtls/programs/test/../../include/mbedtls/ssl.h:20:
In file included from mbedtls/programs/test/../../include/mbedtls/ssl_ciphersuites.h:16:
In file included from mbedtls/programs/test/../../include/mbedtls/pk.h:32:
In file included from mbedtls/programs/test/../../include/psa/crypto.h:71:
In file included from mbedtls/programs/test/../../include/psa/crypto_struct.h:129:
In file included from mbedtls/programs/test/../../include/psa/crypto_driver_contexts_composites.h:28:
In file included from mbedtls/programs/test/../../include/psa/crypto_builtin_composites.h:29:
mbedtls/programs/test/../../include/mbedtls/gcm.h:31:1: error: import of C++ module '_Builtin_stdint' appears within extern "C" language linkage specification [-Wmodule-import-in-extern-c]
   31 | #include <stdint.h>
      | ^
mbedtls/programs/test/../../include/psa/crypto_struct.h:54:1: note: extern "C" language linkage specification begins here
   54 | extern "C" {
      | ^
11 errors generated.
make[2]: *** [programs/test/CMakeFiles/cpp_dummy_build.dir/build.make:80: programs/test/CMakeFiles/cpp_dummy_build.dir/cpp_dummy_build.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:2975: programs/test/CMakeFiles/cpp_dummy_build.dir/all] Error 2
make: *** [Makefile:146: all] Error 2

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcomponent-platformPortability layer and build scriptssize-sEstimated task size: small (~2d)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions