50

I don't know what the argument COMPONENT of the install() command means, and I don't understand the CMake documentation for this. What is it in more detail?

What would an example be?

2 Answers 2

73

You can group installation targets into components, for example, "docs", "libs", "runtime", etc.

add_library(libone libone.c)
add_executable(one main1.c)
install(TARGETS libone one DESTINATION /somedir COMPONENT comp_one)

add_library(libtwo libtwo.c)
add_executable(two main2.c)
install(TARGETS libtwo two DESTINATION /somedir COMPONENT comp_two)

This makes it possible to run cmake -DCOMPONENT=comp_one -P {your_build_dir}/cmake_install.cmake to install only the libone library and the one executable. When you issue make install all components are installed.

11
  • 2
    Excuse me , what's a component. It still confused me, can you give me a test code. then i can run it. and see some difference
    – Samuel
    Feb 9, 2012 at 9:48
  • 8
    I've done some googling and it seems, that using cmake invocation is the only way to do it. But you can wrap it into target using add_custom_target(TARGET install_one COMMAND ${CMAKE_PROGRAM} -DCOMPONENT=comp_one -P {CMAKE_BINARY_DIR}/cmake_install.cmake and then use make install_one.
    – arrowd
    Feb 10, 2012 at 8:26
  • 3
    The install each target command suggested above is broken in several spots, I suggest looking here: stackoverflow.com/a/21853784/258418
    – ted
    Apr 22, 2016 at 15:24
  • 2
    This is also useful when creating CPack component style installers. Unfortunately, CPack is terribly documented, but the following two links are a start: Component Install With CPack and CPackComponent Variables
    – AndrewJC
    Apr 14, 2018 at 0:02
  • 2
    There is a new feature cmake --install <dir> --component <component> since 3.15. Could you add this to your answer?
    – kuga
    Jun 18, 2021 at 10:06
-21

There are three ways you can use the install command in CMake:

  1. Install an executable

    INSTALL(TARGETS ExecutableTarget RUNTIME DESTINATION FOLDER_LOCATION)
    
  2. Install a static library

    INSTALL(TARGETS StaticLibraryTarget ARCHIVE DESTINATION FOLDER_LOCATION)
    
  3. Install dynamic library

    INSTALL(TARGETS SharedLibraryTarget LIBRARY DESTINATION FOLDER_LOCATION)
    
1
  • 16
    -1 because the question was about the meaning of the COMPONENT keyword. This answer demonstrates three kinds of targets which is an orthogonal concept. Jul 8, 2015 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.