[CMake] How to link executable with libraries containing circulardependencies

Martin Apel martin.apel at simpack.de
Wed Apr 30 09:36:42 EDT 2008


Newell, James C. wrote:
>> -----Original Message-----
>> From: cmake-bounces at cmake.org [mailto:cmake-bounces at cmake.org] On
>> Behalf Of Martin Apel
>> Sent: Tuesday, April 22, 2008 9:06 AM
>> To: cmake at cmake.org
>> Subject: [CMake] How to link executable with libraries containing
>> circulardependencies
>>
>> Hi there,
>>
>> I am trying to link an executable with multiple libraries, which
>> reference each other in a circular fashion. It seems, that I cannot
>> specify the same library multiple times
>> in TARGET_LINK_LIBRARIES, so the link line will always end up with
>>     
> each
>   
>> library listed exactly once. In my case, this leads to unresolved
>> symbols.
>> I know that circular dependencies are not nice, but it's a large
>> "inherited" project I would like to convert to use CMake.
>> I posted this issue before, trying to resolve it using the --start-
>> group
>> and --end-group options to the linker, but haven't gotten any
>>     
> responses
>   
>> yet.
>> To me it looks, as if CMake is not able to handle linker calls with
>> circular references. Any pointers?
>>
>> Regards,
>>     
>
> I solved it by using the 'add_custom_target'.  The example below isn't
> generic, but could be made so with a bit more work and the example
> assumes the main is contained in one of the libx.a and I used the cxx
> instead of c compiler.
>
> project(myproj)
>
> set(mylibs
>     lib1.a
>     lib2.a
>     lib3.a)
>
> foreach(libs ${mylibs})
>   set(userlibs ${uselibs} somepath/${libs})
> endforeach(libs)
>
> add_custom_target(mymain ALL ${CMAKE_CXX_COMPILER} -g -o mymain
> -Wl,--start-group ${uselibs} -Wl,--end-group ${MY_LINK_FLAGS})
>
> Regards,
> Jim
>
>   
Hi Jim,

thanks for your reply. I tried this approach myself, but the problem 
with it is, that you also have to define custom commands for generating 
the object file containing the main function,
which should rather not be contained in a library. Otherwise one of the 
libraries has to contain exactly one main function, which probably leads 
to creating one additional library for each main
program in the system, which is not nice.

Regards,

Martin


More information about the CMake mailing list