Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 626
  • Last Modified:

Exporting from a Static Library

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
Xerpher
Asked:
Xerpher
  • 3
  • 3
1 Solution
 
_corey_Commented:
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
 
efnCommented:
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
 
XerpherAuthor Commented:
@ _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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
efnCommented:
> 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
 
XerpherAuthor Commented:
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
 
efnCommented:
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
 
XerpherAuthor Commented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now