1

I have a .h and .cpp file

Foo.h

template<typename T>
class Foo {
public:
  void g();
  void h();
};

Foo.cpp

#include <iostream>
#include "Foo.h"
template<typename T>
void Foo<T>::g()
{
  std::cout << "Foo<T>::g()\n";
}
template<typename T>
void Foo<T>::h()
{
  std::cout << "Foo<T>::h()\n";
}

The above two files are in abc/def directory and I have the implementation in a different cpp file

Foo-impl.cpp

#include <../../Foo.cpp>
template class Foo<int>;

This Foo-impl.cpp is in abc/ghi/jkl directory.

But when I use #include <abc/def/Foo.cpp I get an error saying that the file doesn't exist. But when I use #include <abc/def/Foo.h>, I don't get file doesn't present erro (the file is identified by the compiler) but I get linker error.

I am using the implementation as per link

Why does it work with <../../Foo.cpp> and why not with <abc/def/Foo.cpp. How should I make it work ?

  • Did you try to define the template in the header? It will help to avoid complications such as linker errors. – vahancho Jul 14 '20 at 13:06
  • https://isocpp.org/wiki/faq/templates#templates-defn-vs-decl – alex_noname Jul 14 '20 at 13:09
  • Don't ever include one cpp file in another. The point about cpp files is that they get compiled separately, the point about header files is that they get compiled multiple times. Even if you make it work anything else is just abuse of the C/C++ build model. – john Jul 14 '20 at 13:17
  • I can't include in the head file or in the same cpp file as this is abstracted from the end user. This class template accepts different other class andso providing a way to the clients to have different implementations. – Vinodini Natrajan Jul 14 '20 at 13:19
  • @Yksisarvinen Yes it is there. That's why if I include .h file, it doesn't complain about the file not present. – Vinodini Natrajan Jul 14 '20 at 13:21
  • 3
    This is an entirely wrong duplicate. The question deals with explicit instantiation. The standard sanctioned technique for **not implementing templates entirely in the header**. Furthermore, the question is about *tooling* not seeing the file being included. – StoryTeller - Unslander Monica Jul 14 '20 at 13:24
  • @john Why is that so. What I am doing is a recommended technique as per isocpp.org - https://isocpp.org/wiki/faq/templates#separate-template-class-defn-from-decl – Vinodini Natrajan Jul 14 '20 at 13:44
  • @VinodiniNatrajan That recommendation is specifically for the case when you *cannot modify the original cpp file*. – john Jul 14 '20 at 14:00
  • Yes, Being an end user, I can't modify the .h or .cpp files andso doing it this way – Vinodini Natrajan Jul 14 '20 at 14:02
  • OK, then your question is essentailly about your compiler not begin able to find the other cpp file. That's just about where the files are and the compiler options that you are using. Nothing to do with templates. It would be exactly the same problem if your compiler couldn't find a normal header file. – john Jul 14 '20 at 14:03
  • You should probably read your compiler documentation to find out how it looks up `#include` files. Different compilers do this differently, so it's hard to give specific advice. One piece of advice though is to use `#include "..."` not `#include <...>`. Generally `<>` is used for system files not for user files. – john Jul 14 '20 at 14:08
  • @john I am using gcc. Could you help. I couldn't find the exact answering anywhere – Vinodini Natrajan Jul 14 '20 at 14:22
  • @VinodiniNatrajan I'm not familair with gcc, but you say that you have `abc/def/Foo.cpp` and `abc/ghi/jkl/Foo-impl.cpp` so I would try `#include "../../def/Foo.cpp"`. But as I say, not really familar with gcc. – john Jul 14 '20 at 15:06

0 Answers0