[sldev] CMake library depend tracking

Bill Hoffman bill.hoffman at kitware.com
Tue Jul 29 06:53:55 PDT 2008


In debugging one of the issues with cmake 2.6.1 I found that the Second 
Life CMakeLists.txt files are not handling library dependencies 
correctly.  CMake has the ability to keep track of libraries that depend 
on other libraries.  However, you have to tell CMake what each library 
depends on for this to work.   For example, if you create a library A 
that links to libcurl.a you should do something like this:

include(FindCURL)
add_library(A ...)
target_link_libraries(A ${CURL_LIBRARIES})

The idea is that any library that is "directly" used by A, should be 
linked into A, in the correct order.  Then if any other target links to 
A, it will know to link in A and all of its dependent libraries.
Now when any other executable or library links to A, it will 
automatically get the curl library.  For example:

add_executable(B ...)
target_link_libraries(B A)
# the link line for B will have A and curl in that order.


In Second Life, variables are being used to list dependent libraries. 
For example, the current SL CMakeLists have this:

set(LLMESSAGE_LIBRARIES
     llmessage
     ${CURL_LIBRARIES}
     ${CARES_LIBRARIES}
     ${OPENSSL_LIBRARIES}
     ${CRYPTO_LIBRARIES}
     ${XMLRPCEPI_LIBRARIES}
     )


And any target that links to llmessage will do something like this:

target_link_libraries(linux-crash-logger
   ${LLCRASHLOGGER_LIBRARIES}
     ${LLVFS_LIBRARIES}
     ${LLXML_LIBRARIES}
     ${LLMESSAGE_LIBRARIES}    <<--------
     ${LLVFS_LIBRARIES}
     ${LLMATH_LIBRARIES}
     ${LLCOMMON_LIBRARIES}
     ${UI_LIBRARIES}
     ${BOOST_SIGNALS_LIBRARY}
     ${DB_LIBRARIES}
     )

The right way to do that in CMake, is to tell CMake what libraries 
llmessage depends on.  So you would have something like this:

add_library (llmessage ${llmessage_SOURCE_FILES})
target_link_libraries(llmessage  ${CURL_LIBRARIES}
     ${CARES_LIBRARIES}
     ${OPENSSL_LIBRARIES}
     ${CRYPTO_LIBRARIES}
     ${XMLRPCEPI_LIBRARIES})


After that is done, CMake will always make sure that those libraries are 
linked after llmessage.  So, you would just need to do this:
target_link_libraries(linux-crash-logger llmessage  ...)


-Bill


More information about the SLDev mailing list