Skip to main content
gamedev.net
🔒 Locked

Static Library

Started by dave Jan 19, 2005 at 4:39 AM 2 replies 400+ views
Original Post
dave
dave
Hi, I'm having problems using a static library. I have 2 classes to put in it and one of them is a template class. This is the one causing problems. I believe it is because it is a template class. 1. Is it possible to have a template class in a static library? 2. If not is this where DLLs come in? 3. How do STL do it? ace
nmi
nmi
Think of a template as some source code with placeholders, that the compiler fills out in place.

So when you use a template class, you not only have to provide the declaration, but also the definition. The compiler then will generate code that matches the template parameters.

If you want to put a template class inside a dll, you can put the declaration of the template in your header file. Then you provide some implementation for your template class and create template instantiations.

From the top of my head:
// header file#ifndef ...#define ...template <typename T>class Whatever {// declare the functionT somefunc();};#endif


// implemenation file#include "..." // header file include// define the functiontemplate <typename T>T Whatever<T>::somefunc(){...}// instantiate templateclass Whatever<int>;

dave
dave
Oh yeah i know that, sorry if i wasnt clear, i basically had the template class H and CPP files in the project. The main header had includes to add in the other header files and then i assumed the template CPP was built into the library.

But is this the same for Static libraries as DLLs?

ace
SiCrane
SiCrane
We've been over this before. If you have a template class or a class with template members, the implementation of the templated members have to go in the header. (Unless you explicitly instantiated the templates, but that only works for a limited number of types.)

So the answer to your question:

1. No.
2. No.
3. They stick the implementation of template members in the headers.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.