Solved

Exporting from a Static Library

Posted on 2003-11-12
7
610 Views
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.
0
Comment
Question by:Xerpher
  • 3
  • 3
7 Comments
 
LVL 8

Expert Comment

by:_corey_
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.
0
 
LVL 15

Expert Comment

by:efn
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.

--efn
0
 

Author Comment

by:Xerpher
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?
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 15

Accepted Solution

by:
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.

--efn
0
 

Author Comment

by:Xerpher
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.
0
 
LVL 15

Expert Comment

by:efn
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.

--efn
0
 

Author Comment

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

Alright, thanks.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 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