[CMake] To specify dependency to other library

Per Rosengren perrose at kth.se
Tue Dec 2 06:41:17 EST 2008


Per Rosengren wrote:
> I have two programs foo and bar.
> All classes and functionality that could potentially be used by other
> programs are in the library mor.
> foo and bar include mor's headers and link to it.
> mor will be used by other developers. foo and bar should not be
> accessible by them.
> 
> In my current setup, mor, foo and bar are all separate cmake projects in
> separate directories, in separate svn repositories.
> 
> My problem is that foo and bar does not have the mor files as
> dependencies in the generated Makefiles, and so are not properly rebuilt
> when the mor headers or library change. What would be the best solution?

I have improved my setup with the following changes:

> mor is built and installed with
> ADD_LIBRARY( mor
> STATIC <mor sources> )
> INSTALL( TARGETS mor
>   RUNTIME DESTINATION bin
>   LIBRARY DESTINATION lib
>   ARCHIVE DESTINATION lib )
> INSTALL(
> FILES <mor headers>
> DESTINATION include/mor )

ADD_LIBRARY( mor
STATIC <mor sources> )
INSTALL( TARGETS mor EXPORT mor
  RUNTIME DESTINATION bin
  LIBRARY DESTINATION lib
  ARCHIVE DESTINATION lib )
INSTALL(
FILES <mor headers>
DESTINATION include/mor )
INSTALL( EXPORT mor
  DESTINATION cmake )

> foo and bar include the installed mor headers with
> #include <mor/<Class>.hh>

#include "mor/<Class>.hh"

> and they link with the cmake command
> ADD_EXECUTABLE( foo foo.cpp )
> TARGET_LINK_LIBRARIES( foo mor )

SET( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_INSTALL_PREFIX}/cmake )
INCLUDE( mor )
ADD_EXECUTABLE( foo foo.cpp )
TARGET_LINK_LIBRARIES( foo mor )

This makes foo rebuild the affected files whenever mor is rebuilt and
installed. If mor's sources are changed, and mor is not manually rebuilt
and installed, foo is not aware of those changes. It would be convenient
if a "make install" was automatically run on mor before every build of
foo. As I understand it the exported mor.cmake does not keep track of
the cmake project it came from. If it did, I could run make install on
that project from foo's CMakeLists.txt.


More information about the CMake mailing list