[CMake] Merge two static libraries

pellegrini pellegrini at ill.fr
Tue Oct 5 04:45:37 EDT 2010


in fact the makefile we would like to generate should be able to:

    - build the console version of library (-DBUILD_WINDOW=False)
    - build the console AND the window versions of the library 
(-DBUILD_WINDOW=True)

this is the latter case that triggered my questions.

Considering that only 2 files out of 50 will have a different 
compilation flag between the console and window mode, that
would be a pity to duplicate the compilation of the 48 common sources 
files. That's why I was looking for a way to produce:

    - once the 48 objects files common to console and window building modes
    - twice the 2 object files specific to the building mode:
        * once in the console mode
        * once in the window mode
    - make a static library for the console mode
    - make a static library for the window mode

That's why to do so, I planned to build:
    - a static library for the 48 common files --> common.a
    - a static library for the 2 specific files in console mode in which 
I would have inserted common.a
    - a static library for the 2 specific files in window mode in which 
I would have inserted common.a

but failed to perform the insertion step ...

Marcel Loose a écrit :
>>>> On 5-10-2010 at 10:10, in message <4CAADD79.7000801 at ill.fr>,
>>>>         
> pellegrini
> <pellegrini at ill.fr> wrote: 
>   
>> Hello everybody,
>>
>> I have a library that can be built for a use in different modes 
>> (console, window). It is made of two sets of files. The first one is
>>     
>
>   
>> common to
>>  the console/window building modes while the second set has to be be
>>     
>
>   
>> built with a slightly different compiler flags depending on the
>>     
> selected
>   
>> building mode.
>>
>> After many discussions here, I was advised to build a static library 
>>     
>
>   
>> for the the files common to console and window building modes
>> (e.g. common.a) and to build a second static library for the files 
>> depending on the building mode (e.g. console.a and window.a) linking
>>     
> the
>   
>> latter to the first one. I did it and ... it worked ! But what I
>>     
> would 
>   
>> like is a little bit different. I would like my console.a (or
>>     
> window.a) 
>   
>> library to
>> contain the object files of the common.a library. Indeed something
>>     
> like 
>   
>> 'ar cr console.a library.a'.
>>
>> Would you have any idea ?
>>
>> thank you
>>
>> Eric
>>     
>
> Hi Eric,
>
> My first question is: why do you want to join these two libraries. I
> understand from your mail that people suggested you to create two
> separate libraries. Now you want to join them again.
>
> To answer your question: you can't join two static libraries, not in a
> portable way. You should specify a complete list of sources in your
> CMakeLists.txt file for each of the two libraries. Something like:
>
> SET(LIB_SOURCES common.c)
> IF(BUILD_CONSOLE)
>   LIST(APPEND LIB_SOURCES  console.c)
>   ADD_LIBRARY(console ${LIB_SOURCES})
> ELSEIF(BUILD_WINDOW)
>   LIST(APPEND LIB_SOURCES window.c)
>   ADD_LIBRARY(window ${LIB_SOURCES})
> ENDIF(BUILD_CONSOLE)
>
> The downside to this solution is that you have duplicates of the object
> files that are part of common, but that's the price you'll have to pay
> if you want to have just one static library.
>
> HTH,
> Marcel Loose.
>   


-- 
Eric Pellegrini
Calcul Scientifique
Institut Laue-Langevin
Grenoble, France



More information about the CMake mailing list