error: header file <iostream> (aka '/usr/local/Cellar/llvm/18.1.8/bin/../include/c++/v1/iostream') cannot be imported because it is not known to be a header unit
It can’t, unless the distro ships precompiled header units correctly. Many distros still don’t, e.g. ArchLinux doesn’t. Here’s an example (hello.c++):
import <iostream>;
int main() { std::cout << "hello world\n"; }
$ clang++ -std=c++23 -fmodules hello.c++ -o hello
hello.c++:1:8: error: header file <iostream> (aka '/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../include/c++/14.2.1/iostream') cannot be imported because it is not known to be a header unit
1 | import <iostream>;
| ^
hello.c++:2:14: error: use of undeclared identifier 'std'
2 | int main() { std::cout << "hello world\n"; }
| ^
2 errors generated.
Not good. However, if we first precompile the header unit manually, as documented here, then it will work just fine:
The problem is that (1) many of these command-line flags may be clang++-specific and (2) (re)building of standard C++ header units again for each project is not a great approach. Distros should make sure that .pcm files are available out-of-the-box.