修改最外层的CMakeLists.txt, 添加几行代码:
# If GNUInstallDirs is not included, CMAKE_INSTALL_BINDIR is empty.
include(GNUInstallDirs)# it must go before project in order to work
set(CMAKE_INSTALL_PREFIX "${PROJECT_SOURCE_DIR}" CACHE PATH "Installation Directory" FORCE)# install
include(cmake/InstallLinux.cmake)# cpack
include(InstallRequiredSystemLibraries)
set(CPACK_GENERATOR "ZIP")
set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY OFF)
set(CPACK_PACKAGE_NAME "${CMAKE_PROJECT_NAME}")include(CPack)
include(GNUInstallDirs)的作用是包含GNUInstallDirs模块。这个模块定义了一些CMake变量,这些变量包含了在GNU/Linux系统上安装软件时使用的标准目录。这些变量包括CMAKE_INSTALL_PREFIX、CMAKE_INSTALL_FULL_INCLUDEDIR、CMAKE_INSTALL_FULL_LIBDIR等等,它们定义了安装目录结构的各个部分。
通过包含GNUInstallDirs模块,您可以在CMake脚本中使用这些预定义的变量,而无需手动指定安装目录。这样可以使您的CMake脚本更加通用,因为它可以自动适应GNU/Linux系统上的标准安装目录结构。
例如,您可以在CMakeLists.txt中使用${CMAKE_INSTALL_LIBDIR}来引用安装目录中的库文件目录,而无需手动指定这个目录。这样可以使您的软件在不同的GNU/Linux系统上更容易地进行安装和部署。
最初,在我没有include(GNUInstallDirs)的时候,打印${CMAKE_INSTALL_BINDIR}的值就是空的。
cmake/InstallCommon.cmake:
# note: The cpack packaging command requires that the installation path of the install command is a relative path,
# and its relative is CMAKE_INSTALL_PREFIX.# Install model engine file
install(DIRECTORY model/ DESTINATION ${CMAKE_INSTALL_BINDIR}/model/ COMPONENT dev)# install npy_files
install(DIRECTORY npy_files/ DESTINATION ${CMAKE_INSTALL_BINDIR}/npy_files/ COMPONENT dev)# install include files
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_BINDIR}/include/ COMPONENT dev)install(FILES build/libocc_alg.so DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT dev)
cmake/InstallLinux.cmake:
include(cmake/InstallCommon.cmake)
这样运行sudo make install即完成了功能,将发布需要的model文件,npy文件,头文件以及libocc_alg.so动态库文件install到bin目录下。