[CMake] Dealing with circular linker dependencies

Alexandru Ciobanu aciobanu at matrox.com
Thu Oct 4 16:51:32 EDT 2007


Hi!

I was able to build three circularly dependent libs with CMake.
Still I strongly recommend listening to the advices of refactoring
your code.

If there is no other way around it here is what I have used.

The procedure to follow ( on Linux ) where libfoo and libbar are
the libraries you have.

1. build a tmp version of libfoo using something like

   add_library ( foo_tmp SHARED ${foo_src} )
   set_target_properties ( foo_tmp PROPERTIES OUTPUT_NAME foo )

   NOTE: OUTPUT_NAME is important so that you have the correct
   SONAME in case you build shared libraries

   NOTE2: you'll have to add a POST_BUILD custom command to rename
   the resulting lib, so that step 2 will work.


2. build libbar using the tmp version of foo

   add_library ( bar SHARED ${bar_src} )
   target_link_libraries ( bar foo_tmp )


3. build the final version of libfoo using libbar

   add_library ( foo SHARED ${foo_src} )
   target_link_libraries ( foo bar )

   NOTE: this will recompile the libfoo sources, but you can avoid that
   by feeding the *.o files into add_library() instead of the sources.

I hope this helps.

sincerely,
Alex Ciobanu






Tal Blum wrote:
>
> I have two c++ libraries each depending on the other which creates a 
> circular linker dependency problem. Does anyone know how to solve this 
> problem? If this is not possible to solve at the command-line level 
> can someone tell me how I can write a ADD_CUSTOM_COMMAND or 
> ADD_CUSTOM_TARGET that will collect all the object files into a new 
> library and make my executable depend on that new library target.
>
>  
>
> Thanks, Tal
>
>  
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> CMake mailing list
> CMake at cmake.org
> http://www.cmake.org/mailman/listinfo/cmake



More information about the CMake mailing list