[CMake] Setting dependencies between libraries (cmake projects)

Michael Wild themiwi at gmail.com
Fri Nov 4 06:14:02 EDT 2011


On 11/04/2011 11:07 AM, Ludovic Hoyet wrote:
[fixed top-post]
> 
> 2011/11/4 Michael Wild <themiwi at gmail.com <mailto:themiwi at gmail.com>>
> 
>     On 11/04/2011 10:47 AM, Ludovic Hoyet wrote:
>     > Hi,
>     >
>     > I am using cmake to build an application made up of a dozen
>     projects. We
>     > use cmake to automatically generate solutions for x86, x64, and
>     both VS
>     > 2005 and 2010.
>     >
>     > Here is an idea of our organisation:
>     >
>     >   * a.lib, which has no dependency
>     >   * b.lib, which has no dependency
>     >   * c.lib, which depends on a and b
>     >   * d.dll, which depends on c
>     >
>     > Each project lies in its own subdirectory, and has its own
>     > CMakeLists.txt file. In order to keep track of the lib/dll
>     generated for
>     > our different platforms, we automatically post-fix each lib/dll with
>     > _x86/_x64 and _vc80/_vc100 (e.g., a_x86_vc100.lib), and with an _d for
>     > debug (e.g., a_x86_vc100_d.lib).
>     >
>     > In the CMakeLists.txt files, I use target_link_libraries to link each
>     > target with the corresponding libraries, for instance:
>     >
>     >     TARGET_LINK_LIBRARIES( c debug a_${VS}_${PLATFORM}/d optimized
>     >     a/${VS_DIR}/${PLATFORM} debug b/${VS}_${PLATFORM}/d optimized
>     >     b/${VS_DIR}_${PLATFORM})
>     >
>     > In Visual Studio, the different "Project Dependencies" between the
>     > various projects do not appear. I wonder if there is something I miss,
>     > or if it is simply not compatible with our library post-fix. I also
>     > tried to use ADD_DEPENDENCIES(c a b) but it does not seem to work
>     either.
>     >
>     > Does anyone has an idea how to solve this?
>     >
>     > Cheers,
>     >
>     > Ludovic
> 
> 
>     Are these projects completely isolated, i.e. there is no higher-level
>     CMakeLists.txt file that calls add_subdirectory() for each of the
>     project directories?
> 
>     1: if they belong to the same project, then you should use the *target*
>     names, not the *output* names in target_link_libraries().
> 
>     2: if they indeed are independent projects, you should take a look at
>     this:
>     http://www.cmake.org/Wiki/CMake/Tutorials/How_to_create_a_ProjectConfig.cmake_file
>     and for each of the libraries create a <libname>Config.cmake file.
> 
>     A bit more information on how your projects are structured would help.
>     Also, showing example code is always useful.
> 
>     HTH
> 
>     Michael
>
> No, they all belong to a higher-level project. There is a root directory
> with a CMakeLists.txt. This file calls ADD_SUBDIRECTORY(dir_i) for each
> subproject (a, b, c and d). Each sub-project is in his own directory,
> with a CMakeLists.txt for each of them.
>
> Root : CMakeLists.txt
>   - dir_a :  CMakeLists.txt
>   - dir_b :  CMakeLists.txt
>   - dir_c :  CMakeLists.txt
>   - dir_d :  CMakeLists.txt
>
> In the root CMakeLists.txt I call
>   ADD_SUBDIRECTORY(dir_a)
>   ADD_SUBDIRECTORY(dir_b)
>   ADD_SUBDIRECTORY(dir_c)
>   ADD_SUBDIRECTORY(dir_d)
>
> My a CMakeLists contains:
> SET(a project_a)
> add_library(${a} ${SRCS_a} ${HDRS_a})
> SET_TARGET_PROPERTIES(${a} PROPERTIES OUTPUT_NAME ${a} DEBUG_POSTFIX
> "_${VS_DIR}_${PLATFORM}_d" RELEASE_POSTFIX "_${VS_DIR}_${PLATFORM})
>
> My c CMakeLists contains:
> SET(c project_c)
> add_library(${c} ${SRCS_c} ${HDRS_c})
> ADD_DEPENDENCIES( ${c} ${a} )
>
> I hope it is clearer now and that I provided the required information...
>
> Ludo

Your c/CMakeLists.txt should look like this:

set(c project_c)
add_library(${c} ${SRCS_c} ${HDRS_c})
target_link_libraries(${c} project_a project_b)


In CMake you should always use *target* names, CMake handles the rest
for you. Also, the dependencies will be automagically handles this way.


Michael


More information about the CMake mailing list