[CMake] Dynamic libraries and library dependencies

Bill Hoffman bill.hoffman at kitware.com
Wed Jan 31 16:55:16 EST 2007


Stefan Buschmann wrote:
> Hi all
>
> I'm trying to build a project that consists of several shared 
> libraries and some applications using those libraries.
> The directory layout is as follows (simplified):
>
> Project
> Project/CMakeLists.txt
> Project/SharedLib
> Project/SharedLib/CMakeLists.txt
> Project/SharedLib/include
> Project/SharedLib/src
> Project/SharedLib/external
> Project/SharedLib/external/include
> Project/SharedLib/external/lib
> Project/SharedLib/external/lib/libFoo.a
> Project/SampleApp
> Project/SampleApp/CMakeLists.txt
> Project/SampleApp/src
>
> Project/CMakeLists.txt:
>  ADD_SUBDIRECTORY(SharedLib)
>  ADD_SUBDIRECTORY(SampleApp)
>
> Project/SharedLib/CMakeLists.txt:
>  INCLUDE_DIRECTORIES(external/include)
>  LINK_DIRECTORIES(external/lib)
>  SET(PROJECT_LIBS Foo)
>  ...
>  ADD_LIBRARY(${PROJECT_TARGET} SHARED ${PROJECT_SOURCES})
>  TARGET_LINK_LIBRARIES(${PROJECT_TARGET} ${PROJECT_LIBS})
>
> Project/SampleApp/CMakeLists.txt:
>  INCLUDE_DIRECTORIES(../SharedLib/include)
>  SET(PROJECT_LIBS SharedLib)
>  ...
>  ADD_EXECUTABLE(${PROJECT_TARGET} ${PROJECT_SOURCES})
>  TARGET_LINK_LIBRARIES(${PROJECT_TARGET} ${PROJECT_LIBS})
>
> Now, when building the project, cmake tries to link libFoo to the 
> sample application instead of linking it to the dynamic library (which 
> is explained in the cmake faq). So it adds -lFoo to the command line, 
> but that library can't be found because the path to the library is 
> unknown (Project/SharedLib/external/lib) as this is only defined in 
> the SharedLib/CMakeLists.txt, but not inside SampleApp/CMakeLists.txt.
> So, is there a way to solve this without having to add the library 
> path to every dependent project (which would in fact make the library 
> abstraction useless)?
Try putting the full path to Foo.  SET(PROJECT_LIBS 
${Project_SOURCE_DIR}/external/lib/libFoo.a)

-Bill



More information about the CMake mailing list