Solved

Exporting from a Static Library

Posted on 2003-11-12
7
608 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now