在cmake项目里,最终生成的可执行文件往往需要一些配置才能正常运行,这些配置可能会存储入var
, etc
目录,不建议自己写死install路径,或者将install的函数再封装都是不推荐的,毕竟会增加额外的耦合,推荐用cmake内置变量指定安装文件,如下:
CMAKE_INSTALL_BINDIR: bin
CMAKE_INSTALL_DOCDIR: share/doc
CMAKE_INSTALL_INCLUDEDIR: include
CMAKE_INSTALL_DATADIR: share
CMAKE_INSTALL_INFODIR: share/info
CMAKE_INSTALL_LIBDIR: lib
CMAKE_INSTALL_MANDIR: share/man
CMAKE_INSTALL_SBINDIR: sbin
CMAKE_INSTALL_LIBEXECDIR: libexec
CMAKE_INSTALL_SYSCONFDIR: etc
CMAKE_INSTALL_SHAREDSTATEDIR: com
CMAKE_INSTALL_LOCALSTATEDIR: var
CMAKE_INSTALL_RUNSTATEDIR: var/run
CMAKE_INSTALL_DATAROOTDIR: share
CMAKE_INSTALL_LOCALEDIR: share/locale
install(TARGETS my_executable DESTINATION ${CMAKE_INSTALL_BINDIR})
install(FILES my_config.conf DESTINATION ${CMAKE_INSTALL_SYSCONFDIR})
install(FILES my_data_file DESTINATION ${CMAKE_INSTALL_LOCALSTATEDIR})
...
需要注意的是需要导入系统配置, 上面的CMake变量才会有值:
if(WIN32)
include(InstallRequiredSystemLibraries)
elseif(UINIX)
include(GNUInstallDirs)
endif()
如果设置的CMAKE_INSTALL_PREFIX为/home/test/Download/install,那么文件会分别安装到:
/home/test/Download/install/bin
/home/test/Download/install/var
/home/test/Download/install/etc
...
如果重新设置以下路径为opt:
set(CMAKE_INSTALL_BINDIR "opt")
set(CMAKE_INSTALL_LOCALSTATEDIR "opt")
set(CMAKE_INSTALL_SYSCONFDIR "opt")
那么文件会分别安装到:
/home/test/Download/install/opt
/home/test/Download/install/opt
/home/test/Download/install/opt
...