0

I was trying for sake of practice to compile a small module using clang (which is the compiler I have in hand at the moment).

My module code is:

//basic_math.cpp
export module basic.math; //Notice module name is not the same as filename

int mysum(int x, int y) {  // no export keyword implies module linkage
  return x + y;
}

export int add(int x, int y) { return mysum(x, y); }

And my main

import basic.math;
#include <iostream>

int main()
{
    auto res = add(3, 5);
    std::cout << res << '\n';
    return 0;
}

I've tried several options, mainly from chatgpt as I wanted to get it going quickly.

clang++ -std=c++20 -fmodules -fcxx-modules -nostdinc++ -isystem /opt/homebrew/opt/llvm/include/c++/v1 -x c++-module -c basic_math.cpp -o basic_math.pcm && clang++ -std=c++20 -fmodules -fcxx-modules -nostdinc++ -isystem /opt/homebrew/opt/llvm/include/c++/v1 -fmodule-file=basic_math.pcm -c main.cpp -o main.o && clang++ -std=c++20 -fmodules -fcxx-modules -nostdinc++ -isystem /opt/homebrew/opt/llvm/include/c++/v1 main.o -o main

The error I get is:

fatal error: file 'basic_math.pcm' is not a valid precompiled module file: file too small to contain AST file magic
1 error generated.

My clang version is

Homebrew clang version 20.1.4
Target: arm64-apple-darwin24.4.0
Thread model: posix
InstalledDir: /opt/homebrew/Cellar/llvm/20.1.4_1/bin
Configuration file: /opt/homebrew/etc/clang/arm64-apple-darwin24.cfg

which I thought supported modules. I've also tried gcc with several commands with no success.

I am starting to wonder if modules, despite being part of C++20, are actually supported properly by any compiler. So far I've tried gcc 14 and clang 20.

Can anyone tell me if:

  1. Modules are actually supported
  2. What is the right way to compile the code I've shown?
4
  • clang arm64-apple-darwin24.4.0 does not support modules. You might want to switch OS. Commented May 13 at 5:00
  • And where did you get such info? (like official documentation?). Commented May 13 at 5:02
  • Module support is not fully mature. At least try to use CMake, and watch import CMake; // Mastering C++ Modules - Bill Hoffman - CppCon 2024. Commented May 13 at 5:04
  • @PepijnKramer, thank you. That's very useful Commented May 13 at 5:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.