tags:

views:

193

answers:

3

I am trying to expose a single well defined class by building a static library and then shipping the built library with a few header files that define that class and the interfaces needed to use it. I have that working but the problem I am running into is the library is gigantic. It has every single object file from the whole project and all I need is a subset. If I make a simple main.cpp file and include and use that single class then I get a output file that is only as big as the 20% of the library I am using. Is there a way to tell the linker to start from a given place and prune everything else like in the executable case?

EDIT: I forgot to mention that I am using gcc on cygwin and linux (though I would like a solution that worked with visual studio as well, we generally use that for development but deploy primarily on linux)

A: 

You have to split the project up. Take out the files needed for the library and make that a separate project just building the lib.

The remaining project (with main.cpp in) needs to call the new lib project to get the lib Details depend on what tools and OS you are using to manage the project (e.g. Visual Studio or make or ... )

Mark
What annoys me about it is that it does what I expect when building an executable, clearly the logic to prune the unused classes is there. I feel like it just needs a hint on what needs to be left exposed.
Sam Hendley
static libs do not use the linker but just concatenate together all object files you tell it and no more
Mark
A: 

You haven't told us what toolchain you are using but since you say project, I'm guessing you are using the MS toolchain.

The MS toolchain includes every object from a project into the static library. What you want to do is break your single class into a separate project. You can continue to have a super-project that includes that class so you don't have to modify any of your existing projects.

Now, if you want to take this to the next level, you should consider putting each member of the class into it's own translation unit (i.e. .cpp file). This way, if a user of the class only needs a few parts of the class, they will only need to link in the parts they need.

R Samuel Klatchko
A: 

Make a shared library. It behaves like an executable from the point of view of linking etc. It should do the discarding you mentioned you saw on the executable.

Matthew Herrmann
Yeah that's probably the best solution, I still need to figure out how to export just the symbols I want but after a quick googling that looks relatively straight forward. Thanks!
Sam Hendley