1

I have a template class of a binary tree, tree.tpp and tree.h. I have done a test with the class but I can't compile it

#include <cstdlib>
#include <iostream>
#include "arbolbinario.h"

 using namespace std;

int main(int argc, char** argv) {
    ArbolBinario<int> pila(45);


    return 0;
}

And I'm having the following error when I do: g++ -c -o ./tree.o ./tree.tpp -I ./tree.h

g++: warning: ./tree.tpp: linker input file unused because linking not done

(I'm working with netbeans)

Kernel
  • 107
  • 1
  • 10
  • I have added an important "However" part to my answer that I think is relevant to you. (You did not show a full [repro], so I can not say for sure). Please read that as well. – walnut Dec 30 '19 at 01:03

2 Answers2

3

.tpp is not one of the file endings recognized by g++, see its documentation.

Therefore g++ assumes that you want the file to be passed to the linker directly. But since you used the -c flag, which indicates that you want g++ to only compile, but not invoke the linker, you get that error message.

The solution is to tell g++ what kind of file it is that you are passing it explicitly:

g++ -c -o ./tree.o -x c++ ./tree.tpp -I ./tree.h

or, better, rename your C++ source file use one of the common file endings for C++ source files, e.g. .cpp, .cc, .cxx, etc. That would result in less trouble with build tools and less confusion for others looking at your project.


As noted in the question comments -I ./tree.h also is clearly wrong, but not the cause of this particular error (and probably it just doesn't belong there at all).


However:

If your .tpp contains the implementation of methods of a class template, then you should not rename it (.tpp is appropriate in that case), but you also should not compile it as translation unit at all. That means it should not appear in any g++ command.

.tpp files implementing a template classes methods need to be included in the .h file with the class template definition, instead. Otherwise you will get linker errors later when you try to link your files, see Why can templates only be implemented in the header file?.

walnut
  • 21,629
  • 4
  • 23
  • 59
2

The -c flag tells GCC to only compile the input source files, not to do any linking.

If you want the compiler to link the object files into an executable binary, you need to remove the -c flag.

Michael Jarrett
  • 425
  • 3
  • 10