MBR-6161
MBR-6161

Reputation: 164

Clang can't find C++ module with `-fprebuilt-module-path`

I have two files, main.cc and S.cc, in the same directory. S.cc contains a module named S_mod. When compiling I get an error about S_mod not being found despite using -fprebuilt-module-path=.. I've tried creating a separate directory for the *.pcm files but that had no effect.

main.cc:

import S_mod;

int main() {
    return 0;
}

S.cc:

export module S_mod;

export struct S {
    int x, y;
};
clang++ -std=c++20 -fmodules-ts -fprebuilt-module-path=. --precompile -x c++-module S.cc -o S.pcm
clang++ -std=c++20 -fmodules-ts -fprebuilt-module-path=. -c S.pcm -o S.o
clang++ -std=c++20 -fmodules-ts -fprebuilt-module-path=. S.o main.cc -o test
main.cc:3:8: fatal error: module 'S_mod' not found
import S_mod;
~~~~~~~^~~~~
1 error generated.

Upvotes: 2

Views: 1943

Answers (1)

wkl
wkl

Reputation: 80031

For the following, I added some output to your main.cc so it looks like this, to demonstrate it's all working.

import S_mod;

#include <iostream>

int main() {
    S s = { 1, 2 };
    std::cout << s.x << " " << s.y << std::endl;
    return 0;
}

You need to make sure all the names of your module artifacts are correct. For you, main.cc expects to find S_mod, and by default, Clang expects to find something named S_mod.pcm for compilation.

So, for example, this set of commands would work correctly because you'd be outputting a .pcm file with the same name as the module you're trying to import.

clang++ -std=c++20 -fmodules-ts --precompile -x c++-module S.cc -o S_mod.pcm
clang++ -std=c++20 -fmodules-ts -c S_mod.pcm -o S_mod.o -fprebuilt-module-path=.
clang++ -std=c++20 -fmodules-ts -fprebuilt-module-path=. main.cc -o test
./test
1 2

Alternatively, you can specify -fmodule-file which allows you to specify the mapping of module_name=module_file.

clang++ -std=c++20 -fmodules-ts --precompile -x c++-module S.cc -o S.pcm
clang++ -std=c++20 -fmodules-ts -c S.pcm -o S.o

# Notice that we map S_mod=S.pcm - you could just say S.pcm but 
# then clang will always load the module file regardless of whether
# or not it needs to
clang++ -std=c++20 -fmodules-ts -fprebuilt-module-path=. -fmodule-file="S_mod=S.pcm" main.cc -o test
./test
1 2

Upvotes: 3

Related Questions