[CMake] relinking

James Bigler bigler at cs.utah.edu
Wed Jun 27 10:18:02 EDT 2007


Hi,

Alexandru Ciobanu wrote:
> Hi!
> 
> I am using CMake to build three libraries. They are located
> in separate folders (with separata CMakeLists.txt):
> 
> libA/
>   ./CmakeLists.txt
> 
> libB/
>   ./CmakeLists.txt
> 
> libC/
>   ./CmakeLists.txt
> 
> All the problems are caused by the circular dependency among
> these libraries:
>    libA - depends on libB and libC
>    libB - depends on libA and libC
>    libC - depends on libA and libB
> 
> 
> I am able to build them with CMake, but that involves compiling
> the source files two times (for each library). It is done this way:
> 
> in libA/CmakeLists.txt
>     add_library ( libA_tmp ${srcA} )
> 
>     add_library ( libA ${srcA} )
>     target_link_libraries ( libA libB libC )
> 
> in libB/CmakeLists.txt
>     add_library ( libB_tmp ${srcB} )
>     target_link_libraries ( libB_tmp libA_tmp )
> 
>     add_library ( libB ${srcB} )
>     target_link_libraries ( libB libA_tmp libC_tmp )
> 
> in libC/CmakeLists.txt
>     add_library ( libC_tmp ${srcC} )
>     target_link_libraries ( libC_tmp libA_tmp )
> 
>     add_library ( libC ${srcC} )
>     target_link_libraries ( libC libA_tmp libB_tmp )
> 
> In a nutshell:
>   1. I build tmp version of all the libs ( having a few undefined 
> references )
>   2. I link agains the tmp versions of the libs to build the final 
> (complete) library
> 
> The problem:
>   I have to build each library from sources two times each.
> 
> Is there a way to reuse the generated *.o files from the first 
> add_library() clause?
> What I am looking for is something like this (for libA):
> 
>     add_library ( libA_tmp ${srcA} )
> 
>     # get list of generated object files in ${objA}
> 
>     # now only linking will be done ( no recompile )
>     add_library ( libA ${objA} )
>     target_link_libraries ( libA libB libC )
> 
> So, is there a simple way to achieve this? Or I would have to generate the
> list of objects from the list of source files by hand?

I'm not sure about the object file thing.  I know that where the object 
files get placed is not exposed in the CMake API, but there are examples 
of this being done (I think the Chicken language build does something 
like this).

Taking that aside, one would ask why you have three separate libraries 
that depend on the contents of each other.  If it's possible why not 
make just a single library from the sources in A, B and C?  I've done 
this in my project for sources that span several directories.

Another possibility is to create a fourth single library from that links 
against lib{A,B,C}_tmp and use that single library in place of 
lib{A,B,C} thus resolves all the dependencies.

In general, I've been more happy avoiding circular library dependencies 
than trying to support them.  Some operating systems can get really 
cranky trying to do this.

James


More information about the CMake mailing list