cmake: figuring out which libraries is a binary ta

2019-08-26 07:51发布

问题:

I wand to implement in CMake a functionality that would enable the installation of a single binary/target through a make install-TARGET command. This is fairly straightforward to do with a custom target. However, when the target binary in question is linked dynamically against other libs of the project (BUILD_SHARED_LIBS=ON), I need to install the receptive libs as well. Is there any way to somehow query the list of libraries?

I've looked at the target properties, but haven't found anything relevant.

Tips on how to get the list of libs and/or other ways to implement the above described functionality would be very much appreciated!

[Edit]

Example:
Let's assume that there the project MyProj has a CMake target "myprog" which generates the binary myprog. I want to install only this binary with make install-myprog. However myprog links against libmy1.so and the latter links against libmy2.so, both part of MyProj. I need a mechanism to figure out that I need to install both libmy1.so and libmy2.so along myprog.

回答1:

I don't do *nix development, but the functionality you're looking for is same as "Dependency Walker" for windows. A quick search brought up the following:

Dependency resolution in Linux

Hope it helps.



回答2:

The most elegant solution seems to be the following. One has to use the CMake COMPONENT parameter of the install command to assign each install target to a component. For example in the question this would be something like this:

install(TARGETS myprog DESTINATION ${BIN_DEST_DIR} COMPONENT myprog),

and similarly for the shared libraries

install(TARGETS my1 my2 DESTINATION ${LIB_DEST_DIR} COMPONENT my-libs).

Now, to invoke the installation of myprog as well as mylib1 and mylib2 a custom target has to be created that uses the cmake_install.cmake locate in the build tree:

add_custom_target(install-myprog
   COMMAND ${CMAKE_COMMAND} -DCOMPONENT=my-libs -P ${CMAKE_BINARY_DIR}/cmake_install.cmake
   COMMAND ${CMAKE_COMMAND} -DCOMPONENT=myprog -P ${CMAKE_BINARY_DIR}/cmake_install.cmake
   COMMENT "Installing myprog").