I’ve got a C++ template class split into two files using #include directive as suggested in this link –> Templates spread across multiple files
For both files I’ve used the standard naming *.h and *.cpp.
In the makefile I’ve included the *.h one since – as I understand it – both files split in such way are in fact one from the compiler point of view. I’ve set the same compilation options as in case of compiling a ususal *.cpp file.
However, when I try to compile it through makefile i get the following error:
Stos.o: file not recognized: File format not recognized
collect2: ld returned 1 exit status
make: *** [program] Error 1
I’ve read somewhere that it might be due to the fact that I compile *.h file. I do not know what should I do instead.
Thank you for your help.
EDIT
I am posting the makefile itself to make matter more clear. Please, excuse me that the names are not in English, however in this case this shouldn’t be a problem. I’ve updated the error message so that it fits the makefile.
Because of suggestions here, I’ve already changed names for template files from *.h and *.cpp mentioned above to *.hpp and *.tpp.
CC=g++
CFLAGS=-c -Wall -pedantic -W
COMP= $(CC) $(CFLAGS)
program: Graf.o wgraf.o wnazwe.o menu.o Stos.o TabDyn.o komunikaty.o main.o
$(CC) Graf.o wgraf.o wnazwe.o menu.o Stos.o TabDyn.o komunikaty.o main.o -o program
Graf.o: graf_struktura/Graf.cpp graf_struktura/Graf.h lifo/TabDyn.hpp lifo/Stos.hpp
$(COMP) graf_struktura/Graf.cpp -o $@
wgraf.o: wczytywanie_grafu/wczytaj_graf.cpp wczytywanie_grafu/wczytaj_graf.h graf_struktura/Graf.h lifo/Stos.hpp
$(COMP) wczytywanie_grafu/wczytaj_graf.cpp -o $@
wnazwe.o: wczytywanie_grafu/wczytaj_nazwe_pliku.cpp wczytywanie_grafu/wczytaj_nazwe_pliku.h wczytywanie_grafu/komunikaty.cpp
$(COMP) wczytywanie_grafu/wczytaj_nazwe_pliku.cpp -o $@
komunikaty.o: wczytywanie_grafu/komunikaty.cpp
$(COMP) wczytywanie_grafu/komunikaty.cpp
menu.o: menu/menu.cpp graf_struktura/Graf.h
$(COMP) menu/menu.cpp
main.o: main.cpp wczytywanie_grafu/wczytaj_nazwe_pliku.h wczytywanie_grafu/wczytaj_graf.h menu/menu.cpp graf_struktura/Graf.h
$(COMP) main.cpp
#Here is the issue.
Stos.o: lifo/Stos.hpp lifo/Stos.tpp lifo/TabDyn.hpp
$(COMP) lifo/Stos.hpp -o $@
TabDyn.o: lifo/TabDyn.hpp lifo/TabDyn.tpp
$(COMP) lifo/TabDyn.hpp -o $@
Compiler actually creates "Stos.o" but fails to include it in "program" producing quoted error.
Hope that now my issue is more clear. Sorry if makefile is not elegant. I’m still a beginner at makefiles, please be understanding.
If your MyClass.h looks like
And the MyClass.tcc file looks like
Then code that uses the template, say in a file called main.cpp, looks like:
And the Makefile could look like:
The makefile should not compile the template header, it compiles the main.cpp file into a .o, and lists the headers as dependencies. If either of the headers or
main.cppchanges thenmain.owill be recompiled. Don’t compile the headers though.FWIW, in my own code I use
.ccfor source files,.hfor headers and.tccfor inline definitions. GCC knows that.tccis a header file and will treat it correctly (as a header) if you sayg++ foo.tcc, see http://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html for the file extensions GCC recognises.N.B. rather than manually listing all the header dependencies you can use GCC to produce makefile fragments listing the dependencies, e.g. for
main.cppyou would generate amain.dand in the makefile doinclude main.d… that’s beyond the scope of this answer though.