Modules TS and templates

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Modules TS and templates

Xin Wang via cfe-dev
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
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Modules TS and templates

Xin Wang via cfe-dev
Emily Maier writes:

> 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?

My guess would be this is not implemented yet. If you replace bar with
an inline function (which doesn't change anything from the module name
visibility POV), then it links:

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


> export module main;
>
> import foo;
>
> int main(int argc, char** argv) {
> [...]

So you've put main() into a module's purview. Since it is not exported,
it will have module linkage which means the symbol will most likely
end up decorated and not what the runtime expects. It works with Clang
because currently it exports everything (according to Richard).

Just me test-driving the newly-acquired C++ modules knowledge ;-).

Boris
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Loading...