[CMake] Using objects in different targets

Alexander Neundorf a.neundorf-work at gmx.net
Mon May 24 05:12:01 EDT 2010


On Friday 21 May 2010, Christoph Rüdiger wrote:
> Am 20.05.2010 um 13:33 schrieb Michael Hertling:
> > On 05/20/2010 01:04 PM, "Christoph Rüdiger" wrote:
> >> [...]
> >> I've a directory src containing the complete source code of the
> >> program and
> >> another directory called test containing the source code for the
> >> unit tests.
> >> In the top level directory is a CMakeLists.txt that points to the
> >> subdirectories containing each an own CMakeLists.txt for building
> >> the program or the unit tests.
> >>
> >> Now I want to use the already build object files from the src
> >> directory one time for linking the program and one time for linking
> >> to the unit tests.
> >>
> >> My current way is building a static library in the src directory
> >> and link the unit tests against this library. But the sources would
> >> be compiled twice: One time for the static library and one time for
> >> the program itself.
> >
> > Link the program against the static library, too, while removing
> > the latter's source files from the program's ADD_EXECUTABLE().
>
> Then I need either one big library which I can link against each unit
> test, resulting in a unit test of the size of the whole program plus
> the size of the unit test,

A static library consists on UNIX just of a bunch of object files.
When linking a program against a static library, only those object files will 
be included in the resulting executable, which contain symbols which are 
referenced.
So if your unit test uses only stuff from one object file (and this object 
file doesn't use symbols from other object files in the static library), only 
this one object file will be included in the resulting executable.

So, the unit tests shouldn't become too big.

I guess it's somewhat similar under Windows.

Alex


More information about the CMake mailing list