[CMake] To specify dependency to other library

Per Rosengren perrose at kth.se
Sat Nov 29 07:34:15 EST 2008


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?

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 )

foo and bar include the installed mor headers with
#include<mor/<Class>.hh>,
and they link with the cmake command
ADD_EXECUTABLE( foo foo.cpp )
TARGET_LINK_LIBRARIES( foo mor )

Below are my actual CMakeFiles.txt files.

Thank you,
Per Rosengren


CMakeLists.txt for mor:
-------------------------------
#if you don't want the full compiler output, remove the following line
#SET(CMAKE_VERBOSE_MAKEFILE ON)

SET( LTI_CXXFLAGS "`lti-config --cxxflags debug`" )
SET( LTI_LIBS -llapack -lblas -lpthread -lm -lz -ljpeg -lpng
-lgthread-2.0 -lrt -lglib-2.0 -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0
-lgdk_pixbuf-2.0 -lm -lpangocairo-1.0 -lpango-1.0 -lcairo -lgobject-2.0
-lgmodule-2.0 -ldl -lglib-2.0 -L${CMAKE_INSTALL_PREFIX}/lib/ltilib -lltid )

INCLUDE( FindPkgConfig )
PKG_SEARCH_MODULE( OSG REQUIRED openscenegraph )

#add definitions, compiler switches, etc.
ADD_DEFINITIONS( ${LTI_CXXFLAGS} ${OSG_CFLAGS} ${OSG_CFLAGS_OTHERS} )

#list all source files here
ADD_LIBRARY( mor
STATIC DeformationExtractor.cpp PoseAndDeformationEstimator.cpp
ClosestLongEdge.cpp PoseEstimator.cpp KeyPointList.cpp KeyPoint.cpp
VectorWithCrossProduct.cpp VectorWithNorm.cpp EdgeFinder.cpp
BundleAdjuster.cpp CameraParams.cpp ModelDeformer.cpp Quaternion.cpp
TriangleMeshIntersector.cpp )

ADD_EXECUTABLE(test-sort-samples test-sort-samples.cpp)
ADD_EXECUTABLE(test-closest-offset test-closest-offset.cpp)

#need to link to some other libraries ? just add them here
TARGET_LINK_LIBRARIES(test-closest-offset mor ${LTI_LIBS} )

INSTALL( TARGETS mor
  RUNTIME DESTINATION bin
  LIBRARY DESTINATION lib
  ARCHIVE DESTINATION lib )
INSTALL(
FILES DeformationExtractor.hh PoseAndDeformationEstimator.hh
ClosestLongEdge.hh  PoseEstimator.hh KeyPointList.hh KeyPoint.hh
VectorWithCrossProduct.hh VectorWithNorm.hh EdgeFinder.hh
BundleAdjuster.hh CameraParams.hh ModelDeformer.hh Quaternion.hh
SortedWorm.hh InverseRenderer.hh TriangleMeshIntersector.hh
MatlabReaderWriter.hh
DESTINATION include/mor )

######################################################################

CMakeLists.txt for foo or bar - here linematcher-test and
GradientDescent-test:
-------------------------------
#if you don't want the full compiler output, remove the following line
SET(CMAKE_VERBOSE_MAKEFILE ON)

SET( LTI_CXXFLAGS "`lti-config --cxxflags debug`" )
SET( LTI_LIBS "'$(lti-config --libs debug)'" )
SET( LTI_LIBS -llapack -lblas -lm -lz -ljpeg -lpng -pthread
-lgthread-2.0 -lrt -lglib-2.0 -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0
-lgdk_pixbuf-2.0 -lm -lpangocairo-1.0 -lpango-1.0 -lcairo -lgobject-2.0
-lgmodule-2.0 -ldl -lglib-2.0 -L${CMAKE_INSTALL_PREFIX}/lib/ltilib -lltid )

INCLUDE( FindPkgConfig )
PKG_SEARCH_MODULE( OSG REQUIRED openscenegraph )
PKG_SEARCH_MODULE( PL REQUIRED plplotd-c++ )
INCLUDE_DIRECTORIES( BEFORE ${PL_INCLUDE_DIRS} )

#add definitions, compiler switches, etc.
ADD_DEFINITIONS( ${LTI_CXXFLAGS} ${OSG_CFLAGS} ${OSG_CFLAGS_OTHERS} )

#list all source files here
ADD_EXECUTABLE( linematcher-test main.cpp )
ADD_EXECUTABLE( GradientDescent-test GradientDescent-test.cpp )

#need to link to some other libraries ? just add them here

TARGET_LINK_LIBRARIES( linematcher-test mor ${LTI_LIBS} ${OSG_LDFLAGS}
${OSG_LDFLAGS_OTHERS} ${OSG_LIBRARY_DIRS} ${OSG_LIBRARIES}
${PL_LIBRARIES} sba boost_filesystem)

TARGET_LINK_LIBRARIES( GradientDescent-test mor ${LTI_LIBS}
${OSG_LDFLAGS} ${OSG_LDFLAGS_OTHERS} ${OSG_LIBRARY_DIRS} ${OSG_LIBRARIES})

INSTALL( TARGETS linematcher-test
  RUNTIME DESTINATION bin
  LIBRARY DESTINATION lib
  ARCHIVE DESTINATION lib )




More information about the CMake mailing list