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