Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7963595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:38:35+00:00 2026-06-04T05:38:35+00:00

I’ve got a C++ template class split into two files using #include directive as

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:38:37+00:00Added an answer on June 4, 2026 at 5:38 am

    If your MyClass.h looks like

    #ifndef MYCLASS_H
    #define MYCLASS_H
    template<typename T>
      struct MyClass
      {
        void f();
      };
    #include "MyClass.tcc"
    #endif
    

    And the MyClass.tcc file looks like

    template<typename T>
      void
      MyClass::f(T)
      { }
    

    Then code that uses the template, say in a file called main.cpp, looks like:

    #include "MyClass.h"
    
    int main()
    {
      MyClass<int> m;
      m.f(1);
    }
    

    And the Makefile could look like:

    main: main.o
        $(CXX) $^ $(LDFLAGS) -o $@
    
    main.o: main.cpp MyClass.h MyClass.tcc
        $(CXX) $(CXXFLAGS) $< -o $@
    

    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.cpp changes then main.o will be recompiled. Don’t compile the headers though.

    FWIW, in my own code I use .cc for source files, .h for headers and .tcc for inline definitions. GCC knows that .tcc is a header file and will treat it correctly (as a header) if you say g++ 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.cpp you would generate a main.d and in the makefile do include main.d … that’s beyond the scope of this answer though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.