Exporting from a Static Library

Posted on 2003-11-12
Last Modified: 2013-12-14
Here's the situation: I'm using Visual C++ 6.0 on a Win32 Static Library Project.  I have a few classes inside it and one template class.  How would it be possible to export them all through the library? I've been researching for a couple days now but everything I found applied to dynamic libraries and not static libraries.  The template class which is used by one of the classes in the library is only needed for char type so only that version of it needs to be exported.  Also, is it possible to put these classes into a namespace?  I haven't really tried that yet but since I'm posting here, the best programming resource I've ever seen so far, I figure you guys would know already.
Question by:Xerpher
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3

Expert Comment

ID: 9737376
You want it to compile the static lib in and not have the program auto-load the dll from compiling with the .lib?  Why don't you create a dll project and use the .lib it creates for you?

Also, the namespace problem can easily be solved by the header you use in the source from which you are calling the functions in the library.
LVL 15

Expert Comment

ID: 9738283
For the classes, there's not much to it.  You just have to build the library and set the client project to link it in.  You don't have to mess with __declspecs like you do with a DLL.

I don't think you can export a template, though.  You can wrap the one instantiation you need in another class, but for the client to use it as a template, the compiler will want to see the source code.  So you could export the template at the source level.

And yes, it should be possible to put the classes in a namespace.


Author Comment

ID: 9739019
@ _corey_
  If I did that then I would need to send out the dll with the program, right? I don't want that.

@ efn
  So I need to make a big header file with all everything I want exported and put it in the program project?  So there's no solution without having to make a big header file?... and that template class is only needed for one of the classes inside the library, so how would I wrap the one instantiation I need in another class?
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 15

Accepted Solution

efn earned 50 total points
ID: 9741536
> So I need to make a big header file with all everything I want exported and put it in the program project?  So there's no solution without having to make a big header file?

No, you don't have to make a big header file.  You do somehow have to get the classes you want to use declared in the software that will use them.  It's a good idea to use header files, because then you can have the declaration in just one place.  But if you want just to put the declarations in the client code, that will work, too--the compiler doesn't care, it's just worse for maintenance.  And you don't have to use one big header file for the whole library.  You can use multiple smaller files, such as one for each class.

>  and that template class is only needed for one of the classes inside the library, so how would I wrap the one instantiation I need in another class?

Declare a wrapper class in a header file that does not mention the template class.  Use the template class only in the implementation of the wrapper class.  The wrapper class can have the same interface and use the template class to implement it.

This will work if each function can temporarily conjure up a template object to do its job and then get rid of it.  However, you may need a more permanent template object corresponding to the wrapper object.  In this case, you can use the "pimpl idiom."  "pimpl" is short for "pointer to implementation."  The way it works is that you have two wrapper classes, say "Wrapper" and "WrapperImpl."  In the header file for Wrapper, you just declare WrapperImpl as a class, don't #include its header file.  Wrapper has all the interface functions and a private member that is a pointer to a WrapperImpl.  The Wrapper constructor dynamically allocates a WrapperImpl and saves its address in the pointer member.  All the functions of Wrapper just call corresponding functions in WrapperImpl to get their jobs done.

Then Wrapper has no reference to the template, so it can be exported from the library, and WrapperImpl, not being exported, can use the template freely, including having a template object as a member.


Author Comment

ID: 9743149
So theres no way to use the classes in the lib without having to redeclare them in the project that uses the lib?  I guess thats more of what I was hoping for.

If not I'll try that "pimpl" you suggested.
LVL 15

Expert Comment

ID: 9743179
I'm not sure what you mean by "redeclare."  The compiler has to see a declaration of a class for code to use the class, whether you are using a library or not.  The usual approach is to have one header file that declares a class, included in both the client code and the class implementation code.


Author Comment

ID: 9743295
Yea... by " without having to redeclare " I meant just link to the library and start using whats in it :-p

Alright, thanks.

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question