Thread: Static libraries and template functions

  1. #1
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163

    Static libraries and template functions

    Some of you may know the stack library I started developing for fun. I realized the C++ part was very poor so I decided to enchant it with template functions, I wrote them and everything compiled fine. The problem comes up when linking a project with the library (yes, I include the stack.hpp header), it says that there's no, for example, stack::push<int>(int) functions for linking but when I include the full source code it compiles&runs with no errors.
    My question is how do I create static libraries with template functions?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The entire template implementation needs to be in a header file when using templates.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    Ahh.. so there's no way..

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, at least not if you:
    1. Want to be able to compile it with most common compilers.
    2. Do not want to write EVERY implementation that you could possibly conceive - this is really what prevents it from linking is that the way the templates work is that the code is generated as needed by the compiler. If you actually write
    Code:
    stack::push<int>(int) 
    {
      ... // Code that pushes an integer onto a stack<int>
    }
    and every other stack operation for every other type that you use, then you could compile that into a librar that is not part of a header file.

    But the whole point of using templates is generally that you do not WANT to write every variant of every function that you have templates for.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    Okay, thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. Borland, libraries, and template member functions
    By roktsyntst in forum C++ Programming
    Replies: 0
    Last Post: 06-01-2003, 09:52 PM

Tags for this Thread