How do I configure install(RUNTIME/LIBRARY/ARCHIVE) correctly in CMake for C++?

In CMake, the `install` command is used to specify how to install targets such as executables, libraries, or archives. The parameters RUNTIME, LIBRARY, and ARCHIVE define how different types of output are installed. Below is an explanation of each type and a simple example to demonstrate their use.

Understanding CMake install Types

  • RUNTIME: This is used for executables. It specifies where to install executable files.
  • LIBRARY: This is used for shared libraries. It defines where to install shared library files.
  • ARCHIVE: This is used for static libraries. It specifies where to install static library files.

Example of CMake Install Command

cmake_minimum_required(VERSION 3.10) project(MyProject) add_library(my_shared_lib SHARED src/my_shared_lib.cpp) add_library(my_static_lib STATIC src/my_static_lib.cpp) add_executable(my_executable src/main.cpp) install(TARGETS my_shared_lib RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib/static) install(TARGETS my_static_lib ARCHIVE DESTINATION lib/static) install(TARGETS my_executable RUNTIME DESTINATION bin)

This configuration example specifies the installation locations for a shared library, a static library, and an executable. The shared and static libraries are directed to the `lib` and `lib/static` directories, while the executable is installed to the `bin` directory.


CMake install RUNTIME LIBRARY ARCHIVE C++ configuration example