简体   繁体   中英

CMAKE: automatically add dependencies of dependencies

I am trying to migrate a boost-build build system to cmake.

One of the features boost-build has is automatically linking dependencies of dependencies.

For example:

boost-build:

I'm building an executable app . It depends on lib2

exe app
:   [ glob *.cpp ]
    /proj/lib2//lib2
;

In turn, lib2 depends on lib1

lib lib2
:   [ glob *.cpp ]
    /proj/lib1//lib1
;

and lib1 has no dependencies

lib lib1
:    [ glob *.cpp ]
;

Both lib1 and lib2 are static libs.

boost-build will automatically add lib1.a to the linker line for app because it knows that lib2.a depends on lib1.a

cmake:

Explicitly stating both lib1 and lib2 in the target_link_libraries directive works:

lib1:

add_library(lib1 STATIC ${SOURCES})

lib2:

add_library(lib2 STATIC ${SOURCES})

app:

add_executable(app ${SOURCES})
target_link_libraries(app lib1 lib2)

As the number of libraries grows this becomes cumbersome.

target_link_libraries(app lib1 lib2 lib3 lib4 lib5 lib6 lib7 lib8 lib9 ... libN)

Questions:

  • Is there a way to specify that lib2 depends on lib1
  • Is there a way to tell app to pull in lib2 and whatever lib2 depends on?

It's as simple as adding target_link_libraries to lib2

lib1:

add_library(lib1 STATIC ${SOURCES})

lib2:

add_library(lib2 STATIC ${SOURCES})
target_link_libraries(lib2 lib1)

app:

add_executable(app ${SOURCES})
target_link_libraries(app lib2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM