• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 527
  • Last Modified:

classes in a static library

I have some classes that could be used in more than one application. In order to resuse them I plan to put them in a static library. Is it simply a matter of creating a static library and using the appropriate header files to use them in other applications? Is there anything else I need to be aware of?
0
Mydeen Yussouf
Asked:
Mydeen Yussouf
  • 3
  • 2
1 Solution
 
Infinity08Commented:
That's the idea, yes.

There are many other things to be aware of (enough to write a book with so to speak), but given that basic premise, you should be able to get it working.

I'll expand a bit on it just to be sure :

The idea is to write a set of header files that contain the interface you'd like the library to implement. This interface is what other code will use by including those header file(s), and then making use of the functionality that the library makes available through the interface.

The rest of the library would be the implementation of that interface. This in itself generally consists of several source files (including header files) that will not be provided to the users of the library.

And finally : the way to build the static library binary, depends on the specific platform (incl. compiler/linker) you are working on. There should be documentation for your specific platform that explains the details of this.
0
 
theKashyapCommented:
Perhaps one thing out of "enough to write a book" you should be aware before you decide is that after linking the static lib becomes part of the client's binary. In other words the the static lib's size adds to the size of client binary.
If you have too many users and/or your static lib is big in size, consider using dynamic lin (dll on Win, so on POSIX) instead.
0
 
ambienceCommented:
"In other words the the static lib's size adds to the size of client binary." may not be true for every case. WPO (whole program optimization) as well as other kinds like profile-guided optimization work great when all the code resides in the same executable. There's no way to "optimize out" a function from a dll once it has been compiled, not to mention other types of optimizations possible.

This is not to say: dlls are useless!
0
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!

 
theKashyapCommented:
"In other words the the static lib's size adds to the size of client binary." may not be true for every case. WPO (whole program optimization) as well as other kinds like profile-guided optimization work great when all the code resides in the same executable. There's no way to "optimize out" a function from a dll once it has been compiled, not to mention other types of optimizations possible.
I didn't quite understand. Especially the highlighted part.
0
 
ambienceCommented:
Depending upon how intelligent the optimizer is it may be able to access how a certain function is "used" in an application. Since all static libs become part of the application - for the purpose of optmization the optimizer does not have to consider anything beyond the application. Therefore it may be able to make decisions that may be only viable within an application. For example take this hypothetical case - I think its far fetched and probably there isnt any optimizer that would do such inferences but just for idea say we have a fucntion

enum Options {
 o1, o2, o3,
};

API_EXPORT void foo(Options o) {
switch(o) {
 case o1: foo1(); break;
 case o2: foo2(); break;
 case o3: foo3(); break;
}
}


now the optimizer sees that the only use of foo() within an application is with "o1"

foo(o1);

It can then shortcut and replace all calls to foo(01) by foo1() and remove foo2(), foo3() and foo().

However if foo() were in a dll (exported) this is not possible at all.

I hope you see the point.
0
 
theKashyapCommented:
I do.
Thanks.
0

Featured Post

Technology Partners: 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!

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