vendredi 28 octobre 2016

MPICH issue with explicit instantiation(ignoring extern)

I wrote a simple c++ program using explicit instantiation technique as below:

// foo.hpp
#include <iostream>

struct foo
{
  template <typename Arg>
  static void call(Arg arg)
  {
    std::cout << "foo\n";
  }
};

here I have the explicit instantiation of class foo:

// foo.cc
#include "foo.hpp"
template void foo::call(int);

and in the main file I use the extern keyword to tell the compiler the foo::call is already instantiated, so there is no need to compile it again:

// main.cc
#include <iostream>
#include "foo.hpp"

extern template void foo::call(int);

int main(int argc, char const *argv[])
{
  foo::call(1);

  return 0;
}  

I test the program with g++ and mpic++ using gcc-4.9. For g++ when I pass the foo.o it works fine:

g++ -std=c++11 -c foo.cc
g++ -std=c++11 main.cc foo.o -o main

and when I don't pass the foo.o it complains as expected:

g++ -std=c++11 test_simple.cc -o test_simple 
/tmp/ccclcKnc.o: In function `main':
test_simple.cc:(.text+0x15): undefined reference to `void foo::call<int>      
(int)'
collect2: error: ld returned 1 exit status

but when I compile with mpic++ (MPICH) in both cases(either passing or not passing the foo.o) the program compiles which should complain when the foo.o is not passed.

mpic++ -std=c++11 -c foo.cc
mpic++ -std=c++11 main.cc foo.o -o main

or

mpic++ -std=c++11 main.cc -o main // this line shouldn't compile but it does

I tested the code with OpenMPI and the behavior is same as g++. So the question is why the MPICH ignores the extern and compiles the instantiation again.

Thanks,

Aucun commentaire:

Enregistrer un commentaire