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:
- Modules are actually supported
- What is the right way to compile the code I've shown?