[cfe-dev] Modules TS and templates

Emily Maier via cfe-dev cfe-dev at lists.llvm.org
Tue Jul 4 09:19:50 PDT 2017


Hi all,

I've been trying out Clang's support for the modules TS and run into a 
snag trying to get templates to work. Here are my test files:

bar.cppm:
export module bar;

#include <iostream>

export template<int index> void bar() {
  std::cout << index << " from bar!" << std::endl;
}



foo.cppm:
export module foo;

import bar;

export template<int index> void foo() {
  bar<index>();
}



main.cppm:
export module main;

import foo;

int main(int argc, char** argv) {
  foo<3>();
  foo<5>();
}

and the build commands:
clang++ -std=gnu++1z -fmodules-ts -Xclang -fmodules-codegen -stdlib=libc++ --precompile -o bar.pcm -c bar.cppm
clang++ -std=gnu++1z -fmodules-ts -Xclang -fmodules-codegen -stdlib=libc++ --precompile -fmodule-file=bar.pcm -o foo.pcm -c foo.cppm
clang++ -std=gnu++1z -fmodules-ts -Xclang -fmodules-codegen -stdlib=libc++ --precompile -fmodule-file=bar.pcm -fmodule-file=foo.pcm -o main.pcm -c main.cppm
clang++ -std=gnu++1z -fmodules-ts -Xclang -fmodules-codegen -stdlib=libc++ -o test main.pcm foo.pcm bar.pcm

All goes well until the final step:
/tmp/main-57ffba.o: In function `void foo<3>()':
main.pcm:(.text._Z3fooILi3EEvv[_Z3fooILi3EEvv]+0x5): undefined reference to `void bar<3>()'
/tmp/main-57ffba.o: In function `void foo<5>()':
main.pcm:(.text._Z3fooILi5EEvv[_Z3fooILi5EEvv]+0x5): undefined reference to `void bar<5>()'
clang-5.0: error: linker command failed with exit code 1 (use -v to see invocation)


Am I missing something? If I add an "import bar;" statement to main.cppm 
it compiles and works fine. Shouldn't Clang be able to walk the imports 
and instantiate the bar template it needs?

Emily Maier



More information about the cfe-dev mailing list