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. templates in a static library?

templates in a static library?

Scheduled Pinned Locked Moved C / C++ / MFC
questionwpfhelp
7 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.
  • R Offline
    R Offline
    Robert Buldoc
    wrote on last edited by
    #1

    Hi! I am writting a static library. I noticed it does not export any of the templated functions. I have been looking all over MSDN to see if there is any issue with static library and templates but nothing so far. So the question is, is it possible to use a template function in a statically linked library? Thanks for your answers, Rob

    J 1 Reply Last reply
    0
    • R Robert Buldoc

      Hi! I am writting a static library. I noticed it does not export any of the templated functions. I have been looking all over MSDN to see if there is any issue with static library and templates but nothing so far. So the question is, is it possible to use a template function in a statically linked library? Thanks for your answers, Rob

      J Offline
      J Offline
      John M Drescher
      wrote on last edited by
      #2

      Robert Buldoc wrote: So the question is, is it possible to use a template function in a statically linked library? Yes. Remember that for this to work all of the implementation of the template must be in the header file. John

      S 1 Reply Last reply
      0
      • J John M Drescher

        Robert Buldoc wrote: So the question is, is it possible to use a template function in a statically linked library? Yes. Remember that for this to work all of the implementation of the template must be in the header file. John

        S Offline
        S Offline
        Steen Krogsgaard
        wrote on last edited by
        #3

        Well, if the template functions is implemented in the header file they are not really located in the library. The code generated by the compiler will be in the client of the library, not in the library. It cannot be any other way as the template functions are compiled when they are used. The library can't have any prior knowledge of which specializations of the template will be used. The only way to export template functions (that I know of) is to explicit instantiate the functions in the library. See MSDN Q239436 for an explanation. However, if you explicit instatiate template member functions on VC++ 6.0 you get an internal compiler error! br Steen Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

        J 1 Reply Last reply
        0
        • S Steen Krogsgaard

          Well, if the template functions is implemented in the header file they are not really located in the library. The code generated by the compiler will be in the client of the library, not in the library. It cannot be any other way as the template functions are compiled when they are used. The library can't have any prior knowledge of which specializations of the template will be used. The only way to export template functions (that I know of) is to explicit instantiate the functions in the library. See MSDN Q239436 for an explanation. However, if you explicit instatiate template member functions on VC++ 6.0 you get an internal compiler error! br Steen Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

          J Offline
          J Offline
          John M Drescher
          wrote on last edited by
          #4

          Steen Krogsgaard wrote: The only way to export template functions (that I know of) is to explicit instantiate the functions in the library. Putting template functions in a library is the same as putting template source code in a .cpp. Unless you explicitlty instantiate it you can not use it outside of the .cpp file it is defined. That is why in most cases you must put it in the header file. See the following link: http://www.codeproject.com/cpp/templatesourceorg.asp[^] Steen Krogsgaard wrote: However, if you explicit instatiate template member functions on VC++ 6.0 you get an internal compiler error! I believe I have done this with dlls but most of the time I put all the header source code in the header file because I can not possibly explicitly instantiate all possible combinations of things I will want to use with the template. John

          S 1 Reply Last reply
          0
          • J John M Drescher

            Steen Krogsgaard wrote: The only way to export template functions (that I know of) is to explicit instantiate the functions in the library. Putting template functions in a library is the same as putting template source code in a .cpp. Unless you explicitlty instantiate it you can not use it outside of the .cpp file it is defined. That is why in most cases you must put it in the header file. See the following link: http://www.codeproject.com/cpp/templatesourceorg.asp[^] Steen Krogsgaard wrote: However, if you explicit instatiate template member functions on VC++ 6.0 you get an internal compiler error! I believe I have done this with dlls but most of the time I put all the header source code in the header file because I can not possibly explicitly instantiate all possible combinations of things I will want to use with the template. John

            S Offline
            S Offline
            Steen Krogsgaard
            wrote on last edited by
            #5

            I think we agree. I just don't see the sense of putting the template definition/implementation in a dll library if it is never being instantiated in the library. And I agree that explicit instantiations goes against the whole idea with templates. The internal compiler error (C1001) is confirmed by MS and documented in Q179271. It happens when you have more than one explicit instantiation (which must be quite common, otherwise there is no reason to use templates in the first place :-) ) Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

            J 1 Reply Last reply
            0
            • S Steen Krogsgaard

              I think we agree. I just don't see the sense of putting the template definition/implementation in a dll library if it is never being instantiated in the library. And I agree that explicit instantiations goes against the whole idea with templates. The internal compiler error (C1001) is confirmed by MS and documented in Q179271. It happens when you have more than one explicit instantiation (which must be quite common, otherwise there is no reason to use templates in the first place :-) ) Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

              J Offline
              J Offline
              John M Drescher
              wrote on last edited by
              #6

              Steen Krogsgaard wrote: I just don't see the sense of putting the template definition/implementation in a dll library if it is never being instantiated in the library. I do this because I package all my dlls with their own installer and it makes it easier to use and keep only one version of the templates if they are packaged with related classes. In general the classes of the dll may use the templates internally but for the most part they don't use them externally. Steen Krogsgaard wrote: The internal compiler error (C1001) is confirmed by MS and documented in Q179271. I'll take a look at that. I find it hard to believe that I don't have more than one explicit instantiation somewhere in my dll interface code but its possible. John

              S 1 Reply Last reply
              0
              • J John M Drescher

                Steen Krogsgaard wrote: I just don't see the sense of putting the template definition/implementation in a dll library if it is never being instantiated in the library. I do this because I package all my dlls with their own installer and it makes it easier to use and keep only one version of the templates if they are packaged with related classes. In general the classes of the dll may use the templates internally but for the most part they don't use them externally. Steen Krogsgaard wrote: The internal compiler error (C1001) is confirmed by MS and documented in Q179271. I'll take a look at that. I find it hard to believe that I don't have more than one explicit instantiation somewhere in my dll interface code but its possible. John

                S Offline
                S Offline
                Steen Krogsgaard
                wrote on last edited by
                #7

                John M. Drescher wrote: I'll take a look at that. I find it hard to believe that I don't have more than one explicit instantiation somewhere in my dll interface code but its possible. It's only a problem in VC++ 5.0 and 6.0, the problem was corrected in VC++.NET (mentioned in Q179271 available online. The one I quoted was from my October 2001 MSDN CDs). Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

                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