Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. A link problem with c++ template function

A link problem with c++ template function

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cold_Fearing_Bird
    wrote on last edited by
    #1

    template< size_t N >
    void cdecl _DumpWin32StructFields( WCHAR (&szBuffer)[N], LPCWSTR pszFormat, ... )
    {
    va_list pFirstParam ;
    va_start( pFirstParam, pszFormat ) ;
    StringCchVPrintf( szBuffer, N,pszFormat, pFirstParam ) ;
    va_end( pFirstParam ) ;
    }

    If I put the function definition in a different obj file than the caller, it fails to link reporting I didn't give definition to _DumpWin32StructFields, actually I do,in another obj, it's just too stupid for the linker to find out. Is there a solution if I don't put caller and the function together?

    _ S 2 Replies Last reply
    0
    • C Cold_Fearing_Bird

      template< size_t N >
      void cdecl _DumpWin32StructFields( WCHAR (&szBuffer)[N], LPCWSTR pszFormat, ... )
      {
      va_list pFirstParam ;
      va_start( pFirstParam, pszFormat ) ;
      StringCchVPrintf( szBuffer, N,pszFormat, pFirstParam ) ;
      va_end( pFirstParam ) ;
      }

      If I put the function definition in a different obj file than the caller, it fails to link reporting I didn't give definition to _DumpWin32StructFields, actually I do,in another obj, it's just too stupid for the linker to find out. Is there a solution if I don't put caller and the function together?

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      A template class or template function is resolved during compile time. Symbols in an OBJ file are resolved during link time. So to use templates, you have to provide template source access to the calling file. The problem you're facing is not because the linker is stupid. It's because it's the compiler that resolves templates and not the linker.

      «_Superman_»  _I love work. It gives me something to do between weekends.

      _Microsoft MVP (Visual C++)

      Polymorphism in C

      C 1 Reply Last reply
      0
      • _ _Superman_

        A template class or template function is resolved during compile time. Symbols in an OBJ file are resolved during link time. So to use templates, you have to provide template source access to the calling file. The problem you're facing is not because the linker is stupid. It's because it's the compiler that resolves templates and not the linker.

        «_Superman_»  _I love work. It gives me something to do between weekends.

        _Microsoft MVP (Visual C++)

        Polymorphism in C

        C Offline
        C Offline
        Cold_Fearing_Bird
        wrote on last edited by
        #3

        Thanks, I think you are right. I searched some materials, says the compiler won't embed the template definition( blueprint ) into an obj until it is called. So I simply put it in a common header file, but I am still worried if more than one cpp call the template with the same function prototype.

        1 Reply Last reply
        0
        • C Cold_Fearing_Bird

          template< size_t N >
          void cdecl _DumpWin32StructFields( WCHAR (&szBuffer)[N], LPCWSTR pszFormat, ... )
          {
          va_list pFirstParam ;
          va_start( pFirstParam, pszFormat ) ;
          StringCchVPrintf( szBuffer, N,pszFormat, pFirstParam ) ;
          va_end( pFirstParam ) ;
          }

          If I put the function definition in a different obj file than the caller, it fails to link reporting I didn't give definition to _DumpWin32StructFields, actually I do,in another obj, it's just too stupid for the linker to find out. Is there a solution if I don't put caller and the function together?

          S Offline
          S Offline
          Stefan_Lang
          wrote on last edited by
          #4

          If you want to use a function from different cpp files you have to make the declaration (the function signature) available to both. In the case of a template function, the same goes for its definition! To avoid code duplication, the right place to put this is a header file, not a cpp file.

          // file dump.hpp
          template< size_t N >
          void cdecl _DumpWin32StructFields( WCHAR (&szBuffer)[N], LPCWSTR pszFormat, ... )
          {
          va_list pFirstParam ;
          va_start( pFirstParam, pszFormat ) ;
          StringCchVPrintf( szBuffer, N,pszFormat, pFirstParam ) ;
          va_end( pFirstParam ) ;
          }

          // file foo.cpp
          #include "dump.hpp"
          void foo() {
          WCHAR buffer[20];
          _DumpWin32StructFields(buffer, _T("Hello World!"));
          }

          // file bar.cpp
          #include "dump.hpp"
          void bar() {
          WCHAR buffer[30];
          _DunpWin32StructFields(buffer, _T("Goodbye Cruel World!"));
          }

          This will force the compiler to instantiate and create code for your template function for both foo.obj and bar.obj, and the linker won't have trouble finding them.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups