官方文档:https://cmake.org/cmake/help/latest/command/install.html 参考文档:https://stackoverflow.com/questions/10487256/cmake-how-to-properly-copy-static-librarys-header-file-into-usr-include
例子
project(myproject)
add_library(mylib some.c another.c)
set_target_properties(mylib PROPERTIES PUBLIC_HEADER "some.h;another.h")
INSTALL(TARGETS mylib
LIBRARY DESTINATION some/libpath
PUBLIC_HEADER DESTINATION some/includepath
)
官网语法
install(TARGETS <target>... [...])
install(IMPORTED_RUNTIME_ARTIFACTS <target>... [...])
install({FILES | PROGRAMS} <file>... [...])
install(DIRECTORY <dir>... [...])
install(SCRIPT <file> [...])
install(CODE <code> [...])
install(EXPORT <export-name> [...])
install(PACKAGE_INFO <package-name> [...])
install(RUNTIME_DEPENDENCY_SET <set-name> [...])
The following table shows the target types with their associated variables and built-in defaults that apply when no destination is given:
Target Type | GNUInstallDirs Variable | Built-In Default |
---|---|---|
RUNTIME | ${CMAKE_INSTALL_BINDIR} | bin |
LIBRARY | ${CMAKE_INSTALL_LIBDIR} | lib |
ARCHIVE | ${CMAKE_INSTALL_LIBDIR} | lib |
PRIVATE_HEADER | ${CMAKE_INSTALL_INCLUDEDIR} | include |
PUBLIC_HEADER | ${CMAKE_INSTALL_INCLUDEDIR} | include |
FILE_SET (type HEADERS) | ${CMAKE_INSTALL_INCLUDEDIR} | include |
Each <artifact-kind> <artifact-option>
... group applies to Output Artifacts of the specified artifact kind:
-
ARCHIVE
Target artifacts of this kind include: -
Static libraries (except on macOS when marked as FRAMEWORK, see below);
-
DLL import libraries (on all Windows-based systems including Cygwin; they have extension .lib, in contrast to the .dll libraries that go to RUNTIME);
-
On AIX, the linker import file created for executables with ENABLE_EXPORTS enabled.
-
On macOS, the linker import file created for shared libraries with ENABLE_EXPORTS enabled (except when marked as FRAMEWORK, see below).
-
LIBRARY
Target artifacts of this kind include: -
Shared libraries, except
-
DLLs (these go to RUNTIME, see below),
-
on macOS when marked as FRAMEWORK (see below).
-
-
RUNTIME
Target artifacts of this kind include: -
Executables (except on macOS when marked as MACOSX_BUNDLE, see BUNDLE below);
-
DLLs (on all Windows-based systems including Cygwin; note that the accompanying import libraries are of kind ARCHIVE).
-
OBJECTS
Added in version 3.9. Object files associated with object libraries. -
FRAMEWORK
Both static and shared libraries marked with the FRAMEWORK property are treated as FRAMEWORK targets on macOS. -
BUNDLE
Executables marked with the MACOSX_BUNDLE property are treated as BUNDLE targets on macOS. -
PUBLIC_HEADER
Any PUBLIC_HEADER files associated with a library are installed in the destination specified by the PUBLIC_HEADER argument on non-Apple platforms. Rules defined by this argument are ignored for FRAMEWORK libraries on Apple platforms because the associated files are installed into the appropriate locations inside the framework folder. See PUBLIC_HEADER for details. -
PRIVATE_HEADER
Similar to PUBLIC_HEADER, but for PRIVATE_HEADER files. See PRIVATE_HEADER for details. -
RESOURCE
Similar to PUBLIC_HEADER and PRIVATE_HEADER, but for RESOURCE files. See RESOURCE for details. -
FILE_SET <set-name>
Added in version 3.23. File sets are defined by the target_sources(FILE_SET) command. If the file setexists and is PUBLIC or INTERFACE, any files in the set are installed under the destination (see below). The directory structure relative to the file set's base directories is preserved. For example, a file added to the file set as /blah/include/myproj/here.h with a base directory /blah/include would be installed to myproj/here.h below the destination. -
CXX_MODULES_BMI
Added in version 3.28. Any module files from C++ modules from PUBLIC sources in a file set of type CXX_MODULES will be installed to the given DESTINATION. All modules are placed directly in the destination as no directory structure is derived from the names of the modules. An empty DESTINATION may be used to suppress installing these files (for use in generic code).