[CMake] TARGET_LINK_LIBRARIES for library targets?

Werner Smekal smekal at iap.tuwien.ac.at
Fri Oct 10 19:23:35 EDT 2008


Hi,

Convey, Christian J CIV NUWC NWPT wrote:
> I've got three libraries, A, B, C.  C uses symbols from B, and B uses 
> symbols from A.
> 
> When I build these libraries as static libraries (libA.a, libB.a, and 
> libC.a), the linker is perfectly happy to produce libC.a even if I 
> haven't told it about B.  It seems that all that matters is, when I'm 
> linking together some executable program that uses libC.a, I have to 
> tell the linker about libB.a and (presumably) libA.a.
> 
> When I tried building my libraries as shared objects, however, the 
> linker seemed much fussier.  When the linker was trying to produce 
> libC.so, it got very upset about the unresolved symbols in libC.so's 
> object code. 
>

at least for MinGW static libraries are just containers of object files, 
so you can't link in other static libraries. If you use them in your 
application you need to link in C and since C depends on B also B and so 
on. Shared libraries are more or less executables and they need all the 
code to run, so you need to link in B and A.


  > To remedy this, I added this line:  "TARGET_LINK_LIBRARIES(C, B)" and
> "TARGET_LINK_LIBRARIES(B, A)".  But I'm not sure I understand all the 
> ramifications of solving the problem this way.

That's correct. Doesn't do anything for static case, but links the 
correct libraries for shared case.
> 
> For example, does that mean that libC.so will export not only its own 
> symbols, but also those provided by B and A?   And would that answer 
> change if I had build B and A as static libraries, while still building 
> C as a shared object?

In that case, symbols of A and B are linked into C. But C doesn't export 
symbols from A and B, just uses them.

> 
> Also, suppose I go back to building all my libraries as static 
> libraries.  Will there be any harm in having those TARGET_LINK_LIBRARIES 
> statements still in my CMakeLists.txt files?  And if I do leave them 
> there, does this mean that I can get away with just writing this:
> 
>    TARGET_LINK_LIBRARIES(myApp C)
> rather than
>    TARGET_LINK_LIBRARIES(myApp C B A)
> since CMake could (potentially) have enough information to realize that 
> myApp has a transitive link-time dependency on B and A?

There was a discussion on this mailing list a week ago about this topic 
and as far as I remember does CMake the correct thing (if you told it 
the dependencies).

Werner

> 
> Thanks,
> Christian
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> CMake mailing list
> CMake at cmake.org
> http://www.cmake.org/mailman/listinfo/cmake


-- 
Dr. Werner Smekal
Institut fuer Allgemeine Physik
Technische Universitaet Wien
Wiedner Hauptstr 8-10
A-1040 Wien
Austria

email: smekal at iap.tuwien.ac.at
web:   http://www.iap.tuwien.ac.at/~smekal
phone: +43-(0)1-58801-13463 (office)
        +43-(0)1-58801-13469 (laboratory)
fax:   +43-(0)1-58801-13499


More information about the CMake mailing list